Count Pairs Divisible by K

You are given an array ARR and a positive integer K. Your task is to count the total number of pairs within the array whose sum is divisible by K.

Example:

Input:
ARR = [4, 3, 5, 1, 4, 5], K = 5
Output:
3
Explanation:

The pairs whose sums are divisible by 5 are [4, 1], [5, 5], and [4, 1]. Thus, there are 3 pairs.

Constraints:

  • 1 <= T <= 10
  • 2 <= N < 10^6
  • 1 <= K <= 10^9
  • 0 <= arr[i] <= 10^9
  • Time Limit: 1 sec
Note:
You do not need to print anything. It has already been taken care of. Just implement the function.
AnswerBot
4mo

Count pairs in an array whose sum is divisible by a given integer K.

  • Iterate through the array and calculate the remainder of each element when divided by K.

  • Store the remainders in a hashmap along with...read more

Help your peers!
Select
Add answer anonymously...

MathWorks Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Interest in Matlab
A Software Developer Intern was asked Q. Car Pooling Capacity Problem You are a cab driver with a car that initially has ...read more
A Software Developer Intern was asked Q. Distance To Nearest 1 in a Binary Matrix Problem Given a binary matrix MAT conta...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Interest in Matlab
A Software Developer Intern was asked Q2. Car Pooling Capacity Problem You are a cab driver with a car that initially has ...read more
A Software Developer Intern was asked Q3. Distance To Nearest 1 in a Binary Matrix Problem Given a binary matrix MAT conta...read more
MathWorks Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits