A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is arithmetic if and only if s[i+1] - s[i] == s[1] - s[0] for all valid i.

For example, these are arithmetic sequences:

1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9

The following sequence is not arithmetic:

1, 1, 2, 5, 7

You are given an array of n integers, nums, and two arrays of m integers each, l and r, representing the m range queries, where the ith query is the range [l[i], r[i]]. All the arrays are 0-indexed.

Return a list of boolean elements answer, where answer[i] is true if the subarray nums[l[i]], nums[l[i]+1], ... , nums[r[i]] can be rearranged to form an arithmetic sequence, and false otherwise.

&nbsp;
Example 1:

Input: nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5]
Output: [true,false,true]
Explanation:
In the 0th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
In the 1st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
In the 2nd query, the subarray is [5,9,3,7]. This can be rearranged as [3,5,7,9], which is an arithmetic sequence.

Example 2:

Input: nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10], l = [0,1,6,4,8,7], r = [4,4,9,7,9,10]
Output: [false,true,false,false,true,true]


&nbsp;
Constraints:


	n == nums.length
	m == l.length
	m == r.length
	2 &lt;= n &lt;= 500
	1 &lt;= m &lt;= 500
	0 &lt;= l[i] &lt; r[i] &lt; n
	-105 &lt;= nums[i] &lt;= 105


class Solution:
    def checkArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]:
        out=[]
        for i, j in zip(l, r):
            out.append(self.canMakeArithmeticProgression(nums[i:j+1]))
        return out
        
    def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
        minArr = min(arr)
        maxArr = max(arr)
		
		# if difference between minArr and maxArr cannot be divided into equal differences, then return false
        if (maxArr-minArr)%(len(arr)-1)!=0:
            return False
			
		# consecutive difference in arithmetic progression
        diff = int((maxArr-minArr)/(len(arr)-1))
        if diff == 0:
            if arr != [arr[0]]*len(arr):
                return False
            return True
		
		# array to check all numbers in A.P. are present in input array.
		# A.P.[minArr, minArr+d, minArr+2d, . . . . . . . maxArr]
        check = [1]*len(arr)
        for num in arr:
            if (num-minArr)%diff != 0:
                return False
            check[(num-minArr)//diff]=0
		
		# if 1 is still in check array it means at least one number from A.P. is missing from input array.
        if 1 in check:
            return False
        return True
class Solution {
    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        List<Boolean> result = new ArrayList<>();
        int L = nums.length, ll = l.length,ind=0;
        for(int i=0;i<ll;i++){
            int[] arr = new int[r[i]-l[i]+1];
            ind=0;
            for(int k=l[i];k<=r[i];k++){
                arr[ind] =nums[k];
                ind++;
            }
            Arrays.sort(arr);
            result.add(isArithmetic(arr));
        }
        return result;
    }
    
    public boolean isArithmetic(int[] nums) {
        int L = nums.length;
        boolean b = true;
        if(L<=2) return true;
        for(int i=1;i<L-1;i++){
            if(nums[i]-nums[i-1]!=nums[i+1]-nums[i]) return false;
        }
        return b;
    }
}
class Solution {
public:
    vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
        
        vector<bool> ans(l.size(),false);
        for(int i=0;i<l.size();i++)
        {
            //if the sub array size is equal to 2
            if(r[i]-l[i]+1==2)
                ans[i]=true;
            else if(isArthemetic(nums,l[i],r[i]))
                ans[i]=true;
        }
        return ans;
    }
    
    //return true if the sub array can be rearranged in a arthemetic sequence
    bool isArthemetic(vector<int>& nums,int start,int end)
    {
        //get the maximum and min elements 
        int mini=INT_MAX;
        int maxi=INT_MIN;
        for(int i=start;i<=end;i++)
        {
            mini=min(nums[i],mini);
            maxi=max(nums[i],maxi);
        }
        
        //when mini==maxi it means all the elements are same in the sub array
        if(mini==maxi)
            return true; 
        
        //we cant have same common difference betweeen  two adjacent  elements
        //when we arrange in arthemetic sequence
        if((maxi-mini)%(end-start)!=0)
            return false;
        
        
        //the diff between every two integers when we rearrange sub array
        int diff=(maxi-mini)/(end-start);
        
        //to check  if the duplicate elemnts are present
        //ex- [2,4,6,6]
        //6 is repeating two times 
        vector<bool> present(end-start+1,false);
        for(int i=start;i<=end;i++)
        {
            
            //we cant set a index of nums[i]
            if((nums[i]-mini)%diff!=0)
                return false;
            
            int ind=(nums[i]-mini)/diff;
            
            // same element is alreeady repeated ( 6 in the above  example)
            if(present[ind])
                return false;
            //mark it presence
            present[ind]=true;
            
        }
        return true;
        
    }
};
/**
 * @param {number[]} nums
 * @param {number[]} l
 * @param {number[]} r
 * @return {boolean[]}
 */
 var checkArithmeticSubarrays = function(nums, l, r) {
    let result = [];
    for(let i=0;i<l.length;i++) {
        let subNums = [...nums].slice(l[i], r[i]+1);
        subNums = subNums.sort((a,b) => a-b);
        let diff = subNums[1]-subNums[0];
        let s = true
        for(let j=0;j<subNums.length-1;j++) {
            if(!(subNums[j+1]-subNums[j] == diff))
                s = false
        }
        result.push(s)
    }
    return result
};
Arithmetic Subarrays