DEV Community

Cover image for Divisible sum pairs
Klecianny Melo
Klecianny Melo

Posted on

Divisible sum pairs

Prepare your favorite cup of coffee, because we are about to enter the fantastic world of Divisible sum pairs.

The problem

Image description

Image description

The solution

Image description

To start our solution, let's define the divisibleSumPairs function that will receive the following parameters:

  • n: length of array arr;
  • k: integer divisor;
  • ar: input array.
function divisibleSumPairs(n, k, ar) {}
Enter fullscreen mode Exit fullscreen mode

Next we will initialize the counter variable with the value 0:

let counter = 0;
Enter fullscreen mode Exit fullscreen mode

Now let's go through the ar array from the first element to the penultimate element:

for (let i = 0; i < n - 1; i++) {}
Enter fullscreen mode Exit fullscreen mode

Inside the first loop, we will start a second loop, to cycle through the elements of the ar array from the i + 1 element to the last element:

for (let j = i + 1; j < n; j++) {}
Enter fullscreen mode Exit fullscreen mode

After traversing the array ar with the two loops (integrating through the element i and the element j (i + 1)), we will check if the sum of the elements in the indices i and j of the array arr is divisible by k without remainder:

if ((ar[i] + ar[j]) % k === 0) {}
Enter fullscreen mode Exit fullscreen mode

If this condition is met, we will increase the value of the counter variable:

counter++;
Enter fullscreen mode Exit fullscreen mode

Thus, we will have the number of pairs that satisfy this condition counted in counter.

Finally, let's return the value of counter:

return counter;
Enter fullscreen mode Exit fullscreen mode

Final resolution

Image description

After following the step by step we have our final resolution:

function divisibleSumPairs(n, k, ar) {
    let counter = 0;

    for (let i = 0; i < n - 1; i++) {
      for (let j = i + 1; j < n; j++) {
        if ((ar[i] + ar[j]) % k === 0) {
          counter++;
        }
      }
    }

    return counter;
}
Enter fullscreen mode Exit fullscreen mode

Share the code, spread knowledge and build the future! 😉

Images generated by DALL·E 3

Top comments (8)

Collapse
 
tracygjg profile image
Tracy Gilmore • Edited

Hi Klecianny,
I would like to offer two alternative implementations. Not better (probably worse), just different approaches. There is still a double pass through the array, just not as explicate as in your solution.

Using reduce & filter methods

  function divisibleSumPairs(k, ar) {
    return ar.reduce(
      (acc, ar_i, i) =>
        acc + ar.slice(i + 1).filter(ar_j => !((ar_i + ar_j) % k)).length,
      0
    );
  }
Enter fullscreen mode Exit fullscreen mode

Using recursion with a filter method

  function divisibleSumPairs(k, ar) {
    return _divisibleSumPairs(0, ...ar);

    function _divisibleSumPairs(count, ar_i, ...ar_) {
      return ar_.length
        ? _divisibleSumPairs(
            count + ar_.filter(ar_j => !((ar_i + ar_j) % k)).length,
            ...ar_
          )
        : count;
    }
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kecbm profile image
Klecianny Melo

Hello @tracygjg, how are you? Thank you very much for this solutions for the current problem! Programming is fascinating because we can solve the same problem in different ways. I look forward to your interactions in future articles! 😁

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Prepare your favorite cup of coffee, because we are about to enter the fantastic world of Divisible sum pairs.

No! *prepares cup of tea* >:3

Collapse
 
kecbm profile image
Klecianny Melo

Oh, good! Tea is a nice drink too 🥰

Collapse
 
nik4latic profile image
Anuska Santos

Very interesting this solution, use two loops is 🤯

Collapse
 
kecbm profile image
Klecianny Melo

Thank you my dear! Two loops is the power 😎

Collapse
 
rayanny_bezerra_563386fb7 profile image
Rayanny Bezerra

Nice algorithm for study

Collapse
 
kecbm profile image
Klecianny Melo

It's true Lala, thank you for your contribution! 🥰