Finding the longest down sequence in a Java array -


given array

int [] myarray = {5,-11,2,3,14,5,-14,2}; 

i must able return 3 because longest down sequence 14,5,-14. what's fastest way this?

ps: down sequence series of non-increasing numbers.

just make 1 pass through list of numbers. pseudocode:

bestindex = 0 bestlength = 0  curindex = 0 curlength = 1  index = 1..length-1    if a[index] less or equal a[index-1]        curlength++    else         //restart @ index since it's new possible starting point        curlength = 1        curindex = index     if curlength better bestlength        bestindex = curindex        bestlength = curlength  next           

note: can ditch line containing bestindex or curindex if don't care knowing subsequence occurs, seen in gary's implementation.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -