Skip to content

Commit be90aec

Browse files
authored
Refactored Arithmetic Slices.java
1 parent 8e9211e commit be90aec

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

Medium/Arithmetic Slices.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
class Solution {
22
public int numberOfArithmeticSlices(int[] A) {
3-
int count = 0;
4-
for (int i = 0; i < A.length - 2; i++) {
3+
int counter = 0;
4+
int n = A.length;
5+
for (int i = 0; i < n - 2; i++) {
56
int diff = A[i + 1] - A[i];
6-
for (int j = i + 2; j < A.length; j++) {
7-
if (A[j] - A[j - 1] == diff) {
8-
count++;
9-
}
10-
else {
11-
break;
12-
}
7+
int nextIdx = i + 2;
8+
while (nextIdx < n && A[nextIdx] - A[nextIdx - 1] == diff) {
9+
nextIdx++;
1310
}
11+
counter += nextIdx - i - 2;
1412
}
15-
return count;
13+
return counter;
1614
}
1715
}

0 commit comments

Comments
 (0)