-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindIndicesWithIndexAndValueDifferenceII.java
More file actions
47 lines (37 loc) · 1.41 KB
/
FindIndicesWithIndexAndValueDifferenceII.java
File metadata and controls
47 lines (37 loc) · 1.41 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
import java.util.Arrays;
public class FindIndicesWithIndexAndValueDifferenceII {
public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
if (nums.length <= 1) {
if (0 >= valueDifference) {
if (indexDifference == 0) {
return new int[]{0, 0};
} else return new int[]{-1, -1};
}
}
if (nums.length - indexDifference < 0)
return new int[]{-1, -1};
int minIndex = 0;
int maxIndex = 0;
for (int i = 0; i < nums.length - indexDifference; i++) {
int j = i + indexDifference;
if (nums[i] <= nums[minIndex]) {
minIndex = i;
}
if (nums[i] >= nums[maxIndex]) {
maxIndex = i;
}
if (Math.abs(nums[maxIndex] - nums[j]) >= valueDifference) {
return new int[]{maxIndex, j};
}
if (Math.abs(nums[minIndex] - nums[j]) >= valueDifference){
return new int[]{minIndex, j};
}
}
return new int[]{-1, -1};
}
public static void main(String[] args) {
FindIndicesWithIndexAndValueDifferenceII valueDifference = new FindIndicesWithIndexAndValueDifferenceII();
int[] nums = new int[]{1,2,3};
System.out.println(Arrays.toString(valueDifference.findIndices(nums, 2, 4)));
}
}