📜  通过右移数组元素将数组转换为双音数组

📅  最后修改于: 2021-09-06 05:32:59             🧑  作者: Mango

给定一个由N 个整数组成的数组 arr[] ,任务是对数组元素进行右移操作,将给定的数组转换为双调数组。

例子:

方法:按照给定的步骤解决问题

  • 使用右移操作最大化数组的中间元素,即arr[N / 2] 。
  • 遍历给定的数组arr[],通过对数组元素执行右移操作(如果需要)将数组的前半部分升序转换。
  • 遍历给定的数组arr[],通过对数组元素的右移操作(如果需要)按降序转换数组的后半部分。
  • 完成上述步骤后,检查数组是否是双音的。然后打印数组arr[]作为结果数组。
  • 否则,打印“-1”

下面是上述方法的实现:

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[0]
    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))


输出:
[1879048192, 3221225472, 4294967296, 2684354560, 1610612736]

时间复杂度: O(N * log(M)),其中 M 是arr[]的最大元素。
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live