Problem H (Longest Natural Successors):

Two consecutive integers are natural successors if the second is the successor of the first in the sequence of natural numbers (1 and 2 are natural successors). Write a program that reads a number N followed by N integers, and then prints the length of the longest sequence of consecutive natural successors.


Input  7 2 3 5 6 7 9 10
Output 3

This is my code so far:

  import java.util.Scanner;

    public class Conse {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
     Scanner scan = new Scanner(System.in); 
     int x=scan.nextInt();
     int[] array= new int[x];
     for(int i=0;i<array.length;i++)
public static int array(int[] array){
    int count=0,temp=0;
    for(int i=0;i<array.length;i++){
        for(int j=i,k=i+1;j<array.length-1;j++,k++)
    return temp+1;

  • \$\begingroup\$ Would be interesting to try solving this question using LINQ. \$\endgroup\$
    – Mehrad
    Commented Dec 10, 2014 at 5:23

3 Answers 3


Using static for the consecutive() function is appropriate. However, the omission of braces, the inconsistent indentation, and lack of spaces between operators is just as bad as before.

Your function crashes on arrays of length 0 or 1.

The variable names f and counter could be improved. I think that a different looping structure would be clearer as well.

public static int consecutive(int[] array) {
    if (array.length <= 1) {
        return array.length;

    int maxRun = 0;
    for (int i = 1; i < array.length; i++) {
        int thisRun = 1;
        while (i < array.length && array[i - 1] + 1 == array[i]) {
        if (maxRun < thisRun) {
            maxRun = thisRun;
    return maxRun;


  • a class named Conse does not express what the class is about.
  • a method named array does not epress what the method is about.

For naming classes you should use noun or noun phrases. For naming methods you should use verbs or verb phrases.


Consider to change your logic to use only one loop. On each iteration check if the current number equals the lastnumber + 1. If yes, increment the counter by 1 if no set the counter = 1.

After these checks set the lastnumber to the current number and calculate the maximum of the current counter and the overall maximum counter.

private static int getLongestSuccessorCount(int[] input) {
    int length = input.length;
    if (length <= 1) {
        return length;

    int counter = 0;
    int lastNumber = 0;
    int longestSuccessorCount = 0;
    for (int currentNumber : input) {

        if (currentNumber == lastNumber + 1) {
            counter += 1;
        } else {
            counter = 1;

        lastNumber = currentNumber;
        longestSuccessorCount = Math.max(longestSuccessorCount, counter);
    return longestSuccessorCount;

A few generic comments:

  1. CheckStyles. (have spaces, line spaces, if-else/for-do-while block) proper.
  2. Nomenclature (Class Conse, function name array, a return variable temp)

Comments related to the code snippet:

  1. Invalid value of x (if x is zero or negative, would this work:

    int[] array= new int[x];
  2. Two loops is not required. One traversal is enough because all you need to do is check the consecutiveness and if it fails update the max.

    public static int getConsecutiveness(int[] input){
        int maxCount = 1;
        int tempMax=1;
        for(int i=1;i<input.length;i++){
                maxCount=Math.max(maxCount, tempMax);
        maxCount=Math.max(maxCount, tempMax);
        return maxCount;
  • \$\begingroup\$ To post code you need to either add 4 spaces before each line , or you select all of the code and click on {}. \$\endgroup\$
    – Heslacher
    Commented Dec 4, 2014 at 10:35
  • \$\begingroup\$ Had some issue with it. Tried to correct the same. \$\endgroup\$
    – thepace
    Commented Dec 4, 2014 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.