I had this coding question in an interview.I couldnt find an optimum solution to this. what I did was,
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(a[i]+a[j]==k) print a[i], a[j]
But that would give rise to a O(n2) complexity. Is there a better way to solve this??
Array A contains n integers. A pair (i,j) of indexes of the array A is called "K-complementary" if A[i]+A[j] = K. For example, given the array:
A[0]=1 A[1]=8 A[2]=-3 A[3]=0 A[4]=1 A[5]=3 A[6]=-2A[7]=4 A[8]=5
The following pairs are 6-complementary:
(0,8) (1,6) (4,8) (5,5) (6,1) (8,0) (8,4)
For example the pair (4,8) is 6-complementary, because A[4]+A[8] = 1 + 5 = 6
.
Write a function
int complementary_pairs(int k,int[] A);
which given an integer K and an array A of n integers, computes the number of K-complimentary pairs of indexes of the array A. For example for array A:
A[0]=1 A[1]=8 A[2]=-3 A[3]=0 A[4]=1 A[5]=3 A[6]=-2A[7]=4 A[8]=5
and K=6 you should return 7