# Program to check if an array is bitonic or not

Given an array of N elements. The task is to check if the array is bitonic or not.

An array is said to be bitonic if the elements in the array are first strictly increasing then strictly decreasing.

Examples:

```Input: arr[] = {-3, 9, 11, 20, 17, 5, 1}
Output: YES

Input: arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 11};
Output: NO
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Start traversing the array and keep checking if the next element is greater than the current element or not.
• If at any point, the next element is not greater than the current element, break the loop.
• Again start traversing from the current element and check if the next element is less than current element or not.
• If at any point before the end of array is reached, if the next element is not less than the current element, break the loop and print NO.
• If the end of array is reached successfully, print YES.

Below is the implementation of above approach:

## C++

 `// C++ program to check if an array is bitonic ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if the given array is bitonic ` `int` `checkBitonic(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `i, j; ` ` `  `    ``// Check for increasing sequence ` `    ``for` `(i = 1; i < n; i++) { ` `        ``if` `(arr[i] > arr[i - 1]) ` `            ``continue``; ` ` `  `        ``if` `(arr[i] <= arr[i - 1]) ` `            ``break``; ` `    ``} ` ` `  `    ``if` `(i == n - 1) ` `        ``return` `1; ` ` `  `    ``// Check for decreasing sequence ` `    ``for` `(j = i + 1; j < n; j++) { ` `        ``if` `(arr[j] < arr[j - 1]) ` `            ``continue``; ` ` `  `        ``if` `(arr[j] >= arr[j - 1]) ` `            ``break``; ` `    ``} ` ` `  `    ``i = j; ` ` `  `    ``if` `(i != n) ` `        ``return` `0; ` ` `  `    ``return` `1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { -3, 9, 7, 20, 17, 5, 1 }; ` ` `  `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``(checkBitonic(arr, n) == 1) ? cout << ``"YES"` `                                ``: cout << ``"NO"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to check  ` `// if an array is bitonic ` `class` `GFG ` `{ ` `// Function to check if the  ` `// given array is bitonic ` `static` `int` `checkBitonic(``int` `arr[], ``int` `n)  ` `{ ` `    ``int` `i, j; ` ` `  `    ``// Check for increasing sequence ` `    ``for` `(i = ``1``; i < n; i++) ` `    ``{ ` `        ``if` `(arr[i] > arr[i - ``1``]) ` `            ``continue``; ` ` `  `        ``if` `(arr[i] <= arr[i - ``1``]) ` `            ``break``; ` `    ``} ` ` `  `    ``if` `(i == n - ``1``) ` `        ``return` `1``; ` ` `  `    ``// Check for decreasing sequence ` `    ``for` `(j = i + ``1``; j < n; j++)  ` `    ``{ ` `        ``if` `(arr[j] < arr[j - ``1``]) ` `            ``continue``; ` ` `  `        ``if` `(arr[j] >= arr[j - ``1``]) ` `            ``break``; ` `    ``} ` ` `  `    ``i = j; ` ` `  `    ``if` `(i != n) ` `        ``return` `0``; ` ` `  `    ``return` `1``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[])  ` `{ ` `    ``int` `arr[] = { -``3``, ``9``, ``7``, ``20``, ``17``, ``5``, ``1` `}; ` ` `  `    ``int` `n = arr.length; ` ` `  `    ``System.out.println((checkBitonic(arr, n) == ``1``) ?  ` `                                             ``"YES"` `: ``"NO"``); ` `} ` `} ` ` `  `// This code is contributed by Bilal `

## Python3

 `# Python3 program to check if  ` `# an array is bitonic or not. ` ` `  `# Function to check if the  ` `# given array is bitonic ` `def` `checkBitonic(arr, n) : ` ` `  `    ``# Check for increasing sequence ` `    ``for` `i ``in` `range``(``1``, n) : ` `        ``if` `arr[i] > arr[i ``-` `1``] : ` `            ``continue` `        ``else` `: ` `            ``break` ` `  `    ``if` `i ``=``=` `n``-``1` `: ` `        ``return` `1` ` `  `    ``# Check for decreasing sequence ` `    ``for` `j ``in` `range``(i ``+` `1``, n) : ` `         `  `        ``if` `arr[j] < arr[j ``-` `1``] : ` `            ``continue` `        ``else` `: ` `            ``break` ` `  `    ``i ``=` `j ` `    ``if` `i !``=` `n ``-` `1` `: ` `        ``return` `0` ` `  `    ``return` `1` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``arr ``=` `[``-``3``, ``9``, ``7``, ``20``, ``17``, ``5``, ``1``] ` `     `  `    ``n ``=` `len``(arr) ` ` `  `    ``if` `checkBitonic(arr, n) ``=``=` `1` `: ` `        ``print``(``"YES"``) ` `    ``else` `: ` `        ``print``(``"NO"``) ` ` `  `# This code is contributed ` `# by ANKITRAI1  `

## C#

 `// C# program to check  ` `// if an array is bitonic ` `using` `System; ` ` `  `class` `GFG ` `{ ` `// Function to check if the  ` `// given array is bitonic ` `static` `int` `checkBitonic(``int` `[]arr,  ` `                        ``int` `n)  ` `{ ` `    ``int` `i, j; ` ` `  `    ``// Check for increasing sequence ` `    ``for` `(i = 1; i < n; i++) ` `    ``{ ` `        ``if` `(arr[i] > arr[i - 1]) ` `            ``continue``; ` ` `  `        ``if` `(arr[i] <= arr[i - 1]) ` `            ``break``; ` `    ``} ` ` `  `    ``if` `(i == n - 1) ` `        ``return` `1; ` ` `  `    ``// Check for decreasing sequence ` `    ``for` `(j = i + 1; j < n; j++)  ` `    ``{ ` `        ``if` `(arr[j] < arr[j - 1]) ` `            ``continue``; ` ` `  `        ``if` `(arr[j] >= arr[j - 1]) ` `            ``break``; ` `    ``} ` ` `  `    ``i = j; ` ` `  `    ``if` `(i != n) ` `        ``return` `0; ` ` `  `    ``return` `1; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main()  ` `{ ` `    ``int` `[]arr = { -3, 9, 7, 20, 17, 5, 1 }; ` ` `  `    ``int` `n = arr.Length; ` ` `  `    ``Console.WriteLine(( ` `            ``checkBitonic(arr, n) == 1) ?  ` `                                 ``"YES"` `: ``"NO"``); ` `} ` `} ` ` `  `// This code is contributed by Bilal `

## PHP

 ` ``\$arr``[``\$i` `- 1]) ` `            ``continue``; ` ` `  `        ``if` `(``\$arr``[``\$i``] <= ``\$arr``[``\$i` `- 1]) ` `            ``break``; ` `    ``} ` ` `  `    ``if` `(``\$i` `== ``\$n` `- 1) ` `        ``return` `1; ` ` `  `    ``// Check for decreasing sequence ` `    ``for` `(``\$j` `= ``\$i` `+ 1; ``\$j` `< ``\$n``; ``\$j``++)  ` `    ``{ ` `        ``if` `(``\$arr``[``\$j``] < ``\$arr``[``\$j` `- 1]) ` `            ``continue``; ` ` `  `        ``if` `(``\$arr``[``\$j``] >= ``\$arr``[``\$j` `- 1]) ` `            ``break``; ` `    ``} ` ` `  `    ``\$i` `= ``\$j``; ` ` `  `    ``if` `(``\$i` `!= ``\$n``) ` `        ``return` `0; ` ` `  `    ``return` `1; ` `} ` ` `  `// Driver code ` `\$arr` `= ``array``( -3, 9, 7, 20, 17, 5, 1 ); ` ` `  `\$n` `= sizeof(``\$arr``); ` ` `  `checkBitonic(``\$arr``, ``\$n``) == 1 ?  ` `               ``print``(``"YES"``) : ``print``(``"NO"``); ` ` `  `// This code is contributed by ChitraNayal ` `?> `

Output:

```NO
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.