Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. public static int longestFlat(int[] arr)
  2.     {
  3.         return longestFlat(arr,0,0,arr[0],arr[1],0,0);
  4.     }
  5.  
  6.     private static int longestFlat(int [] arr, int i,int j, int first, int second,int count,int max)
  7.     {
  8.         if(count > max)
  9.             max = count;
  10.  
  11.         if(i==arr.length)
  12.             return max;
  13.  
  14.         if((second == first || second == first + 1 || first==second-1) &&
  15.         (arr[i] == first || arr[i] == second || arr[i] == first+1 || arr[i] == first-1 )){
  16.             return longestFlat(arr, i+1,j, first, second, count+1, max);
  17.         }
  18.         else{
  19.             if(i>arr.length-1)
  20.             {
  21.                 j++;
  22.                 i=j;
  23.                 first = arr[i];
  24.                 second = arr[i+1];
  25.                 count=0;
  26.                 return longestFlat(arr, i, j, first,second, count,max);
  27.             }
  28.             return max;
  29.  
  30.         }
  31.  
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement