Java Program to Print Pairs of Elements that have the Given Difference – Sanfoundry

This is the Java Program to Print Pairs of Elements that have the Given Difference.

Problem Description

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}

Problem Solution

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.

Program/Source Code

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.

  1.  
  2. //Java Program to Print Pairs of Elements that have the Given Difference.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.HashMap;
  8.  
  9. public class PairsHavingGivenDifference {
  10.     //Function to print pair of elements having a given difference.
  11.     static void printPairs(int[] array,int difference){
  12.         HashMap<Integer,Integer> obj=new HashMap<>();
  13.         int i;
  14.         for(i=0;i<array.length;i++){
  15.             obj.put(i,array[i]);
  16.         }
  17.         System.out.println("The pairs of elements having the difference "
  18.                                                       +difference+" are ");
  19.         int search;
  20.         for (i=0;i<array.length;i++){
  21.             search=array[i]+difference;
  22.             if(obj.containsValue(search)){
  23.                 System.out.println(array[i] + " and "+search);
  24.             }
  25.         }
  26.     }
  27.     // Function to read input
  28.     public static void main(String[] args) {
  29.         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  30.         int size;
  31.         System.out.println("Enter the size of the array");
  32.         try{
  33.             size=Integer.parseInt(br.readLine());
  34.         }
  35.         catch(IOException e)
  36.         {
  37.             System.out.println("Invalid Input");
  38.             return;
  39.         }
  40.         int[] array=new int[size];
  41.         System.out.println("Enter array elements");
  42.         int i;
  43.         for(i=0;i<array.length;i++){
  44.             try{
  45.                 array[i]=Integer.parseInt(br.readLine());
  46.             }
  47.             catch(IOException e)
  48.             {
  49.                 System.out.println("Invalid element. Enter it again");
  50.                 i--;
  51.             }
  52.         }
  53.         int difference;
  54.         System.out.println("Enter the difference you want to look for");
  55.         try{
  56.             difference=Integer.parseInt(br.readLine());
  57.         }
  58.         catch(IOException e)
  59.         {
  60.             System.out.println("Invalid Input");
  61.             return;
  62.         }
  63.         printPairs(array,difference);
  64.     }
  65. }

Program Explanation

1. In function printPairs(), firstly a hashmap is created HashMap obj=new 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.

Runtime Test Cases

 
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.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Leave a Comment

Your email address will not be published. Required fields are marked *