-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.2.0.dart
More file actions
87 lines (58 loc) · 2.24 KB
/
Copy path11.2.0.dart
File metadata and controls
87 lines (58 loc) · 2.24 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Definition
An element is leader if it is greater than The Sum all the elements to its right side.
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).
arrayLeaders ({5, 2, -1}) ==> return {5, 2}
Explanation:
5 is greater than the sum all the elements to its right side
2 is greater than the sum all the elements to its right side
Note : The last element -1 is less than the sum of its right elements (abstract zero).
arrayLeaders ({0, -1, -29, 3, 2}) ==> return {0, -1, 3, 2}
Explanation:
0 is greater than the sum all the elements to its right side
-1 is greater than the sum all the elements to its right side
3 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).
Playing with Numbers Series
Playing With Lists/Arrays Series
For More Enjoyable Katas
*/
void main() {
arrayLeaders([6, 5, 6, 5, 4]);
}
List<int> arrayLeaders(List<int> numbers) {
List<int> leaders = [];
for (int i = 0; i < numbers.length; i++) {
// current number should be greater than the sum of the following numbers
if (numbers[i] > numbers.sublist(i + 1).fold(0, (num a, int b) => a + b)) {
leaders.add(numbers[i]);
}
}
print(leaders);
return leaders;
}
List<int> arrayLeaders1(numbers) {
final leaders = <int>[];
int rightSum = 0;
for (int n in numbers.reversed) {
if (n > rightSum) leaders.add(n);
rightSum += n;
}
return leaders.reversed.toList();
}