Lets say we have the following set of numbers representing values over time
1 2 3 10 1 20 40 60
Now I am looking for an algorithm to find the highest percentage increase from one time to another. In the above case, the answer would be the pair (1, 60), which has a 6000% increase.
So far, the best algorithm I can think of is a brute-force method. We consider all possible pairs using a series of iterations:
1st Iteration:
1-2 1-3 1-10 .. 1-60
2nd Iteration
2-3 2-10 2-1 ... 2-60
(etc.)
This has complexity O(n3).
I've also been thinking about another approach. Find all the strictly increasing sequences, and determine only the perecentage increase in those strictly increasing sequences.
Does any other idea strike you guys? Please do correct me if my ideas are wrong!
I may have misunderstood the problem, but it seems that all you want is the largest and smallest numbers, since those are the two numbers that matter.
while true:
indexOfMax = max(list)
indexOfMin = min(list)
list.remove(indexOfMax)
list.remove(indexOfMin)
if(indexOfmax < indexOfMin)
contine
else if(indexOfMax == indexOfMin)
return -1
else
SUCCESS