In this tutorial, we will see “How to find two numbers in an array whose sum is closest to zero using Java?”find first non repeated character in a string using Java 8
Java – How to find the two numbers in an array whose sum is closest to zero? Click To Tweet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * Find the two numbers in an array whose sum is closest to zero using Java * @author Deepak Verma * */ import java.util.Arrays; public class Find_two_numbers_in_array_whose_sum_is_closest_to_zero_using_Java { public static void main(String[] args) { int[] numbersArray = {-1, 2, -3, 4, -5, 6, -7, 8, -9}; int[] closestPairOfNumbers = checkNumbersClosestToZero(numbersArray); System.out.println("Two numbers whose sum or addition is closest to zero are: " + closestPairOfNumbers[0] + " and " + closestPairOfNumbers[1]); } public static int[] checkNumbersClosestToZero(int[] numbers) { Arrays.sort(numbers); int left = 0; int right = numbers.length - 1; int closestSumValue = Integer.MAX_VALUE; int[] closestPairArray = new int[2]; while (left < right) { int sumOfNumbers = numbers[left] + numbers[right]; int absOfSum = Math.abs(sumOfNumbers); if (absOfSum < closestSumValue) { closestSumValue = absOfSum; closestPairArray[0] = numbers[left]; closestPairArray[1] = numbers[right]; } if (sumOfNumbers < 0) { left++; } else { right--; } } return closestPairArray; } } |
Output:
1 | Two numbers whose sum or addition is closest to zero are: -9 and 8 |
find first non repeated character in a string using Java 8
In the above example, Our objective is to first sort the array in ascending order using Arrays.sort()
. We then initialize two pointers, left
and right
, to the beginning and end of the array, respectively. We also initialize two variables, closestSumValue
and closestPairArray
, to keep track of the closest sum found so far and the pair of numbers that add up to that sum. We then enter a while
loop that continues until left
and right
meet in the middle. In each iteration of the loop, we calculate the sum of the numbers at left
and right
and take the absolute value of that sum using Math.abs()
. If this absolute sum is closer to zero than any we’ve seen so far, we update closestSumValue
and closestPairArray
. Finally, we move either the left
or right
pointer depending on whether the sum is less than or greater than zero, respectively. This allows us to gradually converge on the two numbers whose sum is closest to zero. At the end of the loop, we return closestPairArray
, which contains the two numbers whose sum is closest to zero.
Java – How to find the two numbers in an array whose sum is closest to zero? Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: