classSolution { public: vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r){ int n = l.size(); vector<bool> ans; for (int i = 0; i < n; ++i) { int left = l[i], right = r[i]; int minv = *min_element(nums.begin() + left, nums.begin() + right + 1); int maxv = *max_element(nums.begin() + left, nums.begin() + right + 1);
publicclassSolution { public IList<bool> CheckArithmeticSubarrays(int[] nums, int[] l, int[] r) { int n = l.Length; IList<bool> ans = new List<bool>(); for (int i = 0; i < n; ++i) { int left = l[i], right = r[i]; int minv = nums[left], maxv = nums[left]; for (int j = left + 1; j <= right; ++j) { minv = Math.Min(minv, nums[j]); maxv = Math.Max(maxv, nums[j]); }
classSolution: defcheckArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]: ans = list() for left, right inzip(l, r): minv = min(nums[left:right+1]) maxv = max(nums[left:right+1]) if minv == maxv: ans.append(True) continue if (maxv - minv) % (right - left) != 0: ans.append(False) continue d = (maxv - minv) // (right - left) flag = True seen = set() for j inrange(left, right + 1): if (nums[j] - minv) % d != 0: flag = False break t = (nums[j] - minv) // d if t in seen: flag = False break seen.add(t) ans.append(flag) return ans
var checkArithmeticSubarrays = function(nums, l, r) { const n = l.length; const ans = []; for (let i = 0; i < n; ++i) { let left = l[i], right = r[i]; let minv = nums[left], maxv = nums[left]; for (let j = left + 1; j <= right; ++j) { minv = Math.min(minv, nums[j]); maxv = Math.max(maxv, nums[j]); }
funccheckArithmeticSubarrays(nums []int, l []int, r []int) []bool { ans := make([]bool, len(l)) for i := range l { ans[i] = isArithmetic(nums[l[i] : r[i]+1]) } return ans }
funcisArithmetic(nums []int)bool { n := len(nums) max, min := nums[0], nums[0] for _, v := range nums { if v > max { max = v } if v < min { min = v } } if max == min { returntrue } d := (max - min) / (n - 1) if (max - min) != d*(n-1) { returnfalse } m := make(map[int]struct{}) for _, v := range nums { m[v] = struct{}{} } for n--; n > 0; n-- { min += d if _, ok := m[min]; !ok { returnfalse } } returntrue }