0

What would be the fastest way to find the maximum of n elements within an array. (They don't need to be consecutive)

Eg a={1 ,3, 2, 5, 0, 10}

maximum sum when n=2 will be 15

Eg if

  • n was 2 then we would use two loops
  • n was 3 we would use 3 loops but that would be O(n^3).

then for n = 4, 5 ... the solution becomes very hard to find iteratively

I thought recursion could present a solution for this so I came up with

long rec(ArrayList<Int> a, int i, int n) {      
        if (i > 0 && i< n) {
            return Math.max(a.get(i), rec(a, i-1, m)+ a.get(i));
        
        } else {
            return a.get(0);
        }
    }

called by

 for(int i = a.size() -1; i >=n; i--) {
             long max = rec(a,i,n);
        
}

But I can't seem to see a solution. Any suggestions on possible algorithms or approaches? Thanks

L m
  • 551
  • 1
  • 5
  • 8
  • 3 loops is O(3N)~O(N) not O(N³) thankfully. – Yann TM Sep 09 '20 at 15:27
  • sort the array first (in nlogn complexity) then take sum of n first entries ? scales and is easy to code – Yann TM Sep 09 '20 at 15:28
  • more complex to write but without a sort is traverse array once with a fixed size array of size n `Maxs` in which you store the largest values you encounter sorted ascending, so that if you current cell is larger than smallest value in Maxs, it is adopted. Then sum the values in the Maxs array at end of traversal. – Yann TM Sep 09 '20 at 15:30
  • Ahh got it. I was only thinking of a recursive way because it was a constraint in the question. I should have made myself clearer – L m Sep 09 '20 at 15:36

1 Answers1

0

Trying to answer from the time complexity point of view. Assuming that the input array has a size of S elements.

  1. Solution above with the fixed "maximums" array of size n storing all the maximums.
  • if the maximums in the array are sorted in place, searching with
    binary search to see if an element from the input array is present or should be inserted in the "maximums" array has the average complexity O(log(n)). this has to be performed for each of the elements in the input array so total average complexity is O(S*log(n)).

  • if the maximums are not sorted in place, complexity will be O(S*n) since there will be a linear search in the maximums array.

  1. Solution using a heap (https://en.wikipedia.org/wiki/Heap_(data_structure)) into which the initial array is loaded. loading the elements in the heap is O(S). popping out a maximum from the heap and re-constructing the heap is: ~O(log(S)); doing it n times is ~O(n*log(S)). so, total complexity is O(S)+O(n*log(S))

Let's compute the time complexity using some real sizes:

  1. S=32, n=3, O(S*log(n))~32, O(S)+O(n*log(S))~47
  2. S=32, n=5, O(S*log(n))~64, O(S)+O(n*log(S))~57
  3. S=32, n=8, O(S*log(n))~96, O(S)+O(n*log(S))~72

So, IMO, for large n the heap solution outperforms the "maximums array" one. If we assume that the array is always stored in a heap (i.e. already pre-loaded in heap) than the heap solution is always better.

cristian
  • 1
  • 1