Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Greatest Sum Divisible by Three
Bookmark
Greedy
Dynamic Programming
Input
Example 1
Sequential
Single Element
Repeated
Custom
nums
=
[3, 6, 5, 1, 8]
totalSum =
?
3
6
5
1
8
totalSum =
?
3
6
5
1
8
totalSum =
23
% 3 =
2
3
6
5
1
8
totalSum =
23
% 3 =
2
3
6
5
1
8
23
% 3 =
2
drop the cheapest offenders
totalSum =
23
% 3 =
2
sorted ascending
1
3
5
6
8
totalSum =
23
% 3 =
2
1
3
5
6
8
rem1
= [1]
rem2
= [5, 8]
totalSum =
23
% 3 =
2
1
3
5
6
8
rem1
= [1]
rem2
= [5, 8]
totalSum =
23
% 3 =
2
1
3
5
6
8
rem1
= [1]
rem2
= [5, 8]
Option A
drop 5
18
totalSum =
23
% 3 =
2
1
3
5
6
8
rem1
= [1]
rem2
= [5, 8]
Option A
drop 5
18
Option B
not possible
0
totalSum =
23
% 3 =
2
1
3
5
6
8
rem1
= [1]
rem2
= [5, 8]
Option A
drop 5
18
Option B
not possible
0
max(
18
,
0
) =
18
totalSum =
23
% 3 =
2
1
3
5
6
8
rem1
= [1]
rem2
= [5, 8]
Option A
drop 5
18
Option B
not possible
0
Greatest sum divisible by 3 =
18
algo
master
.
io
Step:
Find the maximum sum of elements that is divisible by 3
0 / 9
Input
Example 1
Sequential
Single Element
Repeated
Custom
nums
=
[3, 6, 5, 1, 8]
0 / 9
algo
master
.
io
Step:
Find the maximum sum of elements that is divisible by 3