## Infosys Interview Question

Software Engineer / Developers1. Sort the given array by ascending or descending mode by using the quick sort algorithm

2. Subtract one by one every element of the given array from the given number (10) and using the binary search algorithm you will find the next pair.

The complexity of this algorithm is O(LogN)

1. Sorting is O(nlogn). Quicksort even can do this in O(n^2).

2. No one said its only pairs! You can have 7+3+1 or 4+3+2+1.

3. Complexity is O(nlong) and not O(logn) because you do O(logn) for n elements.

Hey, did u apply for Infosys in India or US? Is there any other way to apply to Infosys, apart from their online application? because I don't think their online system is working that well. There is absolutely no response for any application, not even application receipt or acknowledgement. If there is, please do let me know.

Thanks!

This is a dynamic programming problem. Given an array A of numbers:

and the recurrence:

```
T(i) = Set of sets T(i-1), {A[i]}, {{T(j)} union A[i]} for all j < i s.t. sum in the sets + A[i] <= 10
if A(i) <= 10
T(i-1) if A(i) > 10
```

Consider the array and the memoization as follows:

```
A Set of sets < 10
17 {Empty}
6 {6}
9 {6}, {9}
2 {6}, {9}, {2}, {2,6}, {2,9}
7 {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}
8 {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}
1 {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}, {1}, {1,6}, {1,9}, {1,2}, {1,2,6}, {1,7}, {1,7,2}, {1,8}
3 {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}, {1}, {1,6}, {1,9}, {1,2}, {1,2,6}, {1,7}, {1,7,2}, {1,8}
{3}, {3, 6}, {3, 2}, {3,7}, {3, 1} {3, 1, 6}, {3, 1, 2}
4 {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}, {1}, {1,6}, {1,9}, {1,2}, {1,2,6}, {1,7}, {1,7,2}, {1,8}
{3}, {3, 6}, {3, 2}, {3,7}, {3, 1} {3, 1, 6}, {3, 1, 2}, {4}, {4, 6}, {4, 2}, {1, 4}, {1, 2, 4}, {3, 4},
{3, 2, 4}, {3, 1, 4}
7 {6}, {9}, {2}, {2,6}, {2,9}, {7}, {7,2}, {8}, {8,2}, {1}, {1,6}, {1,9}, {1,2}, {1,2,6}, {1,7}, {1,7,2}, {1,8}
{3}, {3, 6}, {3, 2}, {3,7}, {3, 1} {3, 1, 6}, {3, 1, 2}, {4}, {4, 6}, {4, 2}, {1, 4}, {1, 2, 4}, {3, 4},
{3,2,4}, {3,1,4}, {7}, {7,2}, {1,7}, {1,2,7}, {3,7}
```

Now look at the sets of sets and select the ones that sum up to 10! But this algo is O(n^2).

```
public class SubsetSum {
/**
* @param args
*/
public static void main(String[] args) {
int num[] = new int[]{11,16,11,9,7,13,5,3,1,14};
count(num,10);
}
static void count(int num[],int sum){
for(int i=0;i<Math.pow(2, num.length);i++){
int set[]= new int[num.length];
int tsum=0;
for(int j=0;j<num.length;j++){
if((i&(1<<j))!= 0){
set[j] = 1;
tsum+=num[j];
}
}
if(tsum == sum){
for(int k=0;k<num.length;k++){
if(set[k] == 1)System.out.print(" "+num[k]);
}
System.out.print(" : "+sum+" \n");
}
}
}
}
```

using bit operation we can get all the subsets but its limited by the input size :)

Actually the subset sum problem is a NP complete problem.

- m@}{ June 07, 2011