Skip to main content

Find two element in an array whose sum is closest to zero

The array is having both +ve and -ve values.Lets take array values as

 3 -7 8 9 5 -3

Then the two numbers with sum closest to zero will be 3 and -3.

Program :

# include <stdio.h>
# include <stdlib.h>
# include <math.h>
void sumclosetozero(int arr[], int size)
{
  int count = 0;
  int a, b, sum_min, sum, a_min, b_min;
  a_min = 0;
  b_min = 1;
  sum_min = arr[0] + arr[1];
  for(a = 0; a < size - 1; a++)
  {
    for(b = a+1; b < size; b++)
    {
      sum = arr[a] + arr[b];
      if(abs(sum_min) > abs(sum))
      {
        sum_min = sum;
        a_min = a;
        b_min = b;
      }
    }
  }

  printf(" The two elements whose sum is minimum are %d and %d",
          arr[a_min], arr[b_min]);
}


int main()
{
  int a[] = {3, -7, 8, 9, 5, -3};
  sumclosetozero(a, 6);
  getchar();
  return 0;
}

Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

This post is about the Tricky Questions   or code snippet in C or C++ asked in most of the Interviews   (See Interview Experience ) by some of the good companies. You may know probably the right concept but it will not strike you at the interview and to crack all those Interview Questions  you should know some of them beforehand. If you are applying for Job related to JAVA then checkout the blog post  Tricky Questions in JAVA . 1) what will be the output of the following Printf function.   printf("%d",printf("%d",printf("%d",printf("%s","ILOVECPROGRAM")))); Ans-ILOVECPROGRAM1321 The above printf line gives output like this because printf returns the number of character successfully written in the output. So the inner printf("%s","ILOVECPROGRAM") writes 13 characters to the output so the outer printf function will print 13 and as 13 is of 2 characters so the next outer printf function will print 2 and then ...

Programs and Puzzles in technical interviews i faced

I have attended interview of nearly 10 companies in my campus placements and sharing their experiences with you,though i did not got selected in any of the companies but i had great experience facing their interviews and it might help you as well in preparation of interviews.Here are some of the puzzles and programs asked to me in interview in some of the good companies. CHECK-OUT the VIDEO of  Technical Interview for SAP Labs, CA Tech & HP R&D 1) SAP Labs I attended sap lab online test in my college through campus placements.It had 3 sections,the first one is usual aptitude questions which i would say were little tricky to solve.The second section was Programming test in which you were provided snippet of code and you have to complete the code (See Tricky Code Snippets  ).The code are from different data structures like Binary Tree, AVL Tree etc.Then the third section had questions from Database,OS and Networks.After 2-3 hours we got the result and i was sh...

Program to uncompress a string ie a2b3c4 to aabbbcccc

Below is the program to uncompress a string #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { char str[100]="a2b3c4d8u7"; for(int i=0;str[i]!='\0';i++) { if(i%2!=0) { for(int j=0;j<atoi(&str[i]);j++) { printf("%c",str[i-1]); } } } getch(); } Want to become a Data Engineer? Check out below blog posts  1.  5 Key Skills Every Data Engineer needs in 2023 2.  How to prepare for Data Engineering Interviews 3.  Top 25 Data Engineer Questions