-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountDistinctPair.java
More file actions
53 lines (46 loc) · 1.25 KB
/
CountDistinctPair.java
File metadata and controls
53 lines (46 loc) · 1.25 KB
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
51
52
53
/*
Count all distinct pairs with difference equal to k
Given an integer array and a positive integer k, count all distinct pairs with difference equal to k.
Examples:
Input: arr[] = {1, 5, 3, 4, 2}, k = 3
Output: 2
There are 2 pairs with difference 3, the pairs are {1, 4} and {5, 2}
Input: arr[] = {8, 12, 16, 4, 0, 20}, k = 4
Output: 5
There are 5 pairs with difference 4, the pairs are {0, 4}, {4, 8},
{8, 12}, {12, 16} and {16, 20}
*/
//http://www.geeksforgeeks.org/count-pairs-difference-equal-k/
//The method2 in that page is more efficient than this one
import java.util.ArrayList;
import java.util.List;
public class CountDistinctPair {
public void count(int[] arr, int k){
List <Integer> list = new ArrayList<Integer>();
int[] arr2 = arr;
for(int i:arr){
for(int j:arr2){
if(i>j){
if(i-j==k){
list.add(i);
list.add(j);
}
}
}
}
System.out.println("Output Num: "+list.size()/2);
for(int z=0;z<list.size();z++){
System.out.println("{"+list.get(z)+", "+list.get(++z)+"}");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CountDistinctPair cdp = new CountDistinctPair();
int[] arr = {8, 12, 16, 4, 0, 20};
int k = 4;
cdp.count(arr, k);
}
}