Its working in O(N) , then why its showing TLE in all options

public static void main(String[] args) {
	
	Scanner sc=new Scanner(System.in);
	int t=sc.nextInt();

	for(int i=1 ; i<=t ; i++)
	{
		int n=sc.nextInt();
		int arr[]=new int[n];
		for(int j=0 ; j<n ; j++)
			arr[j]=sc.nextInt();
		
		System.out.println(bitonic(arr));
	}
	
}

static int bitonic(int[] arr)
{

// entirely increasing
int length=1;
for(int j=1 ; j<arr.length ; j++)
{
if(arr[j]>arr[j-1])
length++;
else
break;
}

	if(length==arr.length)
		return length;

// entirely decreasing
length=1;
for(int j=1 ; j<arr.length ; j++)
{
if(arr[j]<arr[j-1])
{
length++;
}
else
break;
}

	if(length==arr.length)
		return length;

// checking max length
length=1;
int max=0;
int i=1;
while(i<arr.length)
{
// when increasing
while(i<arr.length)
{
if(arr[i-1]<arr[i])
{
i++;
length++;
}
else
{
break;
}
}

		// when decreasing
		while(i<arr.length)
		{
			if(arr[i-1]>arr[i])
			{
				i++;
				length++;
			}
			else
			{
				break;
			}
		}
		
		// upgrade max ans
		if(max<length)
			max=length;
		
		// checking again for new cycle
		length=1;
	}
	
	
	return max;
	
}

Hey @Himanshu-Jhawar-2273952536067590
Just tell me how it is in O(N).

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.

Algorithm

  1. Initialize inc[0] to 1 and dec[n-1] to 1
  2. Creating inc[] array
    a. Till end of the array ie, i=1 to n, if arr[i] > arr[i-1] then inc[i] = inc[i-1] + 1. else, inc[i] = 1
  3. Creating dec[] array
    a. From the end of the array ie, i = n-2 till i =0, if arr[i] > arr[i+1] then dec[i] = dec[i+1] +1 else, dec[i] = 1