Skip to content

Commit 156aad2

Browse files
committed
Added Arithmetic Subarrays.java
1 parent 0a06058 commit 156aad2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Medium/Arithmetic Subarrays.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
3+
List<Boolean> queryResult = new ArrayList<>();
4+
for (int idx = 0; idx < l.length; idx++) {
5+
queryResult.add(isArithmeticProgressionPossible(nums, l[idx], r[idx]));
6+
}
7+
return queryResult;
8+
}
9+
10+
private boolean isArithmeticProgressionPossible(int[] nums, int startIdx, int endIdx) {
11+
List<Integer> sequence = new ArrayList<>();
12+
for (int idx = startIdx; idx <= endIdx; idx++) {
13+
sequence.add(nums[idx]);
14+
}
15+
Collections.sort(sequence);
16+
Integer diff = sequence.get(1) - sequence.get(0);
17+
for (int idx = 2; idx < sequence.size(); idx++) {
18+
if (diff != (sequence.get(idx) - sequence.get(idx - 1))) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)