Magical Indices
Given an array of integers and a number x.An index is valid if
item y at an index is increased by x and
x+y would be greater than the sum of all other items in the array
write a JS program to determine the number of valid 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.
input:
[1, 2, 3, 5, 7]
13
output:
3
input:
[34, 32, 37, 38, 40]
10
output:
function magicalIndices(arr, x) {
let res = 0;
let sum = arr.reduce((prev, cur) => prev + cur, 0);
arr.map(y=> {
y + x >= sum - y ? res++ : '';
});
console.log(res);
}
Comments
Leave a comment