There are multiple ways of solving this problem. We will discuss some of them in this post. Let us implement the function which prints the two values such that array[i] + array[j] = K if there is such a pair exists. If not print “No such pair”.
Check each possible pair of values from this array to see if their sum is K. The Java function for this algorithm is given below.
public static void printTwoValues(int[] array, int sum)
{
int i,j;
for( i = 0; i < array.length - 1; i++ )
{
for( j = i+1; j < array.length; j++ )
{
if( array[i] + array[j] == sum )
{
System.out.println("The two values are (" + i +", " + j + ")");
return;
}
}
}
System.out.println("No such pair exists!");
}
This algorithm runs in O(n2) time complexity and O(1) space complexity.
public static void printTwoValues(int[] array, int sum)
{
Arrays.sort(array);
int i = 0;
int j = array.length-1;
while( i < j)
{
if( array[i] + array[j] == sum)
{
System.out.println("The two values are (" + array[i] +", " + array[j] + ")");
return;
}
else if( array[i] + array[j] < sum )
{
i++;
}
else
{
j--;
}
}
System.out.println("No such pair exists!");
}
This algorithm runs in O(n log n) time complexity as we are sorting the array first and O(1) space complexity.
public static void printTwoValues(int[] array, int sum)
{
HashSet<Integer> hSet = new HashSet<Integer>();
int i;
for( i = 0; i < array.length; i++ )
{
if( hSet.contains(sum-array[i]) )
{
System.out.println("The two values are (" + array[i] +", " + (sum-array[i]) + ")");
return;
}
else
hSet.add(array[i]);
}
System.out.println("No such pair exists!");
}
This algorithm takes O(n) time but uses O(n) extra space for the set data structure.
Variations of the problem:
- However if the problem is to find the two indices i,j such that array[i] + array[j] = K, the first and third approaches can easily be modified to solve this problem. But the second approach which uses sorting can not be used because the order of the elements gets changed.
- If the problem is to find all pairs whose sum is K, and the array
may contain duplicates, first approach does not change. But the sorting approach will not work.
We will discuss these problems in the upcoming posts.