3
vector<bool> checkArithmeticSubarrays(vector<int> &nums, vector<int> &l, vector<int> &r) {4
vector<bool> ans(l.size(), false);5
for (int i = 0; i < l.size(); i++) {6
// if the sub array size is equal to 27
if (r[i] - l[i] + 1 == 2)9
else if (isArthemetic(nums, l[i], r[i]))15
// return true if the sub array can be rearranged in a arthemetic sequence16
bool isArthemetic(vector<int> &nums, int start, int end) {17
// get the maximum and min elements20
for (int i = start; i <= end; i++) {21
mini = min(nums[i], mini);22
maxi = max(nums[i], maxi);25
// when mini==maxi it means all the elements are same in the sub array26
if (mini == maxi) return true;28
// we cant have same common difference betweeen two adjacent elements29
// when we arrange in arthemetic sequence30
if ((maxi - mini) % (end - start) != 0) return false;32
// the diff between every two integers when we rearrange sub array33
int diff = (maxi - mini) / (end - start);35
// to check if the duplicate elemnts are present37
// 6 is repeating two times38
vector<bool> present(end - start + 1, false);39
for (int i = start; i <= end; i++) {40
// we cant set a index of nums[i]41
if ((nums[i] - mini) % diff != 0) return false;43
int ind = (nums[i] - mini) / diff;45
// same element is alreeady repeated ( 6 in the above example)46
if (present[ind]) return false;