In the previous article, we have seen Java Program to Find a Missing Number in an Array
In this article we will see how to find all pairs of elements in an array whose sum is equal to a specified number.
Java Program to Find All Pairs of Elements in an Array Whose Sum is Equal to a Specified Number
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to Find All Pairs of Elements in an Array Whose Sum is Equal to a Specified Number.
- By Using Brute force approach and Static Input
- By Using Brute force approach and User Input
- By Using HashMap
Method-1: Java Program to Find All Pairs of Elements in an Array Whose Sum is Equal to a Specified Number By Using Brute force approach and Static Input
Approach:
- In this method we will use two nested loops, one for traversing the array and another to check if there’s another number in the array which can be added to get the sum.
- During iteration if any pairs is found whose sum is equal to the specified number.
- Then print that pair.
Program:
public class FindPairs { public static void main(String[] args) { int[] arr = new int[] {2, 7, 5, 4, 3, 9, 15}; //Anumber specified int sum = 9; //calling findPairs() user defined method findPairs(arr, sum); } //findPairs() method //it will find all the pairs whose sum is equal to a specified number static void findPairs(int inputArray[], int sum) { System.out.println("The pairs whose sum are equal to "+ sum+" are : "); for (int i = 0; i < inputArray.length; i++) { for (int j = i + 1; j < inputArray.length; j++) { if (inputArray[i] + inputArray[j] == sum) { System.out.println(inputArray[i] + " " + inputArray[j]); } } } } }
Output: The pairs whose sum are equal to 9 are : 2 7 5 4
Method-2: Java Program to Find All Pairs of Elements in an Array Whose Sum is Equal to a Specified Number By Using Brute force approach and User Input
Approach:
- First declare the size of array, then take the input of array elements and a specified number as sum.
- In this method we will use two nested loops, one for traversing the array and another to check if there’s another number in the array which can be added to get the sum.
- During iteration if any pairs is found whose sum is equal to the specified number.
- Then print that pair.
Program:
import java.util.Scanner; public class FindPair { //Driver method public static void main(String[] args) { java.util.Scanner sc = new Scanner(System.in); //Entering the size of array System.out.println("Enter number of elements in the array: "); int n = sc.nextInt(); //array declared int[] arr = new int[n]; //Entering array elements System.out.println("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } //Entering the sum value, a specified number System.out.println("Enter the sum value: "); int sum = sc.nextInt(); //calling the findPairs() user defined method findPairs(arr, sum); } //findPairs() method //it will find all the pairs whose sum is equal to a specified number static void findPairs(int inputArray[], int sum) { System.out.println("The pairs whose sum are equal to "+ sum+" are : "); for (int i = 0; i < inputArray.length; i++) { for (int j = i + 1; j < inputArray.length; j++) { if (inputArray[i] + inputArray[j] == sum) { System.out.println(inputArray[i] + " " + inputArray[j]); } } } } }
Output: Enter number of elements in the array: 9 Enter array elements: 1 5 8 2 9 3 4 6 7 Enter the sum value: 10 The pairs whose sum are equal to 10 are : 1 9 8 2 3 7 4 6
Method-3: Java Program to Find All Pairs of Elements in an Array Whose Sum is Equal to a Specified Number By Using HashMap
Approach:
- In this method we will use a HashMap. Since HashMap allows O(1) time lookup it can be used to reduce the time complexity.
- Initialize the HashMap.
- Iterate over the array.
- Check in the HashMap if (sum – arr[i]) is present as key and its value is 0(Value 0 to avoid getting same pair twice).
- If yes, print the pair and update the value as 1.
- Else, update the HashMap with key as the current element and value as 0.
Program:
import java.util.HashMap; public class FindPairs { public static void main(String[] args) { //Array declared and initialized int[] arr = new int[] {2, 7, 5, 4, 3, 9, 15}; //A number declared as sum int sum = 9; //findPairsHashing() method called findPairsHashing(arr, sum); } //findPairsHashing() method //it will find all the pairs whose sum is equal to a specified number public static void findPairsHashing(int arr[], int sum) { System.out.println("The pairs which are equal to "+ sum+" are : "); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < arr.length; i++) { if (map.containsKey(sum - arr[i]) && map.get(sum - arr[i]) == 0) { System.out.println(arr[i] + " " + (sum - arr[i])); map.replace(sum - arr[i], 1); } else { map.put(arr[i], 0); } } } }
Output: The pairs which are equal to 9 are : 7 2 4 5
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Related Java Programs: