Last active
April 17, 2022 07:07
-
-
Save MohamedGamil/22bfd107f3372022e0c569b3295a1606 to your computer and use it in GitHub Desktop.
HackerRank / Apple and Orange
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
'use strict'; | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', function(inputStdin) { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', function() { | |
inputString = inputString.split('\n'); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
/* | |
* Complete the 'countApplesAndOranges' function below. | |
* | |
* The function accepts following parameters: | |
* 1. INTEGER s | |
* 2. INTEGER t | |
* 3. INTEGER a | |
* 4. INTEGER b | |
* 5. INTEGER_ARRAY apples | |
* 6. INTEGER_ARRAY oranges | |
*/ | |
function countApplesAndOranges(s, t, a, b, apples, oranges) { | |
const sum = (tree) => (memo, value) => { | |
const distance = tree + value; | |
if (s <= distance && t >= distance) { | |
memo++; | |
} | |
return memo; | |
}; | |
const countApples = apples.reduce(sum(a), 0); | |
const countOranges = oranges.reduce(sum(b), 0); | |
console.log(countApples); | |
console.log(countOranges); | |
} | |
function main() { | |
const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' '); | |
const s = parseInt(firstMultipleInput[0], 10); | |
const t = parseInt(firstMultipleInput[1], 10); | |
const secondMultipleInput = readLine().replace(/\s+$/g, '').split(' '); | |
const a = parseInt(secondMultipleInput[0], 10); | |
const b = parseInt(secondMultipleInput[1], 10); | |
const thirdMultipleInput = readLine().replace(/\s+$/g, '').split(' '); | |
const m = parseInt(thirdMultipleInput[0], 10); | |
const n = parseInt(thirdMultipleInput[1], 10); | |
const apples = readLine().replace(/\s+$/g, '').split(' ').map(applesTemp => parseInt(applesTemp, 10)); | |
const oranges = readLine().replace(/\s+$/g, '').split(' ').map(orangesTemp => parseInt(orangesTemp, 10)); | |
countApplesAndOranges(s, t, a, b, apples, oranges); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment