Divisible Sum Pairs

Divisible Sum Pairs

You are given an array of n integers, ar = [ar[0], ar[1], ..., ar[n - 1], and a positive integer, k. Find and print the number of (i, j) pairs where i < j and ar[i] + ar[j] is divisible by k.

For example, ar = [1, 2, 3, 4, 5, 6] and k = 5. Our three pairs meeting the criteria are [1, 4], [2, 3] and [4, 6].


Function Description

Complete the divisibleSumPairs function in the editor below. It should return the integer count of pairs meeting the criteria.

divisibleSumPairs has the following parameter(s):

  • n: the integer length of array ar
  • ar: an array of integers
  • k: the integer to divide the pair sum by

Input Format

The first line contains 2 space-separated integers, n and k. The second line contains n space-separated integers describing the values of ar[ ar[0], ar[1], …, ar[n-1]].


Constraints


Output Format

Print the number of (i, j) pairs where i < j and a[i] + a[j] is evenly divisible by k.


Sample Input

1
2
6 3
1 3 2 6 1 2

Sample Output

1
5

Explanation

Here are the 5 valid pairs when k = 3:

  • (0, 2) -> ar[0] + ar[2] = 1 + 2 = 3
  • (0, 5) -> ar[0] + ar[5] = 1 + 2 = 3
  • (1, 3) -> ar[1] + ar[3] = 3 + 6 = 9
  • (2, 4) -> ar[2] + ar[4] = 2 + 1 = 3
  • (4, 5) -> ar[4] + ar[5] = 1 + 2 = 3


Solution

1
2
3
4
5
6
7
8
9
10
11
function divisibleSumPairs(n, k, ar) {
return ar.reduce((target, numberm, index) => {
new Array(n - index - 1).fill(0).reduce((innerTarget, fill, innerIndex) => {
((ar[index] + ar[innerIndex + index + 1]) % k === 0) && target++;

return innerTarget;
}, 0);

return target;
}, 0);
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×