All problems
0551MediumArrayHash TableMathPrefix SumPigeonhole Principle

Billing Window on the Water Meter

Tracked in this browser only
Write code

Trains the technique from

LeetCode 523Continuous Subarray Sum

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def check_subarray_sum(nums: list[int], k: int) -> bool:
Java
public boolean checkSubarraySum(int[] nums, int k)
September 7
Apply