Editorial for Longest Sequence of Equal


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
by: praykov

Sample solutions:

Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = in.nextInt();
        }

        int maxLength = 1;
        int currentLength = 1;
        int currentElement = numbers[0];
        for (int i = 1; i < n; i++) {
            if (numbers[i] == currentElement) {
                currentLength++;
                if (currentLength > maxLength) {
                    maxLength = currentLength;
                }
            } else {
                currentElement = numbers[i];
                currentLength = 1;
            }
        }

        System.out.println(maxLength);
    }
}

Comments

There are no comments at the moment.