给定一个由N 个整数组成的数组arr[] ,任务是通过将每个数组元素替换为通过以下方式获得的数字来修改数组 反转它们各自的二进制表示并对修改后的数组进行排序。
例子:
Input: arr[ ] = {43, 422, 132}
Output: 33 53 203
Explanation:
The binary representation of the array elements are {101011, 110100110, 10000100}.
Reversed binary representations are {110101, 011001011, 0010000}.
Equivalent numeric values of the reversed binary representations are {53, 203, 33}.
Sorted order of these elements are {33, 53, 203}.
Input: arr[ ] ={ 98, 43, 66, 83}
Output: 33 35 53 101
处理方法:按照以下步骤解决问题:
- 遍历数组arr[]并将每个数组元素转换为其等效的二进制表示并以二进制字符串的形式存储。
- 反转这些二进制字符串并将反转的二进制字符串转换为等效的十进制。
- 对修改后的数组进行排序。
- 打印排序后的数组。
下面是上述方法的实现:
C++14
// C++ implementation of
// the above approach
#include
using namespace std;
// Function to convert binary
// number to equivalent decimal
int binaryToDecimal(string n)
{
string num = n;
int dec_value = 0;
// Set base value to 1, i.e 2^0
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}
// Function to convert a decimal
// to equivalent binary representation
string decimalToBinary(int n)
{
// Stores the binary representation
string binstr = "";
while (n > 0) {
// Since ASCII value of
// '0', '1' are 48 and 49
binstr += (n % 2 + 48);
n = n / 2;
}
// As the string is already reversed,
// no further reversal is required
return binstr;
}
// Function to convert the reversed binary
// representation to equivalent integer
int reversedBinaryDecimal(int N)
{
// Stores reversed binary
// representation of given decimal
string decimal_to_binar
= decimalToBinary(N);
// Stores equivalent decimal
// value of the binary representation
int binary_to_decimal
= binaryToDecimal(decimal_to_binar);
// Return the resultant integer
return binary_to_decimal;
}
// Utility function to print the sorted array
void printSortedArray(int arr[], int size)
{
// Sort the array
sort(arr, arr + size);
// Traverse the array
for (int i = 0; i < size; i++)
// Print the array elements
cout << arr[i] << " ";
cout << endl;
}
// Utility function to reverse the
// binary representations of all
// array elements and sort the modified array
void modifyArray(int arr[], int size)
{
// Traverse the array
for (int i = 0; i < size; i++) {
// Passing array elements to
// reversedBinaryDecimal function
arr[i] = reversedBinaryDecimal(
arr[i]);
}
// Pass the array to
// the sorted array
printSortedArray(arr, size);
}
// Driver Code
int main()
{
int arr[] = { 98, 43, 66, 83 };
int n = sizeof(arr) / sizeof(arr[0]);
modifyArray(arr, n);
return 0;
}
Python3
# Python3 implementation of
# the above approach
# Function to convert binary
# number to equivalent decimal
def binaryToDecimal(n):
num = n
dec_value = 0
# Set base value to 1, i.e 2^0
base = 1
length = len(num)
for i in range(length - 1, -1, -1):
if (num[i] == '1'):
dec_value += base
base = base * 2
return dec_value
# Function to convert a decimal
# to equivalent binary representation
def decimalToBinary(n):
# Stores the binary representation
binstr = ""
while (n > 0):
# Since ASCII value of
# '0', '1' are 48 and 49
binstr += chr(n % 2 + 48)
n = n // 2
# As the string is already reversed,
# no further reversal is required
return binstr
# Function to convert the reversed binary
# representation to equivalent integer
def reversedBinaryDecimal(N):
# Stores reversed binary
# representation of given decimal
decimal_to_binar = decimalToBinary(N)
# Stores equivalent decimal
# value of the binary representation
binary_to_decimal = binaryToDecimal(
decimal_to_binar)
# Return the resultant integer
return binary_to_decimal
# Utility function to print the sorted array
def printSortedArray(arr, size):
# Sort the array
arr.sort()
# Traverse the array
for i in range(size):
# Print the array elements
print(arr[i], end=" ")
print()
# Utility function to reverse the
# binary representations of all
# array elements and sort the modified array
def modifyArray(arr, size):
# Traverse the array
for i in range(size):
# Passing array elements to
# reversedBinaryDecimal function
arr[i] = reversedBinaryDecimal(arr[i])
# Pass the array to
# the sorted array
printSortedArray(arr, size)
# Driver Code
if __name__ == "__main__":
arr = [ 98, 43, 66, 83 ]
n = len(arr)
modifyArray(arr, n)
# This code is contributed by ukasp
输出:
33 35 53 101
时间复杂度:O(NlogN)
辅助空间: O(log 2 M),其中 M 表示数组中存在的最大元素。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live