DSA & Coding Copilot
gemini-2.5-flashFeed screenshots from LeetCode, HackerRank, CodeSignal, or CoderPad for instant analysis.
Gemini Generated Response
1. BUG / LOGICAL FLAW
Standard division modulo gives negative remainders on negative inputs in languages like C++/Python, requiring ((sum % k) + k) % k normalization.
2. OPTIMAL APPROACH & COMPLEXITY
Hash map of prefix sum modulo counts. Time Complexity: O(N), Space Complexity: O(K).
3. CORRECT CODEpython
def subarrays_div_by_k(nums: list[int], k: int) -> int:
remainder_count = {0: 1}
curr_sum = 0
total = 0
for x in nums:
curr_sum += x
rem = ((curr_sum % k) + k) % k
if rem in remainder_count:
total += remainder_count[rem]
remainder_count[rem] += 1
else:
remainder_count[rem] = 1
return total