Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. public class LongestSequence {
  2.  
  3.     public static void main(String [] args) {
  4.        
  5.         String input = args[0];
  6.         int ld = 0;
  7.         int [] inArr = new int[100];
  8.         int numInts = 0;
  9.         String substr = "";
  10.        
  11.        
  12.         for (int i = 0; i < input.length(); i++) {
  13.             if(input.charAt(i) == ' ') {
  14.                 substr = input.substring(ld,i);
  15.                 inArr[numInts++] = Integer.parseInt(substr);
  16.                 ld = ++i;
  17.             }
  18.         }
  19.         substr = input.substring(ld);
  20.         inArr[numInts++] = Integer.parseInt(substr);
  21.        
  22.         if(numInts == 1 || numInts == 2) {
  23.             System.out.println("Longest Sequence: " + numInts);
  24.             return;
  25.         }
  26.        
  27.         int a = inArr[0];
  28.         int b = inArr[1];
  29.         int c = -99;
  30.         int ls = 2;
  31.         int cs = 2;
  32.        
  33.         for(int i = 2; i < numInts; i++) {
  34.             c = inArr[i];
  35.             if (c == a || c == b) {
  36.                 cs++;
  37.                 if (cs > ls) {
  38.                     ls = cs;
  39.                 }
  40.             } else {
  41.                 cs = 2;
  42.                 a = inArr[i-1];
  43.                 b = inArr[i];
  44.             }
  45.         }
  46.        
  47.         System.out.println("Longest Sequence: " + ls);
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement