Skip to content
Onsite

Longest Subarrays With Sum Not Divisible by K

MediumP3Coding

Given an integer array nums and a positive integer k, look at all contiguous subarrays whose sum is not divisible by k. Among those, find the greatest length L, and return how many subarrays of length L have a sum that is not divisible by k.

If no subarray has a sum that is not divisible by k, return 0.

Example 1

Input
nums = [1,2,3], k = 4
Output
1

Example 2

Input
nums = [2,4,6], k = 2
Output
0

Example 3

Input
nums = [3,6,9,2], k = 3
Output
1

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= 10^9

Hints

Run checks the 3 visible tests, Submit all 12.