Untitled

mail@pastecode.io avatar
unknown
java
2 months ago
645 B
3
Indexable
Never
class Solution {
    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        List<Boolean> list = new ArrayList<>();
        for (int i = 0; i < l.length; i++) {
            list.add(isArthimetic(nums, l[i], r[i] + 1));
        }
        return list;
    }

    private boolean isArthimetic(int[] nums, int l, int r) {
        int[] arr = Arrays.copyOfRange(nums, l, r);
        Arrays.sort(arr);
        int diff = arr[1] - arr[0];
        for (int j = 1; j < arr.length - 1; j++) {
            if (diff != (arr[j + 1] - arr[j])) {
                return false;
            }
        }
        return true;
    }
}
Leave a Comment