Magical Indices
Given an array of integers and a number
Write a JS program to determine the number of valid indices.
For example, an array A = [10, 20, 30] and a value x = 25.
We have values 10, 20, 30 at indices 0,1,2 respectively.
So there are 2 valid indices.
Sample Input 1
[1, 2, 3, 5, 7]
13
Sample Output 1
3
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
/////
////
function main() {
let integers = JSON.parse(readLine());
let value = JSON.parse(readLine());
/* Write your code here and log the output */
}
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
/////
////
function main() {
let integers = JSON.parse(readLine());
let value = JSON.parse(readLine());
/* 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);
}
Comments
Leave a comment