4
\$\begingroup\$

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.

Example:

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++)
 array[i]=scan.nextInt();
 System.out.println(array(array));
 }
public static int array(int[] array){
 int count=0,temp=0;
 for(int i=0;i<array.length;i++){
 count=0;
 for(int j=i,k=i+1;j<array.length-1;j++,k++)
 if(array[j]-array[k]==1)
 count++;
 else{if(temp<count)
 temp=count;
 break;}
 }
 return temp+1;
 } 
 }
asked Dec 3, 2014 at 21:16
\$\endgroup\$
1
  • \$\begingroup\$ Would be interesting to try solving this question using LINQ. \$\endgroup\$ Commented Dec 10, 2014 at 5:23

3 Answers 3

5
\$\begingroup\$

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]) {
 thisRun++;
 i++;
 }
 if (maxRun < thisRun) {
 maxRun = thisRun;
 }
 }
 return maxRun;
}
answered Dec 3, 2014 at 22:10
\$\endgroup\$
0
3
\$\begingroup\$

Naming

  • 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.

Logic

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;
}
answered Dec 4, 2014 at 9:20
\$\endgroup\$
1
\$\begingroup\$

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++){
     if(input[i]-input[i-1]==1){
     tempMax++;
     }else{
     maxCount=Math.max(maxCount, tempMax);
     tempMax=1;
     }
     }
     maxCount=Math.max(maxCount, tempMax);
     return maxCount;
    }
    
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Dec 4, 2014 at 10:03
\$\endgroup\$
2
  • \$\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\$ Commented Dec 4, 2014 at 10:35
  • \$\begingroup\$ Had some issue with it. Tried to correct the same. \$\endgroup\$ Commented Dec 4, 2014 at 10:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.