Any help is highly appreciated
Loading
Any help is highly appreciated
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Nov 4, 2008, 6:15 AM
Well, as the question is asking for the "index position of those two elements", I assume that the only sub-arrays which are of interest are those with two elements.
In an array of size 'n', there will be 'n' ways of selecting the first element and 'n-1' ways of selecting the second element. So, altogether, there will be n * (n-1) / 2 combinations to consider and work out their sums, remembering that you're not interested in the order of the two elements - the sum is the same either way :)
So all you need to do is to set up a double 'for' loop (with indices i and j( > i) say), work out i + j and test whether it's greater than the previous highest total and record the indices of the highest combination as you go.