Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int longestFlat(int[] arr)
- {
- return longestFlat(arr,0,0,arr[0],arr[1],0,0);
- }
- private static int longestFlat(int [] arr, int i,int j, int first, int second,int count,int max)
- {
- if(count > max)
- max = count;
- if(i==arr.length)
- return max;
- if((second == first || second == first + 1 || first==second-1) &&
- (arr[i] == first || arr[i] == second || arr[i] == first+1 || arr[i] == first-1 )){
- return longestFlat(arr, i+1,j, first, second, count+1, max);
- }
- else{
- if(i>arr.length-1)
- {
- j++;
- i=j;
- first = arr[i];
- second = arr[i+1];
- count=0;
- return longestFlat(arr, i, j, first,second, count,max);
- }
- return max;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement