Any help is highly appreciated
How to find the sum of sub -arrays in an int array and return the largest sum value and the index position of those two elements
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Any help is highly appreciated
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Nov 4, 2008, 1:15 AM EST
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.