amazingsum.js
/*Amazing Sum
You have been given n integer values. Lets say the given values are [latex]a_1[/latex],
[latex]a_2[/latex], [latex]a_3[/latex], [latex]a_4[/latex] ... [latex]a_n[/latex]
If the sum of two consecutive input values is greater than 100, then the given values have
amazing sum
Input Format:
First line denotes n, the number of inputs. The next n lines contains one integer in each line.
Output Format:
One string, either true or false, denoting whether the given values has amazing sum or not.
Example:
Input:
5
20
12
23
41
17
Output:
false
Explanation:
The maximum sum of two consecutive values here is 64 (i.e. 23 + 41), which is not greater
than 100, so the answer is false.
*/
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 noofinputs = parseInt(readLine()); // 5
let firstinput = parseInt(readLine()); //20
let sum = 0;
for (let i = 1; i <= noofinputs - 1; i++) // 1.(i=1<=4 true) 2.(i=2<=4 true) 3.(i=3<=4 true) 4.(i=4<=4 true) 5.(i=5<=4 false exit the loop)
{
let num = parseInt(readLine());// 1.(12) 2.(23) 3.(41) 4.(17)
sum = firstinput + num; // 1.(20+12 = 32) 2.(12+23=35) 3.(23+41=64) 4.(41+17=58)
if (sum > 100) // 1.(32>100 false) 2.(35>100 false) 3.(64>100 false) 4.(58>100 false)
{
console.log("true");
break;
}
firstinput = num; // 1.(12) 2.(23) 3.(41) 4.17
}
if (sum < 100) // 32 , 35 , 64, 58 all are less than 100
{
console.log(false); // therefore false gets printed
}
TERMINAL:

Comments
Post a Comment