Program to check if an array is bitonic or not
Last Updated :
22 Dec, 2022
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 and 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
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++
#include <bits/stdc++.h>
using namespace std;
int checkBitonic( int arr[], int n)
{
int i, j;
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;
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;
}
int main()
{
int arr[] = { 1,2,3 };
int n = sizeof (arr) / sizeof (arr[0]);
(checkBitonic(arr, n) == 1) ? cout << "YES"
: cout << "NO" ;
return 0;
}
|
Java
class GFG
{
static int checkBitonic( int arr[], int n)
{
int i, j;
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 ;
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 ;
}
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" );
}
}
|
Python3
def checkBitonic(arr, n) :
for i in range ( 1 , n) :
if arr[i] > arr[i - 1 ] :
continue
else :
break
if i = = n - 1 :
return 1
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
if __name__ = = "__main__" :
arr = [ - 3 , 9 , 7 , 20 , 17 , 5 , 1 ]
n = len (arr)
if checkBitonic(arr, n) = = 1 :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static int checkBitonic( int []arr,
int n)
{
int i, j;
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;
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;
}
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" );
}
}
|
PHP
<?php
function checkBitonic(& $arr , $n )
{
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;
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;
}
$arr = array ( -3, 9, 7, 20, 17, 5, 1 );
$n = sizeof( $arr );
checkBitonic( $arr , $n ) == 1 ?
print ( "YES" ) : print ( "NO" );
?>
|
Javascript
<script>
function checkBitonic(arr,n)
{
let i, j;
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;
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;
}
let arr = [ -3, 9, 7, 20, 17, 5, 1 ];
let n = arr.length;
document.write((checkBitonic(arr, n) == 1) ?
"YES" : "NO" );
</script>
|
Time Complexity: O(n), As we are traversing the array only once.
Auxiliary Space: O(1), As constant extra space is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...