I have an array, i'd like to calculate the length of the longest subsequence made by equals numbers: Example: 5 1 1 9 9 9 4 4 6 6 4 4 4 --> the length of the longest subsequence is 3 (9 9 9). This is what i got so far but it doesn't work.
int lung=0, lungmax=0;
int indice = 0;
int [] values = new int [30];
for(int k=0; k<30; k++)
{
if (values[k]==values[k+1])
{
lung++;
if (lung>lungmax)
{
lungmax=lung;
indice=values[k];
}
}
else lung=0;
}
lungmax = lungmax++;
System.out.println("the length of the longest subsequence is: "+lungmax);
ArrayIndexOutOfBoundsExceptionon this lineif (values[k]==values[k+1]), right? Maybe you should loop while*k < values.length - 1sok+1won't exceed the array bounds. (* this is not a recommendation to use awhileloop instead)