We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0a06058 commit 156aad2Copy full SHA for 156aad2
Medium/Arithmetic Subarrays.java
@@ -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