Question : Write a program to print the count of the longest sequence in the input array
Lets take an example of
input: 1,2,14,3,4,5,10,7,8,9,10,11,12,13,14,15
output: 9
Here we have sequence 1,2 count=2
3,4,5 count=3
7,8,..15 count=9
So the output should be 9.
public class LongSequence {
public static int printCounter(int[] arg) {
int counter = 1;
int resultCounter = 0;
for (int i = 0; i < arg.length - 1; i++) {
if (arg[i] + 1 == arg[i + 1]) {
++counter;
} else {
if (counter >= resultCounter) {
resultCounter = counter;
}
counter=0;
}
}
System.out.println(resultCounter);
return resultCounter;
}
}
Lets take an example of
input: 1,2,14,3,4,5,10,7,8,9,10,11,12,13,14,15
output: 9
Here we have sequence 1,2 count=2
3,4,5 count=3
7,8,..15 count=9
So the output should be 9.
public class LongSequence {
public static int printCounter(int[] arg) {
int counter = 1;
int resultCounter = 0;
for (int i = 0; i < arg.length - 1; i++) {
if (arg[i] + 1 == arg[i + 1]) {
++counter;
} else {
if (counter >= resultCounter) {
resultCounter = counter;
}
counter=0;
}
}
System.out.println(resultCounter);
return resultCounter;
}
}
No comments:
Post a Comment