Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daubattu/dde5c99dc947fb99e798798b24c50de2 to your computer and use it in GitHub Desktop.
Save daubattu/dde5c99dc947fb99e798798b24c50de2 to your computer and use it in GitHub Desktop.
[Hackerrank] Solution of Divisible Sum Pairs in JavaScript
function divisibleSumPairs(n, k, ar) {
let result = 0;
for(let i = 0; i < n - 1; i++) {
result += ar.slice(i + 1, n).filter((item, index) => {
if ((item + ar[i]) % k === 0) {
return item;
}
}).length;
}
return result;
}
@zabuza1997
Copy link

zabuza1997 commented May 26, 2021

This is my solution
function divisibleSumPairs(n, k, ar) { // Write your code here var counter=0; for (let i=0; i<n; i++) { for (let j=n-1; j>i; j--) { if ((ar[i]+ar[j])%k===0) { counter++ } } } return counter; }

@Kennedy-Njeri
Copy link

function divisibleSumPairs(n, k, ar) {
// Write your code here

let counter = 0

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

return counter

}

@ZeeshanAli-0704
Copy link

ZeeshanAli-0704 commented Aug 3, 2021

// no need for n inside ar.slice
// To avoid confusion refer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

function divisibleSumPairs(n, k, ar) {
    let counter = 0;
    for (let i = 0; i < n; i++) {
        ar.slice(i + 1).filter((item) => {
            if ((item + ar[i]) % k === 0) {
                counter++
            }
        })
    }
    return counter;
}

@hamko34
Copy link

hamko34 commented Jan 18, 2022

instead of .filter(), you can use regular .forEach() since we don't need the return value of .filter()

@dmitrisanzharov
Copy link

function divisibleSumPairs(n, k, ar) {
let tempArr = [];
let finalArr = [];

for (let i = 0; i < ar.length; i++) {
	tempArr.push(ar.slice(i, ar.length));
}

tempArr.forEach((el) => {
	for (let i = 1; i < el.length; i++) {
		if ((el[0] + el[i]) % k === 0) {
			finalArr.push([el[0], el[i]]);
		}
	}
});

return finalArr.length;

// end of function

}

@Mousamia
Copy link

Mousamia commented Feb 3, 2023

function divisibleSumPairs(n, k, ar) { // Write your code here

let counter = 0

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

return counter

}
this does not work for single element array

@Mousamia
Copy link

Mousamia commented Feb 3, 2023

for(let i = 0; i < n; i++) {
for(let j = i + 1; j < n; j++ ) {
let sum = ar[i] + ar[j]
if(sum % k === 0) {
counter++
}
}
this does not work for single element ... the following code will work

function divisibleSumPairs(n, k, ar) {
// Write your code here
var sum = 0;
var pair = 0;

// for single element array where the element is divisible by k
if(n==1 && (ar[0]%k == 0)){
   pair = 1;
}
else if (n > 1){
    for(i = 0; i<n; i++){
        // for adding the values
        for(j = i+1; j<n; j++){
            sum = ar[i] + ar[j];
            if(sum % k == 0){
                pair++;
                // console.log("I am pair", pair);
            }
        }
    }
    
}
    
else {
    pair = 0;
}

return pair;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment