Given a sequence of integer of length and prints the length of the longest sorted subsequence. If there is more than one subsequence of equal maximum length, the subsequence that appears first (the one with the smallest index) is to be used for the output
For the sequence: 8 2 3 4 5 6 0 10 26 24. The max length is 5. Here is the code i have so far. THank you guys. Note: i am not suppose to use array. I have to use Ctrl+Z to display the max sorted subsequence length.
import java.io.IOException;
public class RepeatingCharacterPositions {
public static void main(String[] s) throws IOException {
int counter = 0;
int inputValue;
int previousNum = 0;
int nextNum;
while ((inputValue = System.in.read()) != -1) {
nextNum = (int) inputValue;
if (previousNum < nextNum) {
counter++;
} else if (previousNum > nextNum) {
continue;
}
previousNum = nextNum;
}
System.out.println("Number of repeating " + counter);
}
}
A few notes:
You're not handling two consecutive equal values, at least not explicitly. You should consider using a >= or <= in whichever of your tests makes sense.
if (previousNum < nextNum) { counter++; } else if (previousNum > nextNum) { if(counter > longestSeq) { longestSeq = counter; } counter = 1; } previousNum = nextNum;
I believe that you're missing a few things.
counter
reached int
from System.in
, you should use a Scanner
which provides the method Scanner#nextInt
if (previousNum <= nextNum)
counter
to the value of 1 (because any number IS itself an ordered sequence of length 1 ) For example:
public static void main(final String[] s) {
int counter = 0;
int highestCount = 0;
int previousNum = 0;
int position = 0;
int latestStartingPosition = 0;
int startingPosition = 0;
int inputValue;
try (final Scanner sc = new Scanner(System.in)) {
while ((inputValue = sc.nextInt()) != -1) {
if (previousNum <= inputValue) {
counter++;
if (counter > highestCount) {
highestCount = counter;
startingPosition = latestStartingPosition;
}
} else {
latestStartingPosition = position;
counter = 1;
}
previousNum = inputValue;
position++;
}
}
System.out.println("starting pos: " + startingPosition);
System.out.println("Number of repeating positions: " + highestCount);
}
I hope it helps!
EDIT: Answering additional question asked in comments.
To get the index of the beginning of the longest ordered sequence, you have to:
int
to know your current position at any instant counter
. counter
has reached. See updated example in the code above :)
Besides what ccjmne pointed out, you can always use a separate method to do this:
public class RepeatingCharacterPositions {
public static int findLongestSorted(int[] array) {
int maxCount = 0, count = 0;
int prev = array[0];
for(int num : array) {
if(prev <= num) {
count++;
} else {
count = 1;
}
if(count > maxCount)
maxCount = count;
prev = num;
}
return maxCount;
}
public static void main(String[] s) throws IOException {
int[] arr = {8, 2, 3, 4, 5, 6, 0, 10, 26, 24};
System.out.println("Number of repeating positions " + findLongestSorted(arr));
}
}
Note that prev
is initialized to be the first element in the array since array with only one element is considered to be sorted. After execution of the program, it will print out
Number of repeating positions 5
If you test on array with only one element, eg,
int[] arr = {8};
It will output:
Number of repeating positions 1
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.