2

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);
5
  • Doesn't work how? What does it give you, what do you expect it to give you. Commented Nov 29, 2014 at 10:26
  • 1
    I assume you'll get an ArrayIndexOutOfBoundsException on this line if (values[k]==values[k+1]), right? Maybe you should loop while* k < values.length - 1 so k+1 won't exceed the array bounds. (* this is not a recommendation to use a while loop instead) Commented Nov 29, 2014 at 10:28
  • why not the answer is 3(4 4 4 ) in ur example? Commented Nov 29, 2014 at 10:31
  • @SUDHIR I want the first longest subsequent. Commented Nov 29, 2014 at 10:32
  • @Tom yes, i knew it but if I write System.out.println(values[k]) before if (values[k]==values[k+1]) I get 1 1 9 9 9 4 4 6 6 4 4 4 and it doesn't print the first element (5) Commented Nov 29, 2014 at 10:35

3 Answers 3

5

Your code has two errors:

for(int k=0; k<30; k++) {
    if (values[k]==values[k+1]) {

This loop will be executed until k reaches value 30. Therefore, the last used value for k is 29. If you use that value in the if statement, you'll exceed that array bounds by calling values[k+1] (== values[30]).

Change that loop to:

for(int k = 0; k < values.length - 1; k++) {

The second problem is this line:

lungmax = lungmax++;

This is the same as:

int temp = lungmax;
lungmax = lungmax + 1;
lungmax = temp;

As you can see, you're "ignoring" the increment. Change that line to:

lungmax++;
Sign up to request clarification or add additional context in comments.

1 Comment

Plus : I think lung++ will not work at 3rd 9 because 9==6 is false. and hence lungmax for 9 remains 2. and it will take 4's indice
4

Try this. By applying my approach, the ArrayIndexOutOfBoundsException has been eliminated.

public static void main(String args[]) {
    int lung = 1, lungmax = 0;
    int indice = 0;
    int[] values = {1, 2, 2, 3, 3, 3, 5, 5, 5, 5, 6};
    for (int k = 1; k < values.length; k++) {

        if (values[k - 1] == values[k]) {
            lung++;
            if (lung > lungmax) {
                lungmax = lung;
                indice = values[k];
            }
        } else {
            lung = 1;
        }
    }
    System.out.println("the length of the longest subsequence is: " + indice + "/" + lungmax);
}

Comments

1

Or simply make following changes

int lungmax=1;

and replace

lungmax=lung

with

lungmax++;

and remove last lungmax=lungmax++

plus what Tom suggested : k<30-1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.