Find Closest Number to Zero
To solve this problem, we can start by initializing two variables, closest_to_zero
and min_distance
, to store the closest number to zero and its distance respectively. We will initially set closest_to_zero
to the first number in the list, and min_distance
to its absolute value.
We will then iterate over the rest of the numbers in the list. For each number, we compute its distance from zero by taking its absolute value. If this distance is less than min_distance
, we update min_distance
and closest_to_zero
to this new distance and number respectively.
In the case where the distance is equal to min_distance
, we check if the current number is larger than closest_to_zero
. If so, we update closest_to_zero
to this number.
Finally, we return closest_to_zero
.
Here is the Python code:


This code iterates over the list once, so its time complexity is O(n), where n is the length of the list. It uses a constant amount of extra space, so its space complexity is O(1).
The complex conditional is in place to handle two specific cases:
When the current number’s distance from zero is less than the minimum distance found so far. In this case, we want to update
min_distance
andclosest_to_zero
.When the current number’s distance from zero is equal to the
min_distance
, but the current number is larger thanclosest_to_zero
. According to the problem statement, if two numbers are equally close to zero, we should return the larger number. This part of the conditional checks for that scenario and updatesclosest_to_zero
accordingly.
Therefore, the complex conditional ensures we not only find the number closest to zero, but also the larger number in case of a tie for closest distance.
The building blocks of this solution include:
Iteration: We traverse the entire array once to find the solution, which is a basic operation in many arrayrelated problems.
Comparison: At each step, we compare the absolute value of the current number with the smallest absolute value we’ve found so far. Comparison is a fundamental operation in many algorithms, particularly those that involve finding a minimum or maximum value.
Conditionals: We use ifelse statements to handle two distinct scenarios: when the current number’s distance from zero is less than the minimum distance found so far, and when the current number’s distance from zero is equal to the minimum distance but the current number itself is larger.
Mathematical Operations: We make use of absolute values and basic arithmetic operations.
Variable tracking: We keep track of the smallest absolute value (
min_distance
) and the corresponding number (closest_to_zero
). This is a common technique in problems where we need to find a particular element based on some condition.
Yes, you’re correct. In this problem, the algorithm keeps track of the “Running Minimum” absolute difference from zero as it iterates through the array. At each step, it checks whether the current number’s absolute difference from zero is smaller than the current “Running Minimum”. If it is, it updates the “Running Minimum” and the corresponding number. If it’s equal to the “Running Minimum”, it checks whether the current number is greater than the number currently marked as closest to zero. In that case, it also updates the number. This method is a common technique used in many programming problems to maintain the current minimum or maximum value.
Sure, along with the “Running Minimum” concept, we also have:
Array Traversal: Going through each element in the array is necessary to identify the closest number to zero. This is a basic and fundamental concept for any arraybased problem.
Conditional Checking: Depending upon the absolute difference with zero and also considering the requirement of picking the larger number in case of equal distances, we need to have multiple conditional checks in place. This incorporates logical thinking into our solution.
Absolute Value Calculation: Since we’re measuring distance from zero, the absolute value plays a crucial role in our solution. Irrespective of the sign, we need to find out how ‘far’ a number is from zero.
Updating Variables: Based on the results of our checks, we keep updating our running minimum and closest number. This is another basic programming concept of using variables to hold temporary data that may change as the program executes.
These building blocks together form the complete solution at a high level of abstraction.
Im not sure why does my solution fail for the first test case, equivalent JS code runs without issues.


The error in your solution is due to the index out of range in the condition nums[i] <= nums[i2]
. This condition is checked even when i = 0
or i = 1
, when i2
would be 2
or 1
respectively. In Python, unlike some other languages, negative indices are valid and they refer to positions from the end of the list. So, nums[2]
or nums[1]
would not be the intended comparison for the first two elements.
Also, the condition nums[i] <= nums[i1]
could be problematic for i = 0
.
To fix this, you should start the loop from i = 1
and adjust your conditions to handle edge cases correctly. Here’s a corrected version:


In this corrected version, we only start checking from the second element (index 1) onwards. When checking if nums[i] <= nums[i2]
, we also make sure that i > 1
, which ensures we are not accessing out of bounds indices. This should work as intended and provide the correct results.


The provided C++ function, findClosestNumber
, returns the number in the input vector nums
that is closest to zero. If there are two numbers with the same absolute value, it returns the positive one.
Here’s how it works:
It uses the
min_element
function from the C++ Standard Template Library (STL) to find the element with the smallest value innums
.The
min_element
function takes two iterators (pointers) that define the range of elements to consider (in this case, the whole vector), and a comparison function that determines which element is considered smaller.The comparison function is a lambda function (an anonymous function) that takes two numbers
a
andb
, and returnstrue
ifa
is considered smaller thanb
.In this comparison function, a number is considered smaller if its absolute value is less than the absolute value of the other number, or if their absolute values are equal and the number is greater than the other number (i.e., it’s positive).
So, for example, if the input vector is {3, 2, 2, 4}
, the function will return 2
, because 2
and 2
are both closest to zero, but 2
is positive.
The min_element
function returns an iterator pointing to the smallest element, so we need to dereference it using the *
operator to get the actual number. That’s why the function returns *min_element(...)
.