🎉 One-stop destination for all your technical interview Preparation 🎉
Given an integer array nums and an integer k, return true if nums has a continuous subarray of size at least two whose elements sum up to a multiple of k, or false otherwise.
An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k)
{
int n = nums.size();
unordered_map<int, int> mp;
mp[0] = -1;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += nums[i];
int rem = sum % k;
if (mp.count(rem)) {
if (i - mp[rem] > 1)
return true;
} else {
mp[rem] = i;
}
}
return false;
}
};