Arithmetic Subarray Master: Top 20% Efficiency in C++ with Minimal Memory Usage.
\n# Approach\n\nThe code aims to check whether subarrays within a given \'nums\' array form arithmetic sequences. It does this by iterating through the specifie
\n# Approach\n\nThe code aims to check whether subarrays within a given \'nums\' array form arithmetic sequences. It does this by iterating through the specified subarray ranges denoted by \'l\' and \'r\'. For each subarray:\n\n1. It extracts the subarray and sorts it.\n2. If the subarray has fewer than 3 elements, it\'s considered an arithmetic sequence by default.\n3. If the subarray has 3 elements, it checks if the differences between consecutive elements are the same. If they are, it\'s considered an arithmetic sequence.\n4. For subarrays with more than 3 elements, it calculates the common difference between the first two elements and checks if all differences between consecutive elements match this common difference. If any difference doesn\'t match, it\'s not considered an arithmetic sequence.\n5. The results for each subarray are stored in a boolean vector \'answer\', where each element indicates whether the corresponding subarray is an arithmetic sequence or not. The \'answer\' vector is returned as the final result.\n\n# Complexity\n- Time complexity: O(N * (K * log(K)))\n-  The overall time complexity of the code is O(N * (K * log(K))) , where  N is the number of subarrays specified &K is the size of the subarray.\n\n- Space complexity: O(N + K)\n\n\n# Code\n```\nclass Solution {\npublic:\n    vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {\n        vector<bool>answer;\n       \n       for(int i=0;i<l.size();i++){\n          vector<int> array(nums.begin() + l[i], nums.begin() + r[i] + 1); \n            sort(array.begin(), array.end()); \n\n          if(array.size()<3){\n              answer.push_back(true);\n          }\n\n          else if(array.size()==3){\n              if(array[1]-array[0]==array[2]-array[1]){\n                  answer.push_back(true);\n              }\n              else{\n                  answer.push_back(false);\n              }\n          }\n          else{\n               int value=array[1]-array[0];\n               bool wronganswer=false;\n\n               for(int j=1;j<array.size()-1;j++){\n                  int currvalue=array[j+1]-array[j];\n                   if(currvalue!=value){\n                       wronganswer=true;\n                       break;\n                   }\n                }\n                if(wronganswer){\n                    answer.push_back(false);\n                }\n                else{\n                    answer.push_back(true);\n                }\n              }  \n          }\n     return answer;\n    }   \n};\n```
['C++']