Count Pairs Divisible by K
Updated
Problem statement
Given an integer array nums and positive integer k, count index pairs (i,j) with i < j for which nums[i] + nums[j] is divisible by k.
Different index pairs count separately, including pairs with equal values. An element cannot pair with itself. Return the count as a signed 64-bit integer.
Function
countDivisiblePairs(nums: int[], k: int) → longExamples
Example 1
nums = [4,3,5,1,4,5]k = 5return = 3The two separate occurrences of four each pair with one, and the two fives form one pair.
Example 2
nums = [-1,1,2,-2]k = 3return = 4The valid sums are zero, negative three, positive three, and zero.
Constraints
0 <= nums.length <= 10^5.-10^9 <= nums[i] <= 10^9.1 <= k <= 10^9.
More Apple Onsite Interview questions
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
- Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
- Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
- Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
- Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
- BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
- Product Total With the Cheapest PromotionInstacart · ONSITE INTERVIEWInstacart interview questions
- Equation Tuple CombinationZoox · ONSITE INTERVIEW
- Unique Near-Equal Target-Sum PairsFivetran · ONSITE INTERVIEWFivetran interview questions
- Minimum Unique Card Suffix LengthBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Digit-Reversed Pairs in an ArrayExpedia · ONSITE INTERVIEWExpedia interview questions
- Max Points on a LineLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Majority ElementMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Count Index Pairs with Equal Value OffsetGoogle · ONSITE INTERVIEWGoogle interview questions
- Net Position Calculation from API SnapshotsHeadlands Technologies · ONSITE INTERVIEWHeadlands Technologies interview questions