I have numbers in a single array (30,20,25,15,50,30,80..). I want to group all numbers possible in another array without surpassing a total of 80. For example:
Group 1: 30,20,25 (cant add 15 cause the total will be 90, higher than 80)
Group 2: 50,30
Group 3: 80
But based on your limited example, it sounds like you just skip the number that would otherwise exceed the current group (if the current group isn't already a perfect total of 80).
Have a moving start_index and end_index. Like an "inch worm", so to speak.
Step 1:
start_index = 0
end_index = 0 --> total = 30
Add 30 to first group.
Step 2:
end_index = 1 --> total = 50
Add 20 to first group.
Step 3:
end_index = 2 --> total = 75
Add 25 to first group.
Step 4:
end_index = 3 --> total = 90 --> this is over 80, so:
- set start_index to be end_index + 1.
- start_index = end_index + 1 = 4
- end_index = start_index
- increment group number
- add element[end_index] to group