Leetcode Problem 523. Continuous Subarray Sum

523. Continuous Subarray Sum

Leetcode Solutions

Hash Map Approach for Checking Good Subarray

  1. Initialize sum to 0 and an empty hash map with hashMap[0] set to -1 (to handle the case where the subarray starts from index 0).
  2. Iterate over the array nums using index i.
  3. Update sum by adding nums[i] to it.
  4. Calculate remainder as sum % k.
  5. If remainder is not in hashMap, add hashMap[remainder] = i.
  6. If remainder is already in hashMap and i - hashMap[remainder] > 1, return true (subarray size is at least two).
  7. Continue to the next iteration.
  8. If the end of the array is reached without returning true, return false.
UML Thumbnail

Cumulative Sum Approach for Checking Good Subarray

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...