I am looking out for efficient approach to the solution of th eproblem that is .. write a program to find all pairs of integers whose sum is equal to a given number. For example if input integer array is {2, 6, 3, 9, 11} and given sum is 9, output should be {6,3}
now what I have tried is that below but i know this not a feasible and efficient solution..
To take one number from array and then loop through array and output pairs which is equal to given sum. You do this for all numbers in first array,
import java.util.Arrays;
/**
* Java Program to find pairs on integer array whose sum is equal to k
*/
public class ProblemInArray{
public static void main(String args[]) {
int[] numbers = { 2, 4, 3, 5, 7, 8, 9 };
int[] numbersWithDuplicates = { 2, 4, 3, 5, 6, -2, 4, 7, 8, 9 };
prettyPrint(numbers, 7);
prettyPrint(numbersWithDuplicates, 7);
}
/**
* Prints all pair of integer values from given array whose sum is is equal to given number.
* complexity of this solution is O(n^2)
*/
public static void printPairs(int[] array, int sum) {
for (int i = 0; i < array.length; i++) {
int first = array[i];
for (int j = i + 1; j < array.length; j++) {
int second = array[j];
if ((first + second) == sum) {
System.out.printf("(%d, %d) %n", first, second);
}
}
}
}
/**
* Utility method to print input and output for better explanation.
*/
public static void prettyPrint(int[] givenArray, int givenSum){
System.out.println("Given array : " + Arrays.toString(givenArray));
System.out.println("Given sum : " + givenSum);
System.out.println("Integer numbers, whose sum is equal to value : " + givenSum);
printPairs(givenArray, givenSum);
}
}
Output:
Given sum : 7
Integer numbers, whose sum is equal to value : 7
(2, 5)
(4, 3)
Given array : [2, 4, 3, 5, 6, -2, 4, 7, 8, 9]
Given sum : 7
Integer numbers, whose sum is equal to value : 7
(2, 5)
(4, 3)
(3, 4)
(-2, 9)