Advertisement
l-kikov

Maximal_Increasin_Sequence_In_Array

Feb 7th, 2015
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1. public static void main(String[] args) {
  2.        
  3.         Integer[] array = {1,3,5,7,2,2,3,4,8,9};
  4.        
  5.         int startIndex = 0;
  6.         int lenghtSequence = 1;
  7.         int bestStartIndex = 0;
  8.         int bestLenghtSequence = 0;
  9.        
  10.         for (int i = 1; i < array.length; i++) {
  11.            
  12.             if (array[i-1] + 1 == array[i]) {
  13.                 lenghtSequence ++;
  14.                 continue;
  15.             }
  16.            
  17.             if (bestLenghtSequence < lenghtSequence) {
  18.                 bestLenghtSequence = lenghtSequence;
  19.                 bestStartIndex = startIndex;
  20.             }
  21.            
  22.             lenghtSequence = 1;
  23.             startIndex = i;    
  24.            
  25.         }
  26.        
  27.         for (int i = bestStartIndex; i < bestStartIndex + bestLenghtSequence; i++) {
  28.            
  29.             System.out.print(array[i] + " ");
  30.            
  31.         }
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement