This is the Java Program to Find Two Elements whose Sum is Equal to a Given Number.
Given an array of n integers and a value, say m. Find out the pairs of elements from the array, whose sum values m.
Example:
Array = [ 1, 2, 4, 5, 8, 10, 11]
m = 13
Output :
{8, 5}
{11, 2}
Iterate through the array and in a temporary variable, say x, store the difference of the current array element and the value m. Search for value x, in the map, if x is present in the map, then the pair of elements x and the current array element, sums to m, else store x in the map.
Here is the source code of the Java Program to Find Two Elements whose Sum is Equal to a Given Number. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
//Java Program to Find Two Elements whose Sum is Equal to a Given Number.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
public class PairsHavingGivenSum {
// Function to print the pair of elements having a given sum
static void printPairs(int[] array,int sum)
{
HashMap<Integer,Integer> obj=new HashMap<>();
int i,search;
System.out.println("The pairs having sum "+sum+" are");
for(i=0;i<array.length;i++){
search=sum-array[i];
if(obj.containsValue(search)){
System.out.println(array[i]+" and "+search);
}
else
{
obj.put(i,array[i]);
}
}
}
// Main function to read the input
public static void main(String[] args) {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int size;
System.out.println("Enter the size of the array");
try{
size=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println("Invalid Input");
return;
}
int[] array=new int[size];
System.out.println("Enter array elements");
int i;
for(i=0;i<array.length;i++){
try{
array[i]=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println("Invalid element. Enter it again");
i--;
}
}
int sum;
System.out.println("Enter the sum you want to look for");
try{
sum=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println("Invalid Input");
return;
}
printPairs(array,sum);
}
}
1. In function printPairs(), firstly a hashmap is created HashMap
2. The loop for(i=0; i<array.length; i++), sets the value search to sum – array[i] in each iteration.
3. The condition if(obj.containsValue(search)) looks for the variable search in the hashmap.
4. If it is present, then the present element and the element search has the given sum in the array.
5. Otherwise, the value search is added to the hashmap.
Time Complexity: O(n) where n is the number of elements in the array.
Case 1 (Simple Test Case) : Enter the size of the array 7 Enter array elements 1 2 4 5 8 10 11 Enter the sum you want to look for 13 The pairs having sum 13 are 8 and 5 11 and 2 Case 2 (Simple Test Case - another example): Enter the size of the array 10 Enter array elements 12 324 45 34 123 435 23 56 32 47 Enter the sum you want to look for 79 The pairs having sum 79 are 34 and 45 56 and 23 47 and 32
Sanfoundry Global Education & Learning Series – Java Programs.
- Apply for Java Internship
- Practice BCA MCQs
- Check Programming Books
- Practice Programming MCQs
- Practice Information Technology MCQs