给定一个由N个正整数组成的数组arr [] ,任务是通过旋转任意次数的二进制表示形式,找到位于数组偶数索引和奇数索引处的数组元素之和之间的最大绝对差。仅考虑8位表示。
例子:
Input: arr[] = {123, 86, 234, 189}
Output: 326
Explanation:
Following are the rotation of elements:
- For arr[0] (= 123): arr[0] = (123)10 = (1111011)2. Rotate the bits to the right twice to make arr[0] = (1111100)2 = (246)10.
- For arr[1] (= 86): arr[1] = (86)10 = (1010110)2. Rotate the bits to the right once to make arr[1] = (0101011)2 = (43)10.
- For element arr[3](=189): arr[3] = (189)10 = (10111101)2. Rotate the bits once to the left to make arr[3] = (011111011)2 = (111)10.
Therefore, the array arr[] modifies to {246, 43, 234, 111}. The maximum absolute difference = (246 + 234) – (43 + 111) = 326.
Input: arr[] = {211, 122, 212, 222}, N = 4
Output: 376
方法:通过旋转每个数组元素的二进制表示并找到最大差异,可以通过最小化偶数或奇数索引的元素并最大化其他索引的元素来解决给定的问题。请按照以下步骤解决问题:
- 定义一个函数Rotate(X,f),以在旋转任意数字的二进制表示形式的位之后找到一个数字的最大和最小值。
- 初始化两个变量,例如maxi = X和mini = X,以存储可能的数字X的最大值和最小值。
- 遍历数字X的位,然后通过执行以下操作旋转X的位:
- 如果X为奇数,则将X的值更新为X >> 1且X = X | (1 << 7) 。
- 否则,将X的值更新为X >> 1 。
- 将maxi的值更新为maxi和X的最大值。
- 将mini的值更新为mini和X的最小值。
- 如果f的值为1 ,则返回maxi 。否则,返回mini。
- 现在,找到通过最大化放置在偶数索引处的元素并最小化放置在奇数索引处的元素而获得的差异,并将该差异存储在变量中,例如caseOne 。
- 现在,找到通过最小化放置在偶数索引处的元素并最大化放置在奇数索引处的元素而获得的差异,并将该差异存储在变量中,例如caseTwo 。
- 完成上述步骤后,打印出caseOne和caseTwo的最大值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find maximum and
// minimum value of a number that
// can be obtained by rotating bits
int Rotate(int n, int f)
{
// Stores the value of N
int temp = n;
// Stores the maximum value
int maxi = n;
// Stores the minimum value
int mini = n;
for (int idx = 0; idx < 7; idx++) {
// If temp is odd
if (temp & 1) {
temp >>= 1;
temp += pow(2, 7);
}
else
temp >>= 1;
// Update the maximum
// and the minimum value
mini = min(mini, temp);
maxi = max(maxi, temp);
}
// If flag is 1, then
// return the maximum value
if (f)
return (maxi);
// Otherwise, return
// the maximum value
else
return (mini);
}
// Function to find the maximum difference
// between the sum of odd and even-indexed
// array elements possible by rotating bits
int calcMinDiff(int arr[], int n)
{
// Stores the maximum difference
int caseOne = 0;
// Stores the sum of elements
// present at odd indices
int sumOfodd = 0;
// Stores the sum of elements
// present at even indices
int sumOfeven = 0;
// Traverse the given array
for (int i = 0; i < n; i++) {
// If the index is even
if (i % 2)
sumOfodd += Rotate(arr[i], 0);
else
sumOfeven += Rotate(arr[i], 1);
}
// Update the caseOne
caseOne = abs(sumOfodd - sumOfeven);
// Stores the maximum diffrence
int caseTwo = 0;
// Stores the sum of elements
// placed at odd positions
sumOfodd = 0;
// Stores the sum of elements
// placed at even positions
sumOfeven = 0;
// Traverse the array
for (int i = 0; i < n; i++)
{
// If the index is even
if (i % 2)
sumOfodd += Rotate(arr[i], 1);
else
sumOfeven += Rotate(arr[i], 0);
}
// Update the caseTwo
caseTwo = abs(sumOfodd - sumOfeven);
// Return the maximum of caseOne
// and caseTwo
return max(caseOne, caseTwo);
}
// Driver Code
int main()
{
int arr[] = { 123, 86, 234, 189 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << (calcMinDiff(arr, n));
}
// This code is contributed by ukasp.
Python3
# Python program for the above approach
# Function to find maximum and
# minimum value of a number that
# can be obtained by rotating bits
def Rotate(n, f):
# Stores the value of N
temp = n
# Stores the maximum value
maxi = n
# Stores the minimum value
mini = n
for idx in range(7):
# If temp is odd
if temp & 1:
temp >>= 1
temp += 2**7
else:
temp >>= 1
# Update the maximum
# and the minimum value
mini = min(mini, temp)
maxi = max(maxi, temp)
# If flag is 1, then
# return the maximum value
if(f):
return (maxi)
# Otherwise, return
# the maximum value
else:
return (mini)
# Function to find the maximum difference
# between the sum of odd and even-indexed
# array elements possible by rotating bits
def calcMinDiff(arr):
# Stores the maximum difference
caseOne = 0
# Stores the sum of elements
# present at odd indices
sumOfodd = 0
# Stores the sum of elements
# present at even indices
sumOfeven = 0
# Traverse the given array
for i in range(len(arr)):
# If the index is even
if i % 2:
sumOfodd += Rotate(arr[i], 0)
else:
sumOfeven += Rotate(arr[i], 1)
# Update the caseOne
caseOne = abs(sumOfodd - sumOfeven)
# Stores the maximum diffrence
caseTwo = 0
# Stores the sum of elements
# placed at odd positions
sumOfodd = 0
# Stores the sum of elements
# placed at even positions
sumOfeven = 0
# Traverse the array
for i in range(len(arr)):
# If the index is even
if i % 2:
sumOfodd += Rotate(arr[i], 1)
else:
sumOfeven += Rotate(arr[i], 0)
# Update the caseTwo
caseTwo = abs(sumOfodd - sumOfeven)
# Return the maximum of caseOne
# and caseTwo
return max(caseOne, caseTwo)
# Driver Code
arr = [123, 86, 234, 189]
print(calcMinDiff(arr))
输出:
326
时间复杂度: O(N)
辅助空间: O(1)