Posted on

Description

Submission

class Solution {
public:
    vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
        int n = nums.size();
        int m = l.size();
        vector<int> diff(n - 1);
        
        
        vector<bool> rets(m, true);
        for(int i = 0; i < m; ++i) {
            int left = l[i];
            int right = r[i];
            int d = right - left;
            
            vector<int> sub{nums.begin() + left, nums.begin() + right + 1};
            
            sort(sub.begin(), sub.end());
            
            int t = INT_MAX;
            for(int j = 1; j <= d; ++j) {
                int diff = sub[j] - sub[j-1];
                
                if(t == INT_MAX) {
                    t = diff;
                } else if(t != diff) {
                    rets[i] = false;
                    break;
                }
            }
        }
        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *