Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

For a given number n (n > 1), we need to find the number of ways you can make a 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 the 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 endpoints then the number on both side of 1 is 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++


                    

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.

                    

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

                    

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.

                    

PHP


                    

Javascript

<div id="highlighter_192855" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="plain"><script></code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="comments">// Javascript program for finding </code></div><div class="line number4 index3 alt1"><code class="comments">// total bitonic array</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="comments">// Function to calculate no. of ways</code></div><div class="line number7 index6 alt2"><code class="keyword">function</code> <code class="plain">maxWays( n)</code></div><div class="line number8 index7 alt1"><code class="plain">{</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">    </code><code class="comments">// return (2^(n - 1)) -2 </code></div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="keyword">return</code> <code class="plain">(Math.pow(2, n - 1) - 2);</code></div><div class="line number11 index10 alt2"><code class="plain">}</code></div><div class="line number12 index11 alt1"> </div><div class="line number13 index12 alt2"><code class="comments">// Driver Code</code></div><div class="line number14 index13 alt1"><code class="keyword">var</code> <code class="plain">n = 6;</code></div><div class="line number15 index14 alt2"><code class="plain">document.write( maxWays(n));</code></div><div class="line number16 index15 alt1"> </div><div class="line number17 index16 alt2"><code class="plain"></script></code></div></div></td></tr></tbody></table></div>

                    

Output:  

30

Time Complexity: O(log n)
Auxiliary Space: O(1)




Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads