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