How to inform the largest consecutive sequence?

Asked

Viewed 935 times

0

Guys I’m hooked on this algorithm where I create an integer vector with n positions and need to show the longest consecutive sequence.

I will leave the statement to try to be more specific.

"Elaborate an algorithm where the user type 50 integer numbers of a vector and at the end the program tells the size of the largest consecutive increasing sequence."

ex:

6,7,9. Capital = 2.

5,6,7,8,11. Most important = 4

What I’ve done so far:

public class Questao02 {

        public static void main(String[] args){
            String aux;
            int a[] = new int[5];
            int cont=0;

            for(int i=0;i<5;i++){
                aux = JOptionPane.showInputDialog("Números");
                a[i] = Integer.parseInt(aux);

                if(a[i] < a[i+1] ){
                    cont++;

                }

                else{
                    cont=0;

                }

            }

            System.out.println(cont);
        }
}
  • It is not a difficult algorithm even for it is starting. You will need to use a for to traverse the vector and variables to account for the sequences found and verify that the sequence you have so far is larger than the one you’ve already caught so far.

  • When I use the for to go through the vector I need to make a comparison between the current value that is stored in the vector with the next vector value right, I tried to do it here but it’s going wrong.

  • If you do not add your attempt and paste only the statement, the question will be closed again. Edit it and demonstrate what you have already tried to do, demonstrating that you have made some effort to try something.

  • Articuno, I’ll add the code I tried to make

  • 1

    And also take the opportunity to show exactly what point of code is not working for you and that is creating difficulties. Otherwise it is difficult for the community to help you solve the problem

  • And don’t forget to explain at what point your difficulty, as @Isac said.

  • My problem is that I am not able to see a solution in the if, I know it is wrong and also I’m having difficulty in getting the next vector value, I do not know if to do a[i+1], it is right.

  • Ta mixing graphical interface with text mode? I recommend not to do this, it is a bad practice.

  • I like to use Scanner, I’m using Joption out of curiosity, and not recommended in this case because it only accepts Strings, then I had to make this adaptation, using the variable aux.

Show 4 more comments

1 answer

0


It is important to separate the introduction of values from the logic of accounting. In this case it even simplifies because the accounting cannot be done for all values because it considers the front value, so it has to finish one before the end.

You also need another variable to store the maximum string found so far, otherwise when the cont back to 0 loses the previous sequence that could even be larger.

public static void main(String[] args) {
    int a[] = new int[5];
    int cont = 0;
    int maior = 0; //nova variável para a maior sequencia achada

    //só a leitura
    for(int i=0;i<5;i++){
        //sem aux para simplificar
        a[i] = Integer.parseInt(JOptionPane.showInputDialog("Números"));
    }

    //só a contabilização
    for(int i=0;i<4;i++){ //até ao 4, um antes do fim
        if(a[i] == a[i+1]-1){ //teste de valor 1 abaixo, em vez de apenas inferior
            cont++;

            //verifica e atualiza com a maior sequência
            if (cont > maior){  achada até ao momento
                maior = cont;
            }
        }
        else{
            cont=0;
        }
    }

    //quando testa se o número é sequencia com o da frente o maior vai ficar com 1 mas
    //a sequência já representa dois números, logo é necessário no fim somar 1
    maior++;
    System.out.println(maior);
}

Considerations:

  • Just like Articuno this is not a good idea to mix GUI and console mode, so consider using Scanner and nextInt() for reading, for consistency.
  • The way the parseInt can launch an exception if the user does not enter a number. Please consider using exception handling with try catch if you want to prevent the program from exploding in this case.
  • Thanks, really I hadn’t thought about creating another for to account for and use the if that way. The code became well educational learned a lot, will surely serve as experience. I was using Joption out of curiosity, since the teacher showed it in class, but as you said it’s not a very cool practice, I’ll be using the Scanner.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.