Answer to Question #312494 in HTML/JavaScript Web Application for CHINNI

Question #312494

Magical Indices


Input

The first line of input contains an array

The second line of input contains a number

Output

The output should be a number indicating the number of valid positions

Explanation

For example, an array A = [10, 20, 30] and a value x = 25.

Sample Input 1

[1, 2, 3, 5, 7]

13

Sample Output 1

3

"use strict";

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

  .trim()

  .split("\n")

  .map((str) => str.trim());

 main();

});

function readLine() {

 return inputString[currentLine++];

}

function main() {

 let integers = JSON.parse(readLine());

 let value = JSON.parse(readLine());


 /* Please do not modify anything above this line */


 /* Write your code here and log the output */

}



1
Expert's answer
2022-03-16T14:48:56-0400
"use strict";

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.trim().split("\n").map((str) => str.trim());
    main();
});

function readLine() {
    return inputString[currentLine++];
}

function main() {
    let integers = JSON.parse(readLine());
    let value = JSON.parse(readLine());
    /* Please do not modify anything above this line */
    /* Write your code here and log the output */
    let res = 0;
    let sum = integers.reduce((prev, cur) => prev + cur, 0);

    integers.map(item => {
        item + value >= sum - item ? res++ : '';
    });

    console.log(res);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS