## Problem Definition

This problem has appeared in many interviews as well as the qualifying round of Google Code Jam in the past. There are various versions of the problem. To list a few:- Find Pair of Numbers in Array with a Given Sum - The array is unsorted and contains a given range of numbers bounded by min and max.
- Find Pair of Numbers in Array with a Given Sum - The array is sorted and contains a given range of numbers bounded by min and max.
- Find Pair of Numbers in Array with a Given Sum - The array contains unique numbers only.

## Approach - Find Pair of Numbers in Array with a Given Sum

Remember that the elements are not unique but they are bounded by a min and a max range. Let us understand the naive way of solving this problem.##### The Naive Way

Calculate the sum of each pair and store it in a hashmap(key-value pair) such that the key is the sum of the pair and the value is (XX-YY) where XX is the index of the first element and YY is the index of the second element. Now you can just iterate through the hashmap once and find the index pair whose sum matches to the given sum. If there are multiple such pairs then the problem becomes slightly complicated because the hash map in certain language overwrites the values if the key is duplicate. So, one can try to swap the keys and values. This means the index pair (XX-YY) is the key and the sum is the value. This will handle the case of duplicates and works perfectly fine. There might be some space and running time issues. For e.g. The space required will be equal to the number of distinct sums in the first case and number of possible pairs in the second case. So, we can say that the upper bound of space is equal to the number of ways to select 2 items from a list of N numbers where N is the length of the input array and the order of selection doesn't matter. This will be equal to C(N,2) which is (1/2) * N * (N-1) that is O(N^{2}. The running time would have the same upper bound because it needs to calculate the sum of all the pairs, considering adding two numbers is a constant time job. Running Time would be O(N

^{2})

##### A smarter Approach

Say that the sum given is S.- Find the minimum (Min) and maximum (Max) numbers from the array.
- In case the numbers are unique, we can allocate a boolean array of length (Max-Min).
- Iterate through the input array and for each number C encountered, mark that index C in the boolean array as true.
- Iterate through the boolean array and for each index
**i**in the array if the index**S-i**and index**i**both contain true. Then we found our two numbers which can result in sum S. - Break the loop and iterate through the input array to find the numbers and report their indices.

- In case the array contains duplicates, we can allocate an int array of length (Max-Min) and initialize it with zeroes.
- Iterate through the input array and for each number C encountered, increment the content of index C in the int array by 1.
- Iterate through the int array and for each index
**i**in the array if the index**S-i**and index**i**both are greater than zero. Then we found our two numbers which can result in sum S. - Break the loop and iterate through the input array to find the numbers and report their indices.

## Source Code - Find Pair of Numbers in Array with a Given Sum

Here is the source code written in Java for the problem definition, this can be further optimized but it is sufficient to prove the idea.private void test(int credit, int[] price) { int max = 0; for (int i = 0; i < price.length; i++) { if (price[i] > max) max = price[i]; } int[] items = new int[max + 1]; for (int i = 0; i < price.length; i++) { items[price[i]]++; } int num1 = 0, num2 = 0; for (int i = 0; i < items.length; i++) { if (items[i] > 0 && credit - i < items.length && items[credit - i] > 0) { num1 = items[i] > items[credit - i] ? credit - i : i; num2 = items[i] > items[credit - i] ? i : credit - i; break; } } int idx1 = -1, idx2 = -1; for (int i = 0; i < price.length; i++) { if (idx1 <= -1 && num1 == price[i]) { idx1 = i++; } if (idx2 <= -1 && num2 == price[i]) { idx2 = i; } if (idx1 > -1 && idx2 > -1) break; } if (idx2 < idx1) { System.out.print(++idx2 + " " + ++idx1); } else { System.out.print(++idx1 + " " + ++idx2); } System.out.println(); }

**Note:**I didn't take care of the minimum value in the array and the int array is declared for a larger range 0 - Max. But you can choose to add that restriction. This piece of code will just allocate a bigger array and the indices 0 - Min will be always filled with zeroes.