Problem 34. train.js

 /*Question

Train
There is an N-car train.
You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back."

Constraints
1 <= i <= N
Input
Two space seperated integers, denoting N,i respectively.

Output
One integer, denoting j.

Example
Input1:

4 2
Output1:

3
Explanation1:

The second car from the front of a 4-car train is the third car from the back.
*/

let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');

function readLine() {
idx++;
return data[idx - 1].trim();
}
//..........................................................................................



let carAndPosition = readLine().split(" "); // ['4', '2']
// console.log(typeof carAndPosition); ['4', '2']

console.log(carAndPosition[0] - carAndPosition[1] + 1); // javascript will convert strings
to number when subtracted

TERMINAL:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node train.js <input.txt 3 Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$


Comments

Popular posts from this blog

Problem 33. patter122333.js

Problem 38. diamond.js

Problem 16. chocolatebills.js