algorithm - Find the maximum value in an array that's the sum of 3 other values -


Looking at a very large integer array, I need to find the maximum value of a4 , such as: a4 = a1 + a2 + a3

Where there are all values ​​in the A array.

How will I do this?

Note: Using 4 for loops is not the ideal solution.

There is a simple (expected) O (N ^ 2) solution:

  1. Repeat through all the pairs of array elements (A, B) and submit their sum to a hash table.
  2. Repeat through all candidates added (A4, A1) and see if the A4 - A1 table is in. All solution solutions are more than A4. Of course you should do the smallest process of A4, but it does not affect asiplotics.

    If you want to avoid using more than one element, then you need some additional information in the hash table so that you can filter the pairs that are either a1 or a4 fast Let's slide with.

    If the integers are bound in the array (max - min

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -