-
Notifications
You must be signed in to change notification settings - Fork 0
/
K_diff_Pairs_in_Array.java
52 lines (36 loc) · 1.1 KB
/
K_diff_Pairs_in_Array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Example: Given an array of distinct integer values,
count the number of pairs of integers that have di erence k.
For example, given the array {1, 7, 5, 9, 2, 12, 3} and the di erence k = 2,there are four pairs with difference2:
(1, 3), (3, 5), (5, 7), (7, 9). */
//Time complexity: Big O(n) where n is the number of elements in the array.
//********Author: Nouru Muneza*******
package sample;
import java.util.*;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
Integer[] input = {1,7,5,9,2,12,3};
Hashtable tempTable = new Hashtable();
List<Integer> list = Arrays.asList(input); //converting an array into an
for(int i = 0; i<input.length; i++)
{
if(list.contains(input[i]-2))
{
System.out.println(input[i] + ","+(input[i]-2)+" ");
}
else if(list.contains(input[i]+2))
{
System.out.println(input[i]+","+(input[i]+2)+" ");
}
}
}
}
/* Output
1,3
7,5
5,3
9,7
3,1
Process finished with exit code 0
*/