Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Create Divisible Sum Pairs.js
- Loading branch information
1 parent
b22a2e9
commit 539aec5
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
|
||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf-8'); | ||
|
||
let inputString = ''; | ||
let currentLine = 0; | ||
|
||
process.stdin.on('data', inputStdin => { | ||
inputString += inputStdin; | ||
}); | ||
|
||
process.stdin.on('end', _ => { | ||
inputString = inputString.replace(/\s*$/, '') | ||
.split('\n') | ||
.map(str => str.replace(/\s*$/, '')); | ||
|
||
main(); | ||
}); | ||
|
||
function readLine() { | ||
return inputString[currentLine++]; | ||
} | ||
|
||
// Complete the divisibleSumPairs function below. | ||
function divisibleSumPairs(n, k, ar) { | ||
|
||
var count = 0; | ||
for(var i = 0; i < n; i++) { | ||
for(var j = i+1; j < n; j++) { | ||
if((ar[i] + ar[j]) % k === 0) { | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
function main() { | ||
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | ||
|
||
const nk = readLine().split(' '); | ||
|
||
const n = parseInt(nk[0], 10); | ||
|
||
const k = parseInt(nk[1], 10); | ||
|
||
const ar = readLine().split(' ').map(arTemp => parseInt(arTemp, 10)); | ||
|
||
let result = divisibleSumPairs(n, k, ar); | ||
|
||
ws.write(result + "\n"); | ||
|
||
ws.end(); | ||
} |