This is the Java Program to Print Pairs of Elements that have the Given Difference.
Given an array of n integers and a value, say m. Find out the pairs of elements from the array, whose difference is value m.
Example:
Array = [ 1, 2, 4, 5, 8, 10, 11]
m = 1
Output :
{1, 2}
{4, 5}
{10, 11}
Iterate through the array and store all of its elements in a map. Now, re-iterate through the array and in a temporary variable, say x, store the sum of the current array element and the difference m. Search for value x, in the map, if x is present in the map, then the pairs of elements x and the current array element exhibits the difference m.
Here is the source code of the Java Program to Print Pairs of Elements that have the Given Difference. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
//Java Program to Print Pairs of Elements that have the Given Difference.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class PairsHavingGivenDifference {
//Function to print pair of elements having a given difference.
static void printPairs(int[] array,int difference){
HashMap<Integer,Integer> obj=new HashMap<>();
int i;
for(i=0;i<array.length;i++){
obj.put(i,array[i]);
}
System.out.println("The pairs of elements having the difference "
+difference+" are ");
int search;
for (i=0;i<array.length;i++){
search=array[i]+difference;
if(obj.containsValue(search)){
System.out.println(array[i] + " and "+search);
}
}
}
// Function to read 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(IOException 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(IOException e)
{
System.out.println("Invalid element. Enter it again");
i--;
}
}
int difference;
System.out.println("Enter the difference you want to look for");
try{
difference=Integer.parseInt(br.readLine());
}
catch(IOException e)
{
System.out.println("Invalid Input");
return;
}
printPairs(array,difference);
}
}
1. In function printPairs(), firstly a hashmap is created HashMap
2. The loop for(i=0; i<array.length; i++) is used to put all the array elements with their indexes, into the map.
3. The second loop for(i=0; i<array.length; i++), sets the value search to array[i] + difference in each iteration.
4. The condition if(obj.containsValue(search)) looks for the variable search in the hashmap.
5. If it is present, then the present element and the element search has the given difference in the array.
Time Complexity: O(n) where n is the number of elements in the array.
Case 1 (Simple Test Case - Positive Difference): Enter the size of the array 10 Enter array elements 123 12 234 24 0 786 798 234 123 213 Enter the difference you want to look for 12 The pairs of elements having the difference 12 are 12 and 24 0 and 12 786 and 798 Case 2 (Simple Test Case - Negative difference): Enter the size of the array 8 Enter array elements 8 7 6 5 4 3 2 1 Enter the difference you want to look for -1 The pairs of elements having the difference -1 are 8 and 7 7 and 6 6 and 5 5 and 4 4 and 3 3 and 2 2 and 1
Sanfoundry Global Education & Learning Series – Java Programs.
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Programming Books
- Practice Information Technology MCQs