Answer to Question #312817 in HTML/JavaScript Web Application for sandhya

Question #312817

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:



1
Expert's answer
2022-03-17T13:55:57-0400
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);
}

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