Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LongestSequence {
- public static void main(String [] args) {
- String input = args[0];
- int ld = 0;
- int [] inArr = new int[100];
- int numInts = 0;
- String substr = "";
- for (int i = 0; i < input.length(); i++) {
- if(input.charAt(i) == ' ') {
- substr = input.substring(ld,i);
- inArr[numInts++] = Integer.parseInt(substr);
- ld = ++i;
- }
- }
- substr = input.substring(ld);
- inArr[numInts++] = Integer.parseInt(substr);
- if(numInts == 1 || numInts == 2) {
- System.out.println("Longest Sequence: " + numInts);
- return;
- }
- int a = inArr[0];
- int b = inArr[1];
- int c = -99;
- int ls = 2;
- int cs = 2;
- for(int i = 2; i < numInts; i++) {
- c = inArr[i];
- if (c == a || c == b) {
- cs++;
- if (cs > ls) {
- ls = cs;
- }
- } else {
- cs = 2;
- a = inArr[i-1];
- b = inArr[i];
- }
- }
- System.out.println("Longest Sequence: " + ls);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement