Title of the question pretty much summarises the question. Here is a concrete example of what I'm trying to achieve with no success.
Let's say we have a sorted list (but sorted is not a requirement)
L = [1,3,20,100,150,200,260] and
K = 260. I need a function
f(L,K) which given this list and
K will return a list
B = [[1,3,20,200],[100,150],]. As you see if we just group elements in their sorting order with their sum <=
K like this:
[[1,3,20,100], , , ] we will have 4 subarrays but it's possible to have 3.
This algorithm may be useful for instance to make a compact linear stack layout of some UI elements of different lengths with min number of lines of those elements.
UPD. I decided to remove a word "efficient" from the question because this looks like an NP-hard problem. To be clear I need more or less optimal but correct solution. Numbers will not be large because I'm talking about this problem in the context of practical task (efficient UI layout) that's why
K and list elements will be a small numbers in the range [0, ..., ~1000].
I put a label "dynamic programming" because I think that this problem may be solved with dynamic programming approach and as was mentioned in the first comment is related to bin packing problem.