-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPair.java
38 lines (28 loc) · 792 Bytes
/
Pair.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
package com.program.dynamic.programming;
/**
* Created by rrohit on 13/10/16.
*/
public class Pair {
public static int pairCount(int arr[], int target) {
int pairCount = 0;
if (arr.length == 0 || arr.length == 1) {
return pairCount;
}
int start = 0, end = arr.length-1;
int currentSum = 0;
while (start < end) {
currentSum = arr[start] + arr[end];
if (currentSum >= target) {
pairCount += end - start;
end--;
} else {
start++;
}
}
return pairCount;
}
public static void main(String args[]) {
int arr[] = {2, 3, 4, 10, 12, 30, 45};
System.out.println(pairCount(arr, 40));
}
}