Dividing the array in K parts

147 Views Asked by At

Let the array $A$ of seven elements be defined: \begin{align*} A[0] &= 2\\ A[1] &= 1\\ A[2] &= 5\\ A[3] &= 1\\ A[4] &= 2\\ A[5] &= 2\\ A[6] &= 2 \end{align*} I have to divide the array into $K$ parts such that larger sum is low, and return the index of those $k$ parts. For example $k=3$ \begin{align*} &[2, 1, 5, 1, 2, 2, 2],\, [],\, [] \quad\text{with a large sum of 15;}\\ &[2], [1, 5, 1, 2],\, [2, 2] \quad\ \ \ \text{with a large sum of 9;}\\ &[2, 1, 5],\, [], \,[1, 2, 2, 2] \quad\ \text{with a large sum of 8;}\\ &[2, 1],\, [5, 1],\, [2, 2, 2] \quad\ \ \ \text{with a large sum of 6.} \end{align*} So the algorithm should return 6. Which algorithm should I use ? Please provide me with an approach or code.