## Microsoft Interview Question

Software Engineer in Tests**Team:**Server tools division

**Country:**United States

**Interview Type:**In-Person

public static void printPair(int[] array, int sum)

{

if (array == null)

{

return;

}

for (int i = 0; i < array.Length; i++)

{

int currentSum = array[i];

for (int j = i + 1; j < array.Length; j++)

{

int newSum = currentSum + array[j];

if (newSum == sum)

{

Console.WriteLine("(" + currentSum + "," + array[j] + ")");

}

newSum = 0;

}

}

}

@pradegup: you should also maintain a count of how many times that value occurs in the array. for example, in the given array, 4 appears twice so when you look up 4, you will try to find if k-4 = 8-4 = 4 exists... and this returns true...

but consider an array where 4 appears only once... and when you do a search on 4, you will return true... but that's not solution...

does this make sense?

@JustCoding: Yes it make sense. Your point is valid.

But this case will arise only if sum is even and (sum/2) is present in array. So we can take care of this as a special case.

```
public static void printAllPairs(int[] a, int sum) {
HashMap<Integer, LinkedList<Integer>> list = new HashMap<>();
for(int i = 0; i < a.length; i++) {
LinkedList<Integer> newList;
if(list.containsKey(sum - a[i])) {
newList = list.get(sum - a[i]);
for(int j = 0; j < newList.size(); j++) {
System.out.println(a[i] + "(" + i + ")" + " ," + (sum - a[i]) + "(" + newList.get(j) + ")");
}
}
if(list.containsKey(a[i])) {
newList = list.get(a[i]);
newList.add(i);
list.put(a[i], newList);
} else {
newList = new LinkedList<>();
newList.add(i);
list.put(a[i], newList);
}
}
}
```

I have used a HashMap , key = value in array, List of all its indexes. this way u can keep track of all the indexes and print all pairs.

The solution given by @pradegup works fine with a little modification. We don't add all the keys initially. Instead,

1)we read an item x.

2)check if sum-x is present in Hash.

3)If yes, then we print x and key (which is sum-x)

Else, we add x to the Hash table.

hash table is best one, but if no additional memory used then

1. we should sort an array in inplace with Quick sort..

2. then keep track of start and end index in array , sum of start + end > given sum then....start++, else end--.

this way, we can get two number without using extra memory

correction- Assuming that the array is sorted in increasing order:

if start+end > given sum, then end-- else start++...

if start + end == sum, return/print those 2 numbers and continue to find more such pairs...

I think using hashmap duplicates cannot be allowed. This works on log(n).When you add a duplicate it updates the value to later one. here is java code

public class SumInArr3 {

public static void findSumHashMap(int[] arr, int key) {

Map<Integer, Integer> valMap = new HashMap<Integer, Integer>();

for (int i = 0; i < arr.length; i++) {

valMap.put(arr[i], i); }

for (int i = 0; i < arr.length; i++) {

if (valMap.containsKey(key - arr[i])

&& valMap.get(key - arr[i]) != i) {

if (valMap.get(key - arr[i]) < i) {

int diff = key - arr[i];

System.out.println(arr[i] + " " + diff);}}}}

public static void main(String[] args) {

int[] arr = { 32, 40, 28, 37, 4, 12, 2, 51, 23, 50, 20, 56, 32,-50,110 };

int sum = 60;

findSumHashMap(arr, sum); }}

This one works on (N^2)/2

public class SumInArr2 {

public static void findSumHashMap(int[] arr, int key) {

for(int i=0;i<=arr.length-2;i++){

for(int j=i+1;j<=arr.length-1;j++){

if (key-arr[i]==arr[j]){

System.out.println(arr[i]+ "index:"+i+" "+arr[j]+ "index:"+j);}}} }

public static void main(String[] args) {

int[] arr={32,40,28,37,4,12,2,51,23,50,20,56};

int sum = 60;

findSumHashMap(arr,sum); }}

```
public class PairIndexSum {
public static void printIndexPair(int[] array, int sum) {
Map<Integer, LinkedList<Integer>> map = new HashMap<Integer, LinkedList<Integer>>();
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
map.get(array[i]).add(i);
} else {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(i);
map.put(array[i], list);
}
}
for (int i = 0; i < array.length; i++) {
if (map.containsKey(sum - array[i])) {
LinkedList<Integer> list = map.get(sum - array[i]);
for (Integer n : list) {
if(i != n){
System.out.println("Index:" + i + " " + n);
}
}
map.remove(array[i]);
map.remove(sum - array[i]);
}
}
}
public static void main(String[] args) {
int[] array = { 1, 4, 4, 3, 7, 5, 8 };
printIndexPair(array, 8);
}
}
```

You can do it in o(n) time complexity by using additional space.

- pradegup October 06, 2012Store all the elements in a hashmap as key-value pair, where key is number and value is index.(this will take O(n) time.)

traverse through the list and for each number see whether (sum-num) is present or not in hashmap.(searching in hashmap will take O(1) time)

if(num is present in hashmap){

print both number with indexes

}else{

do nothing.

}