# Program to calculate Bitonicity of an Array

Given an array of 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
```

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

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 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 ` `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); ` ` `  `    ``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

 ` ``\$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 ` `?> `

Output:

```Bitonicity = 3
```

My Personal Notes arrow_drop_up Striver(underscore)79 at Codechef and codeforces D

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 :

Be the First to upvote.

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