I'm a beginner in programming and I need to print the longest sequence of numbers from int[] array. For example, if we have:
int[] numbers = {1, 3, 3, 5, 5, 5, 5, 5, 5, 6, 0, 12, 2, 2, 2, 12, 0};
Result should be:
String result = "5, 5, 5, 5, 5, 5";
I wrote some bad code that don't work, but maybe it will give you some ideas.
public String findLargestSequence(int[] numbers) {
int bestStart = 0;
int curStart = 0;
int bestLength = 1;
int curLength = 1;
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > numbers[i - 1]) {
curLength++;
if (curLength > bestLength) {
bestStart = curStart;
bestLength = curLength;
}
} else {
curStart = i;
curLength = 1;
}
}
List<String> identical = new ArrayList<>();
for (int i = 0; i < bestLength; i++) {
identical.add(String.valueOf(numbers[bestStart + i]));
}
return Joiner.on(", ").join(identical);
}
Update. Thanks to @phatfingers, I've found problem: (numbers[i] > numbers[i - 1])
should be (numbers[i] == numbers[i - 1])
. But still there is one another problem. If we have something like:
int[] numbers = {1, 2, 3, 3, 4, 4};
The result of it is:
"3, 3"
I think in this case, we could:
1) say, that we don't have any one longest sequence OR
2) show all that sequences, like:
String result = "Founded sequences: " + sequence1 + ", " + sequence2;
3) do nothing with that code above.
What would you do?
this show the max occurrence, you can also count it and print them
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) { // checking geater occurance
maxRun = thisRun;
}
}
return maxRun;
}
You have to handle 4 cases in the algorithm that you can divide in two parts:
Setting the state of the current serie :
Setting the state of the max serie :
In the actual code these conditions are not respected in the loop.
I commented two logic errors to illustrate the problem :
if (numbers[i] > numbers[i - 1]) {
// error : you don't increment the current serie when it grows
// because the condition if true only if the the current value is
// superior to the previous one
curLength++;
if (curLength > bestLength) {
bestStart = curStart;
bestLength = curLength;
}
}
// error : you don't reinit the current serie only when it changes
// because numbers[i] <= numbers[i - 1] is not true if the new number is
// superior to the previous one while it is a change
else {
curStart = i;
curLength = 1;
}
}
Here is the proposed code that handles the 4 conditions two by two:
public static String findLargestSequence(int[] numbers) {
// variables to maintain the max serie found and the number associated
int sizeMaxSerieFound = 0;
int numberMaxFound = numbers[0];
// variables to maintain the current serie found and the number
// associated
int sizeMaxCurrentSerie = 0;
int numberCurrentSerie = numbers[0];
boolean isMaxSerieIsTheCurrent = true;
for (int i = 0; i < numbers.length; i++) {
int currentNumber = numbers[i];
// FIRST STEP : set the state of the current serie
// increment the current serie if it grows or for the first
// iteration
if (currentNumber == numberCurrentSerie) {
sizeMaxCurrentSerie++;
}
// else we reinit to 1 the current serie
else {
sizeMaxCurrentSerie = 1;
numberCurrentSerie = currentNumber;
isMaxSerieIsTheCurrent = false;
}
// SECOND STEP : set the state of the max serie
// we increment the current number of the actual max serie
if (currentNumber == numberMaxFound && isMaxSerieIsTheCurrent) {
sizeMaxSerieFound++;
}
// we reinit the serie because we have found a new greater serie
else if (currentNumber != numberMaxFound && sizeMaxCurrentSerie > sizeMaxSerieFound) {
sizeMaxSerieFound = sizeMaxCurrentSerie;
numberMaxFound = currentNumber;
isMaxSerieIsTheCurrent = true;
}
}
List<String> identical = new ArrayList<>();
for (int i = 0; i < sizeMaxSerieFound; i++) {
identical.add(String.valueOf(numberMaxFound));
}
return Joiner.on(", ").join(identical);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.