C Code 23 _ To Find Whether any pair of elements in the given array yields the given sum(Google Interview Question)



C Code 23 _ To Find Whether any pair of elements in the given array yields the given sum(Google Interview Question)

Sample Input:

5                 //# of elements in array
1 2 3 4 5     //array elements
9                //sum

Sample Output:

4 and 5 yields a sum of 9



Program:


#include<stdio.h>
int main()
{
                int a[50],n,i,sum,j,f=0;
                printf("Enter # of elements : ");
                scanf("%d",&n);
                printf("\n\nEnter %d elements : ",n);
                for(i=0;i<n;i++)
                {
                                scanf("%d",&a[i]);
                }
                printf("\n\nEnter the sum to find whether any corresponding pair yielding that sum is available in the array : ");
                scanf("%d",&sum);
                for(i=0;i<n-1;i++)
                {
                                for(j=i;j<n;j++)
                                {
                                                if((a[i]+a[j])==sum)
                                                {
                                                                printf("\n\n%d and %d yields the sum of %d",a[i],a[j],sum);
                                                                f=1;
                                                }
                                }
                }
                if(f==0)
                {
                                printf("\n\nNo pair in the array yiels the given sum");
                }
                return 0;
}




Output:









Thank You ...

Popular Posts