Max length bitnonic subarray: The code is working for all conditions when (i) when array is increasing and then decreasing, (ii) when array is strictly increasing, (iii) when array is strictly decreasing . Still i am getting wrong answer in three test cases.
import java.util.*;
public class Main{
public static void main(String [] args)
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
int n;
while(t-- != 0)
{
n = s.nextInt();
int [] A = new int [n];
for(int i = 0; i<n; i++)
A[i] = s.nextInt();
System.out.println(maxBitonic(A, n));
}
}
public static int maxBitonic(int [] A, int n)
{
int left = 0, right = 0, maxAns = 1, ans = 0;
boolean isDecreasing = false;
for(; right < n; right++)
{
//last element
if(right == n-1)
{
ans = right - left + 1;
if(ans > maxAns)
maxAns = ans;
}
//decreasing
else if(A[right] > A[right+1])
{
isDecreasing = true;
}
//increasing
else
{
if(isDecreasing)
{
isDecreasing = false;
ans = right - left + 1;
if(ans > maxAns)
maxAns = ans;
left = right;
}
}
}
return maxAns;
}
}