Easy to understand || AP || 3 min read || Beginner Friendly
When you\'re faced with a bunch of numbers and you want to know if they can make an arithmetic sequence, here\'s the drill: sort them.\n\nNow, when you get a qu
When you\'re faced with a bunch of numbers and you want to know if they can make an arithmetic sequence, here\'s the drill: sort them.\n\nNow, when you get a query, it\'s like someone hands you a sub-array, a chunk of numbers in a certain order. Take that chunk, sort it out, and now you\'ve got a sorted sequence. The crucial part? Check if this sorted sequence is an arithmetic sequence. If it is, well, you\'ve cracked the code for that set of numbers.\n\nIt\'s a straightforward process\u2014 Just sort, check, and you\'re good to go.\n\n#### Cpp\n```cpp\nclass Solution {\npublic:\n    bool check(vector<int>& arr){\n        sort(arr.begin(), arr.end());\n        int diff = arr[1] - arr[0];\n        for(int i = 2; i<arr.size(); i++ ){\n            if (arr[i] - arr[i-1] != diff) return false;\n        }\n        return true;\n    }\n    \n    vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {\n        vector<bool> ans;\n        for(int i=0; i<l.size(); i++){\n            vector<int> subarray(nums.begin() + l[i], nums.begin() + r[i] + 1);                     \n            ans.push_back(check(subarray));\n        }\n        return ans;\n    }\n};\n```\n\n#### Java\n```java\nclass Solution {\n    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {\n        List<Boolean> ans = new ArrayList<>();\n\n        for (int i = 0; i < l.length; i++) {\n            int[] subarray = Arrays.copyOfRange(nums, l[i], r[i] + 1);\n            ans.add(check(subarray));\n        }\n\n        return ans;\n    }\n\n    private boolean check(int[] arr) {\n        Arrays.sort(arr);\n        int diff = arr[1] - arr[0];\n        \n        for (int i = 2; i < arr.length; i++) {\n            if (arr[i] - arr[i - 1] != diff) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n}\n```\n\n#### Python\n```python\nclass Solution:\n    def checkArithmeticSubarrays(self, nums, l, r):\n        ans = []\n\n        for i in range(len(l)):\n            subarray = nums[l[i]: r[i] + 1]\n            ans.append(self.check(subarray))\n\n        return ans\n\n    def check(self, arr):\n        arr.sort()\n        diff = arr[1] - arr[0]\n\n        for i in range(2, len(arr)):\n            if arr[i] - arr[i - 1] != diff:\n                return False\n\n        return True\n\n```
['Array', 'Sorting', 'Python', 'C++', 'Java']