Practice
k. What is the return value when called with nums = [2, 4, 1, 3] and k = 5?
def canArrange(nums, k):
if len(nums) % 2 != 0:
return False
freq = {}
for num in nums:
r = ((num % k) + k) % k
freq[r] = freq.get(r, 0) + 1
if freq.get(0, 0) % 2 != 0:
return False
for r in freq:
if r != 0:
complement = (k - r) % k
if freq.get(r, 0) != freq.get(complement, 0):
return False
if k % 2 == 0 and freq.get(k // 2, 0) % 2 != 0:
return False
return True
Solution
Step 1: Compute remainders and their frequencies
nums = [2,4,1,3], k=5 Remainders: 2%5=2, 4%5=4, 1%5=1, 3%5=3 freq = {2:1,4:1,1:1,3:1}Step 2: Check pairing conditions
freq[0] = 0 (even), Check pairs: - remainder 1 pairs with 4 -> freq[1]=1, freq[4]=1 (equal) - remainder 2 pairs with 3 -> freq[2]=1, freq[3]=1 (equal) k is odd, so no special k/2 check needed All conditions satisfied -> return trueFinal Answer:
Option D -> Option DQuick Check:
All remainder pairs match frequencies [OK]
- Forgetting to handle negative numbers modulo
- Not checking freq[0] is even
- Assuming order of freq iteration affects result
def subarraySum(nums, k):
count = 0
running_sum = 0
prefix_counts = {0: 1}
for num in nums:
running_sum += num
if running_sum - k in prefix_counts:
count += prefix_counts[running_sum - k]
prefix_counts[running_sum] = prefix_counts.get(running_sum, 0) + 1
return count
print(subarraySum([1, 2, 3], 3))Solution
Step 1: Trace running_sum and prefix_counts
Iteration 1: running_sum=1, prefix_counts={0:1,1:1}, count=0 Iteration 2: running_sum=3, running_sum-k=0 in prefix_counts, count=1, prefix_counts={0:1,1:1,3:1} Iteration 3: running_sum=6, running_sum-k=3 in prefix_counts, count=2, prefix_counts={0:1,1:1,3:1,6:1}Step 2: Return count
Count is 2, representing subarrays [1,2] and [3].Final Answer:
Option D -> Option DQuick Check:
Count matches expected subarrays summing to 3. [OK]
- Off-by-one errors in counting
- Forgetting to initialize prefix_counts with {0:1}
- Miscounting subarrays ending at current index
k for the problem of checking if array pairs sum to multiples of k?Solution
Step 1: Analyze iteration over input array
The algorithm iterates once over the array of size n to compute remainder frequencies, which is O(n).Step 2: Analyze iteration over remainder keys
It then iterates over at most k remainder keys to verify pairing conditions, which is O(k).Final Answer:
Option C -> Option CQuick Check:
Overall complexity is O(n + k), simplified to O(n) when n >> k [OK]
- Confusing nested loops with frequency map iteration
- Assuming O(n*k) because of remainder checks
- Ignoring that k is usually much smaller than n
Solution
Step 1: Understand the edge case when target == 0
If target == 0, it means totalSum == x, so removing all elements is needed, so minimum operations = length of array.Step 2: Identify incorrect return value
The code returns -1 incorrectly, which signals no solution, but the correct answer is to remove all elements.Final Answer:
Option A -> Option AQuick Check:
Returning -1 here causes wrong output on full removal case [OK]
- Returning -1 instead of full length when target == 0
- Misinterpreting no solution vs full removal
- Ignoring edge cases in initial checks
Solution
Step 1: Identify main operations
The algorithm iterates over the array once, updating running_sum and hash map counts.Step 2: Analyze hash map operations
Hash map insertions and lookups are O(1) on average, so total time is O(n).Final Answer:
Option A -> Option AQuick Check:
No nested loops or sorting involved, so O(n) time complexity. [OK]
- Confusing with brute force O(n²)
- Assuming hash map operations are O(log n)
- Counting prefix sum storage as extra time
