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

This is a C Program to find the two elements such that their sum is closest to zero.

Problem Description

This program finds the two elements such that their sum is closest to zero.

Problem Solution

1. Initialize the array with some numbers.
2. Firstly add the first two elements of array and let it be the minimum sum.
3. Keeping this as default, try all the combinations in the array. Use for loops for doing this.
4. Find the minimum and print the output.

Program/Source Code

Here is source code of the C Program to find the two elements such that their sum is closest to zero. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2. /*
  3.  * C Program to Find the two Elements such that their Sum is Closest to Zero
  4.  */
  5. # include <stdio.h>
  6. # include <stdlib.h>
  7. # include <math.h>
  8.  
  9. void minabsvaluepair(int array[], int array_size)
  10. {
  11.     int count = 0;
  12.     int l, r, min_sum, sum, min_l, min_r;
  13.  
  14.     /* Array should have at least two elements*/
  15.     if (array_size < 2)
  16.     {
  17.         printf("Invalid Input");
  18.         return;
  19.     }
  20.  
  21.     /* Initialization of values */
  22.     min_l = 0;
  23.     min_r = 1;
  24.     min_sum = array[0] + array[1];
  25.     for (l = 0; l < array_size - 1; l++)
  26.     {
  27.         for (r = l + 1; r < array_size; r++)
  28.         {
  29.             sum = array[l] + array[r];
  30.             if (abs(min_sum) > abs(sum))
  31.             {
  32.                 min_sum = sum;
  33.                 min_l = l;
  34.                 min_r = r;
  35.             }
  36.         }
  37.     }
  38.     printf(" The two elements whose sum is minimum are %d and %d", array[min_l], array[min_r]);
  39. }
  40.  
  41. int main()
  42. {
  43.     int array[] = {42, 15, -25, 30, -10, 35};
  44.     minabsvaluepair(array, 6);
  45.     getchar();
  46.     return 0;
  47. }
Program Explanation

1. Initialize the array named array[] with the random values.
2. Call the function minabsvaluepair and pass array and its size as parameters.
3. In the function definition receive the parameters through variables array[] and array_size.
4. If the array_size is less than 2, then print the output as “Invalid Input” and exit.
5. Initialize the variables min_l and min_r with 0 and 1 respectively.
6. Initialize the variable min_sum with the sum of 1st two elements of the array.
7. Using two for loops, compare all the combinations with this default sum.
8. If the minimum one exits, then change the values of variables min_l and min_r with this new array positions and exit.
9. Print the array values with the positions min_l and min_r as output and exit.

advertisement
advertisement
Runtime Test Cases
 The two elements whose sum is minimum are 15 and -10

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.