Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Check if a given array is pairwise sorted or not
Next article icon

Program to check if an array is bitonic or not

Last Updated : 22 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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++




// C++ program to check if an array is bitonic
#include <bits/stdc++.h>
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[] = { 1,2,3 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    (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




<?php
// PHP program to check if
// an array is bitonic
 
// Function to check if the
// given array is bitonic
function checkBitonic(&$arr, $n)
{
 
    // 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
$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
?>
 
 

Javascript




<script>
 
 
// Java Script program to check
// if an array is bitonic
 
// Function to check if the
// given array is bitonic
function checkBitonic(arr,n)
{
    let 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
 
    let arr = [ -3, 9, 7, 20, 17, 5, 1 ];
 
    let n = arr.length;
 
    document.write((checkBitonic(arr, n) == 1) ?
                                            "YES" : "NO");
 
// This code is contributed by sravan kumar
</script>
 
 
Output
YES

Time Complexity: O(n), As we are traversing the array only once.
Auxiliary Space: O(1), As constant extra space is used.



Next Article
Check if a given array is pairwise sorted or not

B

barykrg
Improve
Article Tags :
  • Arrays
  • DSA
  • School Programming
  • bitonic
Practice Tags :
  • Arrays

Similar Reads

  • Program to calculate Bitonicity of an Array
    Given an array of [Tex]N [/Tex]integers. The task is to find the Bitonicity of the given array.The Bitonicity of an array arr[] can be defined as: B[i] = 0, if i = 0. = B[i-1] + 1, if arr[i] > arr[i-1] = B[i-1] - 1, if arr[i] < arr[i-1] = B[i-1], if arr[i] = arr[i-1] Bitonicity will be last el
    5 min read
  • Program to check if a matrix is Binary matrix or not
    Given a matrix, the task is to check if that matrix is a Binary Matrix. A Binary Matrix is a matrix in which all the elements are either 0 or 1. It is also called Logical Matrix, Boolean Matrix, Relation Matrix. Examples: Input: {{1, 0, 1, 1}, {0, 1, 0, 1} {1, 1, 1, 0}} Output: Yes Input: {{1, 0, 1,
    5 min read
  • Check if a Matrix is Reverse Bitonic or Not
    Given a matrix m[][], the task is to check if the given matrix is Reverse Bitonic or not. If the given matrix is Reverse Bitonic, then print Yes. Otherwise, print No. If all the rows and the columns of the given matrix have elements in one of the following orders: Strictly increasingStrictly decreas
    9 min read
  • Check if a Matrix is Bitonic or not
    Given a matrix m[][], the task is to check if the given matrix is Bitonic or not. If the given matrix is Bitonic, then print YES. Otherwise, print NO. If all the rows and the columns of the given matrix have elements in one of the following orders: Strictly increasingStrictly decreasingStrictly incr
    8 min read
  • Check if a given array is pairwise sorted or not
    An array is considered pairwise sorted if each successive pair of numbers is in sorted (non-decreasing) order. In case of odd elements, last element is ignored and result is based on remaining even number of elements. Examples: Input : arr[] = {10, 15, 9, 9, 1, 5}; Output : Yes Pairs are (10, 15), (
    5 min read
  • Check if a given string is a Reverse Bitonic String or not
    Given a string str, the task is to check if that string is a Reverse Bitonic string or not. If the string str is reverse Bitonic string, then print "YES". Otherwise, print "NO". A Reverse Bitonic String is a string in which the characters are arranged in decreasing order followed by increasing order
    6 min read
  • Check if an array represents Inorder of Binary Search tree or not
    Given an array of n elements. The task is to check if it is an Inorder traversal of any Binary Search Tree or not. Examples: Input: arr[] = { 19, 23, 25, 30, 45 }Output: trueExplanation: As the array is sorted in non-decreasing order, it is an Inorder traversal of any Binary Search Tree. Input : arr
    4 min read
  • Program to count number of set bits in an (big) array
    Given an integer array of length N (an arbitrarily large number). How to count number of set bits in the array?The simple approach would be, create an efficient method to count set bits in a word (most prominent size, usually equal to bit length of processor), and add bits from individual elements o
    12 min read
  • Check if original Array Sum is Odd or Even using Bitwise AND of Array
    Given an integer N denoting the size of an array and the bitwise AND (K) of all elements of the array. The task is to determine whether the total sum of the elements is odd or even or cannot be determined. Examples: Input: N = 1, K = 11Output: OddExplanation: As there is only one element in the arra
    6 min read
  • Check whether a given array is a k sorted array or not
    Given an array of n distinct elements. Check whether the given array is a k sorted array or not. A k sorted array is an array where each element is at most k distances away from its target position in the sorted array. For example, let us consider k is 2, an element at index 7 in the sorted array, c
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences