Posted on

Description

Submission

class Solution {
public:
    bool checkSubarraySum(vector<int>& nums, int k) {
        unordered_map<int, int> remainderPos{{0, -1}};

        int rem = 0;

        for(int i = 0; i < nums.size(); ++i) {
            rem = (rem + nums[i]) % k;

            if(remainderPos.find(rem) != remainderPos.end()) {
                if(i - remainderPos[rem] >= 2) {
                    return true;
                }
            } else {
                remainderPos[rem] = i;
            }
        }

        return false;
    }
};

Leave a Reply

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