I know most people don't like writing methods for people but i was hoping someone could help me convert my algorithm into Java code. I hope my algorithm is good and actually works.
- Sort a given array of ints into ascending order. Set Group Limit to 15 (that means that the sum of the group is not greater than 15).
- Take the first element of the sorted array and insert into a Group (new array/list) eg. Group A.
- Take the second element of the sorted array and insert unless it will make it exceed the group limit. If it exceeds, create a new Group B and insert there.
- Take third element and try to insert into next available group.
- Repeat until all
int
s have been checked and grouped.
Input:
egArray = [1,3,4,6,6,9,12,14]
Output:
Group A: [1,3,4,6], Group B: [6,9], Group C: [12], Group D: [14]
I have tried to do this, but failed epically, not even worth me posting my code. :-(
This is an example data and an algorithm I've made up for self learning, so please keep the criticism to a minimum. I genuinely learn from a lot of Stackoverflow posts people have written over the last few months, unfortunately I couldn't find one like this example. Thanks.