Find two elements whose sum is closest to zero

Problem statement:- Program to Find a pair whose sum is closest to zero in an Array.

Example:-

              Input : Given 

                                size=5

                                 arr[]=[4,-5,7,3,9] 

             Output:

                        pair(4,-5)   //sum=4-5=-1           

Data requirement:-

   Input Data:- size, arr

  Output Data:-First_element, Second_element

  Additional Data:- i,  j, Closest_Zero_Sum, sum

Program in C

Here is the source code of the C Program to Find a pair whose sum is closest to zero in an Array.

Code:

#include<stdio.h>
#include<math.h>
main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size];
    int i,j;
    printf("Enter the Element of the array:\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }

    int First_element=arr[0], Second_element=arr[1],sum, Closest_Zero_Sum=First_element+Second_element;

   for(i=0;i<size-1;i++)
   {

       for(j=i+1;j<size;j++)
       {
            sum=arr[i]+arr[j];
            if(abs(sum)<abs(Closest_Zero_Sum))
            {
                Closest_Zero_Sum=sum;
                First_element=arr[i];
                Second_element=arr[j];
            }
       }
   }
   printf("Closest to Zero Pair is (%d, %d)", First_element,Second_element);
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4 -5 7 3 9
Closest to Zero Pair is (4, -5)

Program in C++

Here is the source code of the C++ Program to Find two elements whose sum is closest to zero in an array.

Code:

#include<iostream>
#include<cmath>
using namespace std;
main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size];
    int i,temp=0,j;
    cout<<"Enter the Element of the array:\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }
int First_element=arr[0], Second_element=arr[1],sum, Closest_Zero_Sum=First_element+Second_element;

   for(i=0;i<size-1;i++)
   {

       for(j=i+1;j<size;j++)
       {
            sum=arr[i]+arr[j];
            if(abs(sum)<=abs(Closest_Zero_Sum))
            {
                Closest_Zero_Sum=sum;
                First_element=arr[i];
                Second_element=arr[j];
            }
       }
   }
   cout<<"Closest to Zero Pair is ("<<First_element<<","<< Second_element<<")";
}

Input/Output:
Enter the size of the array:6
Enter the Element of the array:
8 7 11 5 2 3
Closest to Zero Pair is (2,3)

Program in Java

Here is the source code of the Java Program to Find a pair whose sum is closest to zero in an Array.

Code:

import java.util.Scanner;
public class SumIsClosestToZero {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
     int size,i,j;
     size=sc.nextInt();
     int arr[ ]=new int[size];
  System.out.println("Enter the Element of the array:");
      for(i=0;i<size;i++)
     {
         arr[i]=sc.nextInt();
     }
  int First_element=arr[0], Second_element=arr[1],sum, Closest_Zero_Sum=First_element+Second_element;

      for(i=0;i<size-1;i++)
      {
          for(j=i+1;j<size;j++)
          {
               sum=arr[i]+arr[j];
               if(Math.abs(sum)<=Math.abs(Closest_Zero_Sum))
               {
                   Closest_Zero_Sum=sum;
                   First_element=arr[i];
                   Second_element=arr[j];
               }
          }
      }
 System.out.println("Closest to Zero Pair is ("+First_element+","+Second_element+")");
      sc.close();
}
}

Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
-4 -9 19 6
Closest to Zero Pair is (-4,6)

Program in Python

Here is the source code of the Python Program to Find two elements whose sum is closest to zero in an array.

Code:

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)

First_element=arr[0]
Second_element=arr[1]
Closest_Zero_Sum=First_element+Second_element

for i in range(0,size-1):
    for j in range(i+1, size):
        sum = arr[i] + arr[j]
        if abs(sum) <= abs(Closest_Zero_Sum):
            Closest_Zero_Sum = sum
            First_element = arr[i]
            Second_element = arr[j]

print("Closest to Zero Pair is (",First_element,",",Second_element,")")

Input/Output:
Enter the Element of the array:
-6
8
9
3
Closest to Zero Pair is ( -6 , 8 )

Post a Comment

0 Comments