Max length bitnonic subarray:

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;
}

}

Please reply fast!!!

what kind of instant doubt support is this?

@arjunsabu99,
Buddy I am looking at it. Please be patient.

@arjunsabu99,

Approach:
We create two arrays - β€˜inc’ and β€˜dec’

  1. inc[i] stores the length of increasing subarray till i.
  2. dec[i] stores the length of decreasing subarray starting from index i.
  3. Doing so gives us the length of increasing and decreasing subarray at each index in O(n) time.
  4. We calculate the length of the longest bitonic subarray by finding the maximum inc[i] + dec[i] - 1
  5. We subtract one since the current element at ith index is included in both the increasing and decreasing subarray lengths.

https://ide.codingblocks.com/s/328737 corrected code.

are you able to compete this code I also used same approach and able to pass only one test case please reply if your doubt is solved or not and if solved please explain.

No buddy! I was not able to pass all the test cases using that code