Very Easy Solution | c++ | java | python
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nIteration over Subarrays:\n\nThe function iterates over the given ranges 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIteration over Subarrays:\n\nThe function iterates over the given ranges l and r, which represent the starting and ending indices of subarrays to be considered.\nSorting and Arithmetic Check:\n\nFor each subarray, it creates a new vector v containing the elements in the specified range.\nIt sorts the subarray to make it easier to check for arithmetic progression.\n\nIt then checks if the differences between consecutive elements in the sorted subarray are consistent. If they are, the subarray is considered an arithmetic progression.\nResult Storage:\n\nThe boolean result indicating whether each subarray is an arithmetic progression or not is stored in the ans vector.\n\n\n# Complexity\n- Time complexity:\nThe time complexity is O(m * n * log(n)), where m is the number of ranges, n is the length of the input array, and log(n) accounts for the sorting step.\n\n- Space complexity:\nThe overall space complexity is O(m * n), where m is the number of ranges and n is the length of the input array.\n\n# Code\n```c++ []\nclass Solution {\npublic:\n    vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {\n        vector<bool> ans;\n        int m=l.size();\n        for(int i=0;i<m;i++)\n        {\n            vector<int> v= vector<int>(nums.begin()+l[i], nums.begin()+r[i]+1);\n            sort(v.begin(),v.end());\n            bool isarith=true;\n            if(v.size()>1)\n            {\n                int diff=v[1]-v[0];\n                for(int j=1;j<v.size();j++)\n                {\n                    if(v[j]- v[j-1]!= diff)\n                    {\n                        isarith= false;\n                    }\n                }\n            }\n            else \n            {\n                isarith=false;\n            }\n            ans.push_back(isarith);\n        }\n        return ans;\n    }\n};\n```\n```python []\nclass Solution:\n    def checkArithmeticSubarrays(self, nums, l, r):\n        ans = []\n        m = len(l)\n\n        for i in range(m):\n            v = nums[l[i]:r[i] + 1]\n            v.sort()\n            is_arith = True\n            if len(v) > 1:\n                diff = v[1] - v[0]\n                for j in range(1, len(v)):\n                    if v[j] - v[j - 1] != diff:\n                        is_arith = False\n                        break\n            else:\n                is_arith = False\n            ans.append(is_arith)\n\n        return ans\n```\n```java []\nclass Solution {\n    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {\n        List<Boolean> ans = new ArrayList<>();\n        int m = l.length;\n\n        for (int i = 0; i < m; i++) {\n            int[] v = Arrays.copyOfRange(nums, l[i], r[i] + 1);\n            Arrays.sort(v);\n\n            boolean isArith = true;\n            if (v.length > 1) {\n                int diff = v[1] - v[0];\n                for (int j = 1; j < v.length; j++) {\n                    if (v[j] - v[j - 1] != diff) {\n                        isArith = false;\n                        break;\n                    }\n                }\n            } else {\n                isArith = false;\n            }\n            ans.add(isArith);\n        }\n\n        return ans;\n    }\n}\n\n```\n![WhatsApp Image 2023-10-20 at 08.23.29.jpeg](https://assets.leetcode.com/users/images/79d4f323-fbcc-4396-a58c-08308a004ee1_1700726013.3217773.jpeg)\n
['Array', 'Sorting', 'C++', 'Java', 'Python3']