Created
April 17, 2022 07:08
-
-
Save MohamedGamil/08a672976bdebfe1b25e9c3ee56146b6 to your computer and use it in GitHub Desktop.
HackerRank / Number Line Jumps
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'; | |
const fs = require('fs'); | |
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 'kangaroo' function below. | |
* | |
* The function is expected to return a STRING. | |
* The function accepts following parameters: | |
* 1. INTEGER x1 | |
* 2. INTEGER v1 | |
* 3. INTEGER x2 | |
* 4. INTEGER v2 | |
*/ | |
function kangaroo(x1, v1, x2, v2) { | |
if (v1 <= v2) { | |
return 'NO'; | |
} | |
while(x1 <= x2) { | |
x1 += v1; | |
x2 += v2; | |
if (x1 === x2) { | |
return 'YES'; | |
} | |
} | |
return 'NO'; | |
} | |
function main() { | |
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | |
const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' '); | |
const x1 = parseInt(firstMultipleInput[0], 10); | |
const v1 = parseInt(firstMultipleInput[1], 10); | |
const x2 = parseInt(firstMultipleInput[2], 10); | |
const v2 = parseInt(firstMultipleInput[3], 10); | |
const result = kangaroo(x1, v1, x2, v2); | |
ws.write(result + '\n'); | |
ws.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment