SHARE
TWEET

Longest Odd-Even Sequence

g-stoyanov Jul 26th, 2014 243 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     var numbers = input[0].split(/[ ()]+/);
  3.     numbers = numbers.filter(Boolean).map(Number);
  4.    
  5.     var maxCounter = 0;
  6.     var counter = 1;
  7.     for (var i = 0; i < numbers.length - 1; i++) {
  8.        
  9.         var isOddCurrent = numbers[i] % 2 !== 0;
  10.         var isOddNext = numbers[i + 1] % 2 !== 0;
  11.  
  12.         if (isOddCurrent !== isOddNext || numbers[i] == 0 || numbers[i + 1] == 0) {
  13.            
  14.             counter++;
  15.                        
  16.         } else {
  17.             if (maxCounter < counter) {
  18.                 maxCounter = counter;
  19.             }
  20.  
  21.             counter = 1;
  22.         }
  23.     }
  24.  
  25.     console.log(maxCounter < counter ? counter : maxCounter);
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top