Define the subarray sum as: $$ sum(i, j) = prefix[j] - prefix[i]; $$
First let’s see the condition that should return true. $$ prefix[j] - prefix[i] = n * k \\ \Rightarrow (prefix[j] - prefix[i]) \% k = 0 \ $$ According to the math equation: $$ (a + b) \% k = (a \%k + b \% k) \% k $$ Since $ a = prefix[j], b = -prefix[i]$ . And $prefix[j] - prefix[i] > 0$ is always true. So the the condition becomes: $$ (prefix[j] \%k - prefix[i] \%k) \% k = 0 \\ \Rightarrow prefix[j] \%k = prefix[i] \% k $$ So the final condition would be $prefix[i] \% k = prefix[j] \% k$; All we need to do is to find a previous prefix[i] % k that equals the prefix[j] % k.
Since the problem requires the subarray length ≥ 2, we store the first occurrence index of each remainder in a HashMap, and whenever we find the same remainder again, we check if the distance between indices is at least 2.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: boolcheckSubarraySum(vector<int>& nums, int k){ int sum = 0; map<int,int> mp; mp[0] = -1; for(int i = 0; i < nums.size(); i ++) { sum = (sum + nums[i]) % k; // cout << i << ": " << sum << endl; if(mp.find(sum) != mp.end()) { if(i - mp[sum] > 1) returntrue; } else { mp[sum] = i; } } returnfalse; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassSolution { publicboolCheckSubarraySum(int[] nums, int k) { Dictionary<int,int> mp = new Dictionary<int ,int >(); int sum = 0; mp[0] = -1; for(int i = 0; i < nums.Length; i ++) { sum = (sum + nums[i]) % k; if(mp.ContainsKey(sum)) { if(i - mp[sum] > 1) { returntrue; } } else { mp[sum] = i; } } returnfalse; } }