Similar Problems

Similar Problems not available

Find Closest Number To Zero - Leetcode Solution

Companies:

LeetCode:  Find Closest Number To Zero Leetcode Solution

Difficulty: Easy

Topics: array  

Problem Statement:

Given an array of integers, find the closest number to zero. If there are multiple integers with the same distance, return the positive integer.

Example: Input: nums = [4, -1, 2, -3, 5, -6, 1] Output: 1

Solution: Approach: We will loop through the array and compare each number with the current closest number to zero. If the absolute value of the current number is less than the absolute value of the closest number, we will set the current number as the closest number. If the absolute values are equal, we will check if the current number is positive and the closest number is negative. If so, then we will set the current number as the closest number.

Algorithm:

  1. Initialize a variable closest_number to the maximum value of an integer.
  2. Loop through the array and for each number: a. If the absolute value of the current number is less than the absolute value of the closest number or if the current number is positive and the closest number is negative, then set the current number as the closest number.
  3. Return the closest number.

Detailed Steps: Let's start solving the problem step by step.

Step 1: Initialize a variable closest_number to the maximum value of an integer.

int closest_number = INT_MAX;

Step 2: Loop through the array and for each number, check if it is closer to zero than the previous closest number.

for (int i = 0; i < nums.size(); i++) { if (abs(nums[i]) < abs(closest_number) || (abs(nums[i]) == abs(closest_number) && nums[i] > 0 && closest_number < 0)) { closest_number = nums[i]; } }

In the above code, we are using the abs() function to get the absolute value of the numbers. We are also checking if the current number is positive and the closest number is negative. If so, then we are setting the current number as the closest number.

Step 3: Return the closest number.

return closest_number;

Time Complexity: O(n), where n is the size of the input array.

Space Complexity: O(1), as we are using only one variable.

Complete Code:

class Solution { public: int closestToZero(vector<int>& nums) { int closest_number = INT_MAX; for (int i = 0; i < nums.size(); i++) { if (abs(nums[i]) < abs(closest_number) || (abs(nums[i]) == abs(closest_number) && nums[i] > 0 && closest_number < 0)) { closest_number = nums[i]; } } return closest_number; } };

Test Cases:

Let's test our solution with some input values.

Example 1: Input: nums = [4, -1, 2, -3, 5, -6, 1] Output: 1

Example 2: Input: nums = [6, 9, -3, 4, -2, -5] Output: -2

Example 3: Input: nums = [0, 1, 2, 3, 4, 5] Output: 0

Example 4: Input: nums = [-2, -3, 0, 2, 3] Output: 0

Conclusion: In this problem, we have learned how to find the closest number to zero in an array of integers in linear time using constant space.

Find Closest Number To Zero Solution Code

1