## Adobe Interview Question

**Country:**India

**Interview Type:**Written Test

No set.contains()in java is of constant time complexity. It uses hash for searching. So the algorithm is actually O(n).

```
If we can not use any extra space, then the problem can be solvable in O(n lg n) time.
1. Let the given array be A. Sort the array in O(n lg n)
2. For each element A[i] find SUM-A[i] in A in O(lg n) time using binary search.
3. If binary search in step-2 returns TRUE then return TRUE.
4. Else return FALSE
```

The complexity of this program is O(n). I am not sure if the problem can be done at a lesser complexity

```
int find(int sum, int array[])
{
HashSet<Integer> set = new HashSet<Integer>();
for(int i =0 ;i < array.length;i++)
{
set.add(array[i]);
}
for(int i =0; i< array.length-1;i++)
{
if(set.contains(sum-array[i]))
return 1;
}
return 0;
}
```

This is actually not O(n). It depends on the implementation of set.contains(). If it is linear search, then it will be O(n^2). If it is binary search, it will be O(n lg n)

- Anonymous August 11, 2013