Related Articles

# Convert an array into Bitonic array by right shifting array elements

• Last Updated : 03 May, 2021

Giver an array arr[] consisting of N integers, the task is to perform right shift operations on array elements to convert the given array into a bitonic array.

Examples:

Input: arr[] = {7, 3, 4, 5, 3}
Output: 56 96 128 80 48
Explanation:
Perform the operation on the array elements as:

• 7 → 00000111 → 3 right shifts → 00111000 → 56
• 3 → 00000011 → 5 right shifts → 01100000 → 96
• 4 → 00000100 → 5 right shifts → 10000000 → 128
• 5 → 00000101 → 4 right shifts → 01010000 → 80
• 3 → 00000011 → 4 right shifts → 00110000 → 48

After the above operations the modified array is {56, 96, 128, 80, 48}, which is Bitonic array.

Input: arr[] = {255, 243, 23, 141, 46}
Output: -1

Approach: Follow the given steps to solve the problem

Below is the implementation of the above approach:

## Python3

 `# Python program for the above approach`` ` `# Function to check if an``# array arr[] is Bitonic or not``def` `isPeak(arr):`` ` `    ``# Traverse the first half of arr[]``    ``for` `i ``in` `range``(``len``(arr)``/``/``2``, ``len``(arr)``-``1``):``        ``if` `arr[i] < arr[i ``+` `1``]:``            ``return` `False`` ` `    ``# Traverse the second half of arr[]``    ``for` `i ``in` `range``(``len``(arr)``/``/``2``):``        ``if` `arr[i] > arr[i ``+` `1``]:``            ``return` `False``           ` `    ``# Return true if arr[] is bitonic``    ``return` `True`` ` `# Function to maximize the value of N``# by performing right shift operations``def` `maximize(n):``   ` `    ``Ele ``=` `n``    ``ans ``=` `n``     ` `    ``for` `idx ``in` `range``(``7``):``       ` `        ``# Right shift by 1``        ``if` `Ele & ``1``:``            ``Ele >>``=` `1``            ``Ele ``+``=` `2``*``*``32``        ``else``:``            ``Ele >>``=` `1``         ` `        ``# Update the value of ans``        ``ans ``=` `max``(ans, Ele)``         ` `    ``return` `ans`` ` `# Function to arrange array in descending``# order by right shift operations``def` `makeDec(arr):``   ` `    ``# Maximise the array arr``    ``prev ``=` `maximize(arr[``0``])``    ``arr[``0``] ``=` `prev``     ` `    ``# Iterate through array arr[]``    ``for` `i ``in` `range``(``1``, ``len``(arr)):``       ` `        ``optEle ``=` `arr[i]``         ` `        ``# Flag to find the first``        ``# element less than prev``        ``flag ``=` `True``         ` `        ``# Update Ele as arr[i]``        ``Ele ``=` `arr[i]``         ` `        ``for` `idx ``in` `range``(``7``):``           ` `            ``# Right shift by 1``            ``if` `Ele & ``1``:``                ``Ele >>``=` `1``                ``Ele ``+``=` `2``*``*``32``            ``else``:``                ``Ele >>``=` `1``                 ` `           ` `            ``if` `Ele < prev ``and` `flag:``               ` `                  ``# Update the optEle``                ``optEle ``=` `Ele``                 ` `                ``# Unset the flag``                ``flag ``=` `False``                 ` `            ``if` `Ele < prev:``               ` `                  ``# Update the optEle``                ``optEle ``=` `max``(optEle, Ele)``       ` `          ``# Update the array arr[i]``        ``arr[i] ``=` `optEle``         ` `        ``# Update the value of prev``        ``prev ``=` `arr[i]``     ` `    ``# Return the array``    ``return` `arr`` ` `# Function to find the peak``# element of the array arr[]``def` `makePeak(arr):``   ` `    ``# First half of the array``    ``first ``=` `arr[:``len``(arr)``/``/``2` `+` `1``]``    ``first.reverse()``     ` `    ``# Second half of the array``    ``second ``=` `arr[``len``(arr)``/``/``2``:]``     ` `    ``# Merg both halves``    ``ans ``=` `makeDec(first)[::``-``1``] ``+` `makeDec(second)[``1``:]``     ` `    ``# Function Call``    ``if` `isPeak(ans):``        ``return` `ans``       ` `    ``return` `-``1`` ` ` ` `# Driver Code`` ` `# Given array``arr ``=` `[``7``, ``3``, ``4``, ``5``, ``3``]`` ` `# Function Call``print``(makePeak(arr))`
Output:
```[1879048192, 3221225472, 4294967296, 2684354560, 1610612736]
```

Time Complexity: O(N * log(M)),  where M is the maximum element of arr[] .
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up