Java Program To Find Two Elements whose Sum is Closest to Zero

«
»
This is a Java Program to Find Two Elements such that their Sum is Closest to Zero.

Enter any integer number as an input which denotes the size of the array. After that add all the required integers in the array. Now we pick all all two possible pairs and compare their sum with the minumum possible sum closest to zero. Since sum can be negative so we use absolute function to get only positive result. Hence the two integers with sum closest to zero are output.

Here is the source code of the Java Program to Find Two Elements such that their Sum is Closest to Zero. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Sum_Zero 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int n, min1 = 0, min2 = 1, sum, minimum;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter the number of elements you want:");
  9.         n = s.nextInt();
  10.         int a[] = new int[n];
  11.         System.out.println("Enter all the numbers:");
  12.         for(int i = 0; i < n; i++)
  13.         {
  14.             a[i] = s.nextInt();
  15.         }
  16.         minimum=Math.abs(a[0] + a[1]);
  17.         for(int i = 0; i < n; i++)
  18.         {
  19.             for(int j = i + 1; j < n; j++)
  20.             {
  21.                 sum = Math.abs(a[i] + a[j]);
  22.                 if(sum < minimum)
  23.                 {
  24.                     min1 = i;
  25.                     min2 = j;
  26.                     minimum = sum;
  27.                 }
  28.             }
  29.         }
  30.         System.out.println("Element 1:"+a[min1]); 
  31.         System.out.println("Element 2:"+a[min2]);
  32.     }
  33. }

Output:

$ javac Sum_Zero.java
$ java Sum_Zero
 
Enter the number of elements you want:5
Enter all the numbers:
-2
-1
3
6
5
Element 1:-2
Element 2:3

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.