-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array Leaders (Array Series #3)
38 lines (25 loc) · 1.03 KB
/
Array Leaders (Array Series #3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Task
Given an array/list [] of integers , Find all the LEADERS in the array.
Notes
Array/list size is at least 3 .
Array/list's numbers Will be mixture of positives , negatives and zeros
Repetition of numbers in the array/list could occur.
Returned Array/list should store the leading numbers in the same order in the original array/list .
Input >> Output Examples
arrayLeaders ({1, 2, 3, 4, 0}) ==> return {4}
Explanation:
4 is greater than the sum all the elements to its right side
Note : The last element 0 is equal to right sum of its elements (abstract zero).
arrayLeaders ({16, 17, 4, 3, 5, 2}) ==> return {17, 5, 2}
Explanation:
17 is greater than the sum all the elements to its right side
5 is greater than the sum all the elements to its right side
Note : The last element 2 is greater than the sum of its right elements (abstract zero).
*/
export function arrayLeaders(numbers: number[]): number[] {
return numbers.filter((el,i)=>{
const sum = numbers.slice(i+1).reduce((acc,curr)=>acc + curr , 0)
return el > sum
})
}