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;
}
@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;

}

@bhupati0001
Copy link

`function divisibleSumPairs(n, k, ar) {
let count = 0;
let remainderCounts = new Array(k).fill(0);

// Count the occurrences of each remainder
for (let i = 0; i < n; i++) {
    let remainder = ar[i] % k;
    if (remainder < 0) remainder += k; // Handle negative remainders
    remainderCounts[remainder]++;
}

// Count pairs where the sum of remainders is divisible by k
for (let r = 0; r <= k / 2; r++) {
    if (r === 0 || (2 * r === k)) {
        // Special case for remainder 0 or k/2 (when k is even)
        count += (remainderCounts[r] * (remainderCounts[r] - 1)) / 2;
    } else {
        count += remainderCounts[r] * remainderCounts[k - r];
    }
}

return count;

}
`

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