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:
Find an element in Bitonic array
Next article icon

Program to calculate Bitonicity of an Array

Last Updated : 08 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 element of array B[].

Examples:

Input : arr[] = {1, 2, 3, 4, 5} Output : 4 Input : arr[] = {1, 4, 5, 3, 2} Output : 0

Given that 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 element of array B[].

From the above expression, it can be deduced that: 

  • The bitonicity of the array is initially 0.
  • It increases by 1 on moving to next element if the next element is greater than previous element.
  • It decreases by 1 on moving to next element if the next element is greater than previous element.

The idea is to take a variable [Tex]bt = 0    [/Tex]and start traversing the array, if the next element is greater than current, increment bt by 1 and if the next element is smaller than current then decrement bt by 1. 

Below is the implementation of the above approach: 

C++

// C++ program to find bitonicity
// of an array
#include <iostream>
using namespace std;
 
// Function to find the bitonicity
// of an array
int findBitonicity(int arr[], int n)
{
    int bt = 0;
 
    for (int i = 1; i < n; i++) {
        if (arr[i] > arr[i - 1])
            bt++;
        else if (arr[i] < arr[i - 1])
            bt--;
    }
 
    return bt;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 4, 3 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Bitonicity = " << findBitonicity(arr, n);
 
    return 0;
}
                      
                       

Java

// Java program to find bitonicity
// of an array
import java.util.*;
import java.lang.*;
 
class GFG
{
     
// Function to find the bitonicity
// of an array
static int findBitonicity(int[] arr,
                          int n)
{
    int bt = 0;
 
    for (int i = 1; i < n; i++)
    {
        if (arr[i] > arr[i - 1])
            bt++;
        else if (arr[i] < arr[i - 1])
            bt--;
    }
 
    return bt;
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 4, 3 };
 
    int n = arr.length;
 
    System.out.print("Bitonicity = " +
              findBitonicity(arr, n));
}
}
 
// This code is contributed
// by Akanksha Rai
                      
                       

Python3

# Python3 program to find bitonicity
# of an array
 
# Function to find the bitonicity
# of an array
def findBitonicity(arr, n):
    bt = 0
 
    for i in range(1, n, 1):
        if (arr[i] > arr[i - 1]):
            bt += 1
        elif (arr[i] < arr[i - 1]):
            bt -= 1
 
    return bt
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6, 4, 3]
 
    n = len(arr)
 
    print("Bitonicity =",
           findBitonicity(arr, n))
 
# This code is contributed by
# Surendra_Gangwar
                      
                       

C#

// C# program to find bitonicity
// of an array
using System;
 
class GFG
{
// Function to find the bitonicity
// of an array
static int findBitonicity(int[] arr,
                          int n)
{
    int bt = 0;
 
    for (int i = 1; i < n; i++)
    {
        if (arr[i] > arr[i - 1])
            bt++;
        else if (arr[i] < arr[i - 1])
            bt--;
    }
 
    return bt;
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 2, 3, 4, 5, 6, 4, 3 };
 
    int n = arr.Length;
 
    Console.Write("Bitonicity = " +
                  findBitonicity(arr, n));
}
}
 
// This code is contributed
// by Akanksha Rai
                      
                       

PHP

<?php
// PHP program to find bitonicity
// of an array
 
// Function to find the bitonicity
// of an array
function findBitonicity(&$arr, $n)
{
    $bt = 0;
 
    for ($i = 1; $i < $n; $i++)
    {
        if ($arr[$i] > $arr[$i - 1])
            $bt++;
        else if ($arr[$i] < $arr[$i - 1])
            $bt--;
    }
 
    return $bt;
}
 
// Driver Code
$arr = array(1, 2, 3, 4, 5, 6, 4, 3 );
 
$n = sizeof($arr);
 
echo ("Bitonicity = ");
echo findBitonicity($arr, $n);
 
// This code is contributed
// by Shivi_Aggarwal
?>
                      
                       

Javascript

<script>
 
// Javascript program to find bitonicity
// of an array   
 
// Function to find the bitonicity
// of an array
    function findBitonicity(arr , n)
    {
        var bt = 0;
 
        for (i = 1; i < n; i++) {
            if (arr[i] > arr[i - 1])
                bt++;
            else if (arr[i] < arr[i - 1])
                bt--;
        }
 
        return bt;
    }
 
    // Driver Code
     
        var arr = [ 1, 2, 3, 4, 5, 6, 4, 3 ];
 
        var n = arr.length;
 
        document.write("Bitonicity = " + findBitonicity(arr, n));
 
// This code contributed by gauravrajput1
 
</script>
                      
                       

Output
Bitonicity = 3

Complexity Analysis:

  • Time Complexity: O(N), as we are using a loop to traverse N times for finding the bitonicity of the array.
  • Auxiliary Space: O(1), as we are not using any extra space complexity.


Next Article
Find an element in Bitonic array

S

Striver
Improve
Article Tags :
  • Arrays
  • DSA
  • bitonic
Practice Tags :
  • Arrays

Similar Reads

  • 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 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
    6 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
  • Queries to calculate Bitwise OR of an array with updates
    Given an array arr[ ] consisting of N positive integers and a 2D array Q[][] consisting of queries of the form {i, val}, the task for each query is to replace arr[i] by val and calculate the Bitwise OR of the modified array. Examples: Input: arr[ ]= {1, 2, 3}, Q[ ][] = {{1, 4}, {3, 0}}Output: 7 6Exp
    10 min read
  • Queries to calculate Bitwise AND of an array with updates
    Given an array arr[] consisting of N positive integers and a 2D array Q[][] consisting of queries of the type {i, val}, the task for each query is to replace arr[i] by val and calculate the Bitwise AND of the modified array. Examples: Input: arr[] = {1, 2, 3, 4, 5}, Q[][] = {{0, 2}, {3, 3}, {4, 2}}O
    15+ min read
  • Find an element in Bitonic array
    Given a bitonic sequence of n distinct elements, and an integer x, the task is to write a program to find given element x in the bitonic sequence in O(log n) time. A Bitonic Sequence is a sequence of numbers that is first strictly increasing then after a point decreasing. Examples: Input : arr[] = {
    11 min read
  • Bitwise XOR of a Binary array
    Given a binary array arr[], the task is to calculate the bitwise XOR of all the elements in this array and print it. Examples: Input: arr[] = {“100”, “1001”, “0011”} Output: 1110 0100 XOR 1001 XOR 0011 = 1110 Input: arr[] = {“10”, “11”, “1000001”} Output: 1000000 Approach: Step 1: First find the max
    7 min read
  • Count of pairs in an Array with same number of set bits
    Given an array arr containing N integers, the task is to count the possible number of pairs of elements with the same number of set bits. Examples: Input: N = 8, arr[] = {1, 2, 3, 4, 5, 6, 7, 8} Output: 9 Explanation: Elements with 1 set bit: 1, 2, 4, 8 Elements with 2 set bits: 3, 5, 6 Elements wit
    7 min read
  • Sort an array according to count of set bits
    Given an array of integers, sort the array (in descending order) according to count of set bits in binary representation of array elements. Note: For integers having same number of set bits in their binary representation, sort according to their position in the original array i.e., a stable sort. Ex
    15+ min read
  • Count total set bits in an array
    Given an array arr, the task is to count the total number of set bits in all numbers of that array arr. Example: Input: arr[] = {1, 2, 5, 7}Output: 7Explanation: Number of set bits in {1, 2, 5, 7} are {1, 1, 2, 3} respectively Input: arr[] = {0, 4, 9, 8}Output: 4 Approach: Follow the below steps to
    5 min read
  • Bitwise AND of all the elements of array
    Given an array, arr[] of N integers, the task is to find out the bitwise AND(&) of all the elements of the array. Examples: Input: arr[] = {1, 3, 5, 9, 11} Output: 1 Input: arr[] = {3, 7, 11, 19, 11} Output: 3 Approach: The idea is to traverse all the array elements and compute the bitwise AND f
    4 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