Count Subarrays with Sum Divisible by K

Given an array ARR and an integer K, your task is to count all subarrays whose sum is divisible by the given integer K.

Input:

The first line of input contains an integer T representing the number of test cases.
The next 2*T lines represent the T test cases.

For each test case:
- The first line contains two space-separated integers: N (number of elements in the array) and K.
- The second line contains N space-separated integers representing the elements of the array.

Output:

For each test case, output a single integer, the count of all subarrays whose sum is divisible by K.

Example:

Input:
ARR = [5, 0, 2, 3, 1], K = 5
Output:
6
Explanation:

There are 6 subarrays with sums divisible by K.

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ K, N ≤ 104
  • -109 ≤ ARR[i] ≤ 109
  • Time limit: 1 second

Note:

If no subarray exists whose sum is divisible by K, return 0.

Be the first one to answer
Add answer anonymously...
SPRINKLR Full Stack Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter