-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffk.java
More file actions
26 lines (21 loc) · 767 Bytes
/
diffk.java
File metadata and controls
26 lines (21 loc) · 767 Bytes
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
/* Given an array ‘A’ of sorted integers and another non negative integer k, find if there exists 2 indices i and j such that A[i] - A[j] = k, i != j.
Example: Input :
A : [1 3 5]
k : 4
Output : YES as 5 - 1 = 4
Return 0 / 1 ( 0 for false, 1 for true ) for this problem
Try doing this in less than linear space complexity.
*/
public int diffPossible(ArrayList<Integer> a, int b) {
int size = a.size();
int i = 0, j = 1;
while (i < size && j < size) {
if (i != j && a.get(j) - a.get(i) == b) {
return 1;
} else if (a.get(j) - a.get(i) < b)
j++;
else
i++;
}
return 0;
}