Fundamentals/Algorithms
[해커랭크] Divisible Sum Pairs
글로링러
2023. 12. 4. 22:39
Divisible Sum Pairs | HackerRank
Count the number of pairs in an array having sums that are evenly divisible by a given number.
www.hackerrank.com
정수 배열과 양의 정수 k가 주어질 때, i < j이고 ar[i] + ar[j]가 k로 나누어 떨어지는 쌍의 개수를 결정하세요.
풀이 :
function divisibleSumPairs(n, k, ar) {
let count = 0;
for(let i=0; i<n; i++){
for(let j=i+1; j<n; j++){
if((ar[i] + ar[j])%k === 0){
count ++;
}
}
}
return count
}
갯수를 결과로 반환해야 하므로 count 변수를 생성하고 조건에 맞을 때마다 1씩 올려줍니다.