Given a set containing N numbers, minimize the average where you can take out any string of consecutive numbers in the set. |N|<=100000
Ex. {5, 1, 7, 8, 2}
You can take out {1,7}, etc. but the way to minimize in this case is just to take out {7,8} which will give a minimum average of (5+2+1)/3=2.667.
NOTE:-You can't use the first or last one, so you can't take out {5} or {2}. I want to know the general procedure to minimize this. I am looking for a linear solution. thanks
Build a $2$-dimensional table, with each cell $[i][j]$ indicating the sum of elements $i,i+1,\dots,j$:
Then, find the indexes (other than first and last) of the cell with the largest value:
Given a set of $n$ elements, time complexity and space complexity are both $O(n^2)$.