Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise.
A good subarray is a subarray where:
k.Note that:
x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.Example 1:
Input: nums = [23,2,4,6,7], k = 6 Output: true Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
Example 2:
Input: nums = [23,2,6,4,7], k = 6 Output: true Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
Example 3:
Input: nums = [23,2,6,4,7], k = 13 Output: false
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1090 <= sum(nums[i]) <= 231 - 11 <= k <= 231 - 1Problem summary: Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise. A good subarray is a subarray where: its length is at least two, and the sum of the elements of the subarray is a multiple of k. Note that: A subarray is a contiguous part of the array. 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[23,2,4,6,7] 6
[23,2,6,4,7] 6
[23,2,6,4,7] 13
subarray-sum-equals-k)minimum-number-of-operations-to-make-array-continuous)intervals-between-identical-elements)apply-operations-to-make-all-array-elements-equal-to-zero)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #523: Continuous Subarray Sum
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> d = new HashMap<>();
d.put(0, -1);
int s = 0;
for (int i = 0; i < nums.length; ++i) {
s = (s + nums[i]) % k;
if (!d.containsKey(s)) {
d.put(s, i);
} else if (i - d.get(s) > 1) {
return true;
}
}
return false;
}
}
// Accepted solution for LeetCode #523: Continuous Subarray Sum
func checkSubarraySum(nums []int, k int) bool {
d := map[int]int{0: -1}
s := 0
for i, x := range nums {
s = (s + x) % k
if _, ok := d[s]; !ok {
d[s] = i
} else if i-d[s] > 1 {
return true
}
}
return false
}
# Accepted solution for LeetCode #523: Continuous Subarray Sum
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
d = {0: -1}
s = 0
for i, x in enumerate(nums):
s = (s + x) % k
if s not in d:
d[s] = i
elif i - d[s] > 1:
return True
return False
// Accepted solution for LeetCode #523: Continuous Subarray Sum
use std::collections::HashMap;
impl Solution {
pub fn check_subarray_sum(nums: Vec<i32>, k: i32) -> bool {
let mut hashmap: HashMap<i32, i32> = HashMap::new();
hashmap.insert(0, -1);
let mut sum = 0;
for (i, &num) in nums.iter().enumerate() {
sum += num;
if let Some(&prev_index) = hashmap.get(&(sum % k)) {
if i as i32 - prev_index >= 2 {
return true;
}
} else {
hashmap.insert(sum % k, i as i32);
}
}
false
}
}
// Accepted solution for LeetCode #523: Continuous Subarray Sum
function checkSubarraySum(nums: number[], k: number): boolean {
const d: Record<number, number> = { 0: -1 };
let s = 0;
for (let i = 0; i < nums.length; ++i) {
s = (s + nums[i]) % k;
if (!d.hasOwnProperty(s)) {
d[s] = i;
} else if (i - d[s] > 1) {
return true;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.