I'll try to phrase this question without making it sound like I am seeking for homework answers (this is just a practice problem for algorithms)
you have an array of numbers where every value can occur at most 2x [1 3 5 2 5 1 2 3] Examine sums from one value to the other instance of itself (5 + 2 + 5) (2 + 5 + 1 + 2)
Find an algorithm that finds the max of such sum.
I came up with a pretty simple algorithm:
iterate through the array (for i=1 to n)
iterate through the remaining array (for j=i+1)
if A[i] == A[j]
s = 0
iterate through the values between those two points (for k=i to j)
s = s + A[k]
maxVal = max(maxVal,s)
What are some steps I can take towards optimizing the algorithm. (or any algorithm for that matter). Obviously this solution is the first that came to mind but I am having trouble envisioning better solutions that would be more efficient.
Edit: For sake of the problem I'll just say all elements are postitive