📌  相关文章
📜  重新排列数组以使已排序的数组元素的反向二进制表示的十进制等效

📅  最后修改于: 2021-09-02 07:30:16             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是重新排列数组,以便对所有数组元素的反向二进制表示进行排序。

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个数组,比如newArr[] ,插入所有数组元素作为数字的反向二进制表示。
  • 按升序对数组newArr[]进行排序。
  • 遍历数组newArr[]并将所有元素复制到数组arr[]
  • 现在,将所有数组元素更新为数字的反向二进制表示。
  • 完成上述步骤后,打印数组arr[]的元素作为输出。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to reverse the bits of a number
int keyFunc(int n)
{
 
    // Stores the reversed number
    int rev = 0;
 
    while (n > 0)
    {
 
        // Divide rev by 2
        rev = rev << 1;
 
        // If the value of N is odd
        if (n & 1 == 1)
            rev = rev ^ 1;
 
        // Update the value of N
        n = n >> 1;
      }
 
    // Return the final value of rev
    return rev;
}
 
// Function for rearranging the array
// element according to the given rules
vector> getNew(vector arr)
{
 
    // Stores the new array elements
    vector> ans;
 
    for (int i:arr)
        ans.push_back({keyFunc(i), i});
 
    return ans;
}
 
// Function for rearranging the array
vector getArr(vector > arr){
 
    // Stores the new array
    vector ans;
 
    for (auto i:arr)
        ans.push_back(i[1]);
    return ans;
}
 
 
// Function to sort the array by reversing
// binary representation
void sortArray(vector arr)
{
 
  // Creating a new array
  vector > newArr = getNew(arr);
 
  // Sort the array with the key
  sort(newArr.begin(),newArr.end());
 
  // Get arr from newArr
  arr = getArr(newArr);
 
  // Print the sorted array
  int n = arr.size();
  cout<<"[";
  for(int i = 0; i < n - 1; i++)
    cout << arr[i] << ", ";
  cout << arr[n - 1] << "]";
}
 
// Driver Code
int main()
{
   
  vector arr = {43, 52, 61, 41};
  sortArray(arr);
 
  return 0;
}
 
// This code is contributed by mohit kumar 29.


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to reverse the bits of a number
static int keyFunc(int n)
{
     
    // Stores the reversed number
    int rev = 0;
 
    while (n > 0)
    {
         
        // Divide rev by 2
        rev = rev << 1;
 
        // If the value of N is odd
        if ((n & 1) == 1)
            rev = rev ^ 1;
 
        // Update the value of N
        n = n >> 1;
    }
 
    // Return the final value of rev
    return rev;
}
 
// Function for rearranging the array
// element according to the given rules
static int[][] getNew(int arr[])
{
     
    // Stores the new array elements
    int ans[][] = new int[arr.length][2];
 
    for(int i = 0; i < arr.length; i++)
        ans[i] = new int[]{ keyFunc(arr[i]), arr[i] };
 
    return ans;
}
 
// Function for rearranging the array
static int[] getArr(int[][] arr)
{
 
    // Stores the new array
    int ans[] = new int[arr.length];
    int idx = 0;
    for(int i[] : arr)
        ans[idx++] = i[1];
         
    return ans;
}
 
// Function to sort the array by reversing
// binary representation
static void sortArray(int arr[])
{
 
    // Creating a new array
    int[][] newArr = getNew(arr);
 
    // Sort the array with the key
    Arrays.sort(newArr, (a, b) -> {
        if (Integer.compare(a[0], b[0]) == 0)
            return Integer.compare(a[1], b[1]);
             
        return Integer.compare(a[0], b[0]);
    });
 
    // Get arr from newArr
    arr = getArr(newArr);
 
    // Print the sorted array
    int n = arr.length;
    System.out.print("[");
    for(int i = 0; i < n - 1; i++)
        System.out.print(arr[i] + ", ");
         
    System.out.print(arr[n - 1] + "]");
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 43, 52, 61, 41 };
    sortArray(arr);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to reverse the bits of a number
def keyFunc(n):
 
    # Stores the reversed number
    rev = 0
 
    while (n > 0):
 
        # Divide rev by 2
        rev = rev << 1
 
        # If the value of N is odd
        if (n & 1 == 1):
            rev = rev ^ 1
 
        # Update the value of N
        n = n >> 1
 
    # Return the final value of rev
    return rev
 
# Function for rearranging the array
# element according to the given rules
def getNew(arr):
 
    # Stores the new array elements
    ans = []
 
    for i in arr:
        ans.append([keyFunc(i), i])
    return ans
 
# Function for rearranging the array
def getArr(arr):
 
    # Stores the new array
    ans = []
 
    for i in arr:
        ans.append(i[1])
    return ans
 
# Function to sort the array by reversing
# the binary representation
def sortArray(arr):
 
    # Creating a new array
    newArr = getNew(arr)
 
    # Sort the array with the key
    newArr.sort()
 
    # Get arr from newArr
    arr = getArr(newArr)
 
    # Print the sorted array
    print(arr)
 
 
# Driver Code
 
arr = [43, 52, 61, 41]
sortArray(arr)


Python3
# Python3 program for the above approach
 
# Function to reverse the bits of number
def keyFunc(n):
   
    # Stores the reversed number
    rev = 0
     
    while (n > 0):
       
        # Divide rev by 2
        rev = rev << 1
         
        # If the value of N is odd
        if (n & 1 == 1):
            rev = rev ^ 1
             
        # Update the value of N
        n = n >> 1
         
    # Return the final value of rev
    return rev
 
# Function to sort the array by reversing
# binary representation
def sortArray(arr):
 
    # Sort the array with the key
    arr = sorted(arr, key = keyFunc)
 
    # Print the sorted array
    print(arr)
 
# Driver Code
 
arr = [43, 52, 61, 41]
sortArray(arr)


输出:
[52, 41, 61, 43]

时间复杂度: O(N * log N)
辅助空间: O(N)

空间优化方法:按照以下步骤解决问题:

  • 声明一个函数以接受数组元素作为参数并返回其反向二进制表示的十进制等效值。
  • 排序时使用函数作为键对数组进行排序。
  • 打印数组元素作为输出。

下面是上述方法的实现:

蟒蛇3

# Python3 program for the above approach
 
# Function to reverse the bits of number
def keyFunc(n):
   
    # Stores the reversed number
    rev = 0
     
    while (n > 0):
       
        # Divide rev by 2
        rev = rev << 1
         
        # If the value of N is odd
        if (n & 1 == 1):
            rev = rev ^ 1
             
        # Update the value of N
        n = n >> 1
         
    # Return the final value of rev
    return rev
 
# Function to sort the array by reversing
# binary representation
def sortArray(arr):
 
    # Sort the array with the key
    arr = sorted(arr, key = keyFunc)
 
    # Print the sorted array
    print(arr)
 
# Driver Code
 
arr = [43, 52, 61, 41]
sortArray(arr)
输出:
[52, 41, 61, 43]

时间复杂度: O(N * log N)
辅助空间: O(1)

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