Intuition Explained || C++ || Easy To Understand || Sorting
Intution :\n\nIdea is really very simple. Since the contraints are very small i.e value of m, we can use brute force approach. \n\n For every range, we need to 
**Intution :**\n\nIdea is really very simple. Since the contraints are very small i.e value of m, we can use brute force approach. \n\n* For every range, we need to first store the subarray in a temporary vector. Range will be *l[i] to r[i]*\n\n\t\tvector<int> temp(nums.begin() + l[i], nums.begin() + r[i] + 1);\n\n* Then, sort this vector to get a proper increasing seq. so that we can check for common difference d. Also, it should be equal to difference of first two elements.\n\n\t\tsort(temp.begin(), temp.end());          // Sort to get a increasing AP sequence\n\t\tint diff = temp[1] - temp[0];              // Common Difference\n\n* Now for every consecutive element i.e `temp[i] - temp[i-1]` check if their difference is equal to common difference or not. If no, then break and add false to result. Otherwise continue checking.\n\t\n\t\tfor(int j=2; j <= r[i]-l[i]; j++)\n\t\t{\n\t\t\tif(temp[j] - temp[j-1] != diff)\n\t\t\t{\n\t\t\t\tflag = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tres.push_back(!flag);\n\t\t\n* Also, in the beginning we can check if range from l[i] to r[i] is less than 2, then simply add true. Because a sequence of size less that 2 is always a AP. \n\n\tif(r[i] - l[i] < 2) res.push_back(true);\n\n```\nvector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {\n     \n\tvector<bool> res;\n\tfor(int i=0; i<l.size(); i++)                             // O(m)\n\t{\n\t\tif(r[i] - l[i] < 2) res.push_back(true);\n\t\telse\n\t\t{\n\t\t\tvector<int> temp(nums.begin() + l[i], nums.begin() + r[i] + 1);         // O(n) , creating subarray\n\t\t    sort(temp.begin(), temp.end());                                        // O(n^2 * log(n))  , sorting\n\t\t\tbool flag = false;\n\t\t\tint diff = temp[1] - temp[0];\n\t\t\tfor(int j=2; j <= r[i]-l[i]; j++)                                    // O(n) , check if subarray is AP\n\t\t\t{\n\t\t\t\tif(temp[j] - temp[j-1] != diff)\n\t\t\t\t{\n\t\t\t\t\tflag = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tres.push_back(!flag);\n\t\t}\n\t}\n\treturn res;          \n}\n```\n\n**Time : O(m * n * log n)**\n-> **O(m)** to iterate over the range vector,\n-> **O(n + n log n + n) = O(2n + nlogn) ~ O(n log(n))** to form subarray and sort and checking for every range.\n**Space : O(n) -> worst case when r[i] - l[i] = n**\n\n***If you found this post helpful, do consider giving a like, it motivate a lot !! :)***\n
['C', 'Sorting']