Divisible sum pairs
Klecianny Melo

Klecianny Melo @kecbm

About: 💻 Software Engineer | SWE | ⚙️ JavaScript, .NET, React, Docker, Node.js, CSS | 📝 Author

Location:
Garanhuns, Pernambuco
Joined:
Apr 13, 2022

Divisible sum pairs

Publish Date: May 2 '24
17 8

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

Comments 8 total

  • Anuska Santos
    Anuska SantosMay 2, 2024

    Very interesting this solution, use two loops is 🤯

    • Klecianny Melo
      Klecianny MeloMay 2, 2024

      Thank you my dear! Two loops is the power 😎

  • Rayanny Bezerra
    Rayanny BezerraMay 2, 2024

    Nice algorithm for study

    • Klecianny Melo
      Klecianny MeloMay 2, 2024

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

  • 𒎏Wii 🏳️‍⚧️
    𒎏Wii 🏳️‍⚧️May 3, 2024

    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

  • Tracy Gilmore
    Tracy GilmoreMay 13, 2024

    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
    • Klecianny Melo
      Klecianny MeloJun 6, 2024

      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! 😁

Add comment