티스토리 뷰

Algorithms

Divisible Sum Pairs

seoca 2020. 9. 5. 10:22

 

 

 

Solution in JavaScript

const arr = [1, 3, 2, 6, 1, 2];
const k = 3;
const n = arr.length;

function Divisible_Sum_Pairs(n, k, ar) {

    let num = 0;

    for(let i = 0; i < n; i++) {
        //condition 'i < j' 는 i보다 j가 index number로서 우선할 수 없다는 의미지 값이 커야한다는 의미가 아님. 
        //문제 제대로 읽기!
        for(let j = (i + 1); j < n; j++){
            if((ar[i] + ar[j]) % k === 0){ // 계산순서위한 () 잊지말것.
                ++num;
            }
        }
    }

    console.log(num);
}

Divisible_Sum_Pairs(n, k, arr);

 

 

 

 

 

Reference

www.hackerrank.com/challenges/divisible-sum-pairs/problem

'Algorithms' 카테고리의 다른 글

Bon Appétit  (0) 2020.09.12
Birthday Chocolate  (0) 2020.09.11
Breaking the Records  (0) 2020.09.04
Apple and Orange  (0) 2020.08.24
Grading Students  (0) 2020.08.22