# Number of bitonic arrays of length n and consisting of elements from 1 to n

For a given number n (n > 1), we need to find the number of ways you can make bitonic array of length n, consisting of all elements from 1 to n.

Note: [1, 2,…n] and [n, n – 1…2, 1] are not considered as bitonic array.

**Examples :**

Input : n = 3 Output : 2 Explanation : [1, 3, 2] & [2, 3, 1] are only two ways of bitonic array formation for n = 3. Input : n = 4 Output : 6

For creation of a bitonic array, let’s say that we have an empty array of length n and we want to put the numbers from 1 to n in this array in bitonic form, now let’s say we want to add the number 1, we have only 2 possible ways to put the number 1, both are the end positions because if we should put 1 at any place other than end points then number on both side of 1 are greater than 1. After that we can imagine that we have an array of length n-1 and now we want to put the number 2, again for the same reasons we have two ways and so on, until we want to put the number n, we will only have 1 way instead of 2, so we have n-1 numbers that have 2 ways to put, so by multiplication rule of combinatorics the answer is 2^n-1, finally we should subtract 2 from the answer because permutations 1 2 3 4 …. n and n n-1 … 3 2 1 should not be counted.

## C++

`// C++ program for finding ` `// total bitonic array ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate no. of ways ` `long` `long` `int` `maxWays( ` `int` `n) ` `{ ` ` ` `// return (2^(n - 1)) -2 ` ` ` `return` `(` `pow` `(2, n - 1) - 2); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` `cout << maxWays(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for finding ` `// total bitonic array ` `class` `GFG ` `{ ` ` ` ` ` `// Function to calculate no. of ways ` ` ` `static` `int` `maxWays( ` `int` `n) ` ` ` `{ ` ` ` ` ` `// return (2 ^ (n - 1)) -2 ` ` ` `return` `(` `int` `)(Math.pow(` `2` `, n - ` `1` `) - ` `2` `); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `6` `; ` ` ` ` ` `System.out.print(maxWays(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# python program for finding ` `# total bitonic array ` ` ` `# Function to calculate no. of ways ` `def` `maxWays(n): ` ` ` ` ` `# return (2^(n - 1)) -2 ` ` ` `return` `(` `pow` `(` `2` `, n ` `-` `1` `) ` `-` `2` `); ` ` ` `# Driver Code ` `n ` `=` `6` `; ` `print` `(maxWays(n)) ` ` ` `# This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for finding ` `// total bitonic array ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to calculate no. of ways ` ` ` `static` `int` `maxWays( ` `int` `n) ` ` ` `{ ` ` ` ` ` `// return (2 ^ (n - 1)) -2 ` ` ` `return` `(` `int` `)(Math.Pow(2, n - 1) - 2); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `Console.Write(maxWays(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for finding ` `// total bitonic array ` ` ` `// Function to calculate ` `// no. of ways ` `function` `maxWays( ` `$n` `) ` `{ ` ` ` ` ` `// return (2^(n - 1)) -2 ` ` ` `return` `(pow(2, ` `$n` `- 1) - 2); ` `} ` ` ` `// Driver Code ` `$n` `= 6; ` `echo` `maxWays(` `$n` `); ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

Output:

30

This article is contributed by **Shivam Pradhan (anuj_charm)**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Count arrays of length K whose product of elements is same as that of given array
- Number of arrays of size N whose elements are positive integers and sum is K
- Print all the permutation of length L using the elements of an array | Iterative
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed
- Longest Bitonic Subsequence in O(n log n)
- Maximum sum bitonic subarray
- Program to check if an array is bitonic or not
- Printing Longest Bitonic Subsequence
- Number of N length sequences whose product is M
- Queries on insertion of an element in a Bitonic Sequence
- Number of ways to cut a stick of length N into K pieces
- Count number of binary strings of length N having only 0's and 1's
- Number of strings of length N with no palindromic sub string
- Number of Binary Trees for given Preorder Sequence length
- Number of ways of choosing K equal substrings of any length for every query