Continuous Subarray Sum
Real-world version: Billing Window on the Water MeterThe statement here is our own: the same inputs, outputs and technique.
A smart water meter reports the litres drawn on each of a run of consecutive days, given as nums, so nums[i] is the reading for day i. A quiet day reports 0.
The utility settles accounts in windows. A window is a stretch of consecutive days holding two days or more.
Return true when at least one window has a total that is a whole multiple of k, and false when none does. A total of 0 counts as a whole multiple of k.
Examples
Example 1
- Input
- nums = [8, 3, 9], k = 12
- Output
- true
Days 1 and 2 form a window of two days totalling 3 + 9 = 12, which is one whole multiple of 12.
Example 2
- Input
- nums = [9, 2, 5], k = 100
- Output
- false
The windows available total 11, 7 and 16, and none of those is a multiple of 100.
Example 3
- Input
- nums = [7, 4], k = 7
- Output
- false
The only window of two days or more is the whole log, totalling 11, which is not a multiple of 7. Day 0 on its own reads 7 but a single day is too short to be a window.
Constraints
- 1 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^9
- 0 <= sum(nums) <= 2^31 - 1
- 1 <= k <= 2^31 - 1
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def check_subarray_sum(nums: list[int], k: int) -> bool:public boolean checkSubarraySum(int[] nums, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's