• edited by
1,141 views
0 0 votes

Following C program is written to find the pairs of elements in an array whose sum is 9.

The for loop will iterate over the array of size 7.

The program will print the correct pairs (whose sum is 9) on iteration number ‘a’ and ‘b’.

What are the values of ‘a’ and ‘b’?  Note: for 1st iteration, i=0. For 2nd iteration, i=1 and so on.

 

#include<stdio.h>
#define MAX 10
void pair(int *A, int size, int sum)
{

   int i,j;
   int B[MAX]={0};
   for(i=0;i<size;i++)
   { 
        j= sum - A[i];
       if(j>=0 && B[j] == 1)
            printf("Sum is 9 for :%d %d\n",A[i],j);
        B[A[i]] = 1;
   }
}
void main()
{
   int sum = 9;    
   int A[7]={1,4,3,3,5,2,6};
   pair(A,7,sum);
}

1. 5,7

2. 2,5

3. 4,6

4. 2,3

1 Answer

1 1 vote

the concept behind this question is :-

B[MAX] ---> is array which have indices from 0 to MAX-1

where B[1] represents, is 1 present in the given i/p array or not ?

where B[4] represents, is 4 present in the given i/p array or not ?

in general, where B[k] represents, is "k" present in the given i/p array or not ?

i mean if B[k] = 1, we have k in the given i/p array.

 

coming to the pair() function :-

i indicates the index in the given i/p array ==> a[i] represent the element in the array.

on every iteration, we calculate the value of  j= sum - A[i]; ==> it means we are looking as A[i]+j = sum.

( Note that i is index but j is variable. )

condition i :-    j ≥ 0 means, the value present at the index i, should be ≤ required sum.

condition ii :-  B[j] == 1, checks that the value j is present in the given i/p array.

if these two conditions satisfies then print the values.

 

B[A[i]] = 1; ----> due to this line, we are " A[i] value is present in the i/p array "

i mean to say, if i=0, A[i]=1 ===> B[1] = 1 ===> it means we have 1 in the i/p array.

 

PS : from here onwards you can solve this problem easily. So don't look following continuation !

if you still didn't get it, then check it !!

 

i/p array :-  A[7]={1,4,3,3,5,2,6}

when i = 0, j=sum-A[i] = 9-1 = 8, condition fails, due to line B[A[i]] = 1; we kept B[1] = 1, means we have 1 in the i/p array.

when i = 1, j=sum-A[i] = 9-4 = 5, condition fails, due to line B[A[i]] = 1; we kept B[4] = 1, means we have 4 in the i/p array.

when i = 2, j=sum-A[i] = 9-3 = 6, condition fails, due to line B[A[i]] = 1; we kept B[3] = 1, means we have 3 in the i/p array.

when i = 3, j=sum-A[i] = 9-3 = 6, condition fails, due to line B[A[i]] = 1; we kept B[3] = 1, means we have 3 in the i/p array.

when i = 4, j=sum-A[i] = 9-5 = 4, condition SUCCESS, due to B[4]=1, So the pair is 4 and 5 and due to line B[A[i]] = 1; we kept B[5] = 1, means we have 5 in the i/p array.

when i = 5, j=sum-A[i] = 9-2 = 7, condition fails, due to line B[A[i]] = 1; we kept B[2] = 1, means we have 2 in the i/p array.

when i = 6, j=sum-A[i] = 9-6 = 3, condition SUCCESS, due to B[3]=1, So the pair is 3 and 6 and due to line B[A[i]] = 1; we kept B[6] = 1, means we have 6 in the i/p array.

required answer :- a=4 and b=6

Position:
Show:

Related questions

5 5 votes
2 2 answers
380
380 views
GO Classes asked Jun 30
380 views
What is the output of the following code?#include <stdio.h struct Book { char name[4]; int pages; }; int main() { struct Book b1 = {"CAT", 50}; struct Book b2; b2 = b1; b...
0 0 votes
0 0 answers
450
450 views
balaganesh asked Aug 31, 2018
450 views
the output of the following problem?main(){ int a=1,b=2,c=4; printf("%d",a+=(a+=4,10,a));} Explain?
2 2 votes
1 1 answer
931
931 views
Prayag asked Jul 16, 2018
931 views
#include <stdio.h void f(char ); int main() { char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv); return 0; } void f(char p) { char *t; t...
0 0 votes
1 1 answer
1.1k
1.1k views
Lakshman Bhaiya asked Apr 22, 2018
1,068 views
Q)What is the output of the following program segment#include<stdio.h>int main(){char a = 7 ; a ^ = 5 ;printf( "%d", printf( "%d", a + = 3 ) ) ;return 0;} A) 5 B...