给定一个由N 个正整数组成的数组arr[] ,任务是通过交换任何数组的二进制表示中不相等的相邻位来找到放置在数组偶数和奇数索引处的数组元素总和之间的最大绝对差元素任意次数。
例子:
Input: arr[] = {5, 7, 3, 1, 8}
Output: 9
Explanation:
arr[0] = (5)10 = (101)2. Left shift bits to make arr[0] = (110)2 = (6)10.
Therefore, the array arr[] modifies to {5, 7, 3, 1, 8}.
Therefore, the maximum absolute difference = (6 + 3 + 8) – (7 + 1) = 9.
Input: arr[] = {54, 32, 11, 23}
Output: 58
Explanation:
Modify arr[0] to 60 by left shifting the last two set bits of arr[0] (= 54). Therefore, the array arr[] modifies to {60, 32, 11, 23}.
Modify arr[1] to 1 by right shifting all the set bits of arr[1] (= 32). Therefore, the array arr[] modifies to {60, 1, 11, 23}
Modify arr[2] to 14 by left shifting the last three set bits of arr[2] (= 11). Therefore, the array arr[] modifies to {60, 1, 14, 23}.
Modify arr[3] to 15 by right shifting all the set bits of arr[3] (= 23). Therefore, the array arr[] modifies to {60, 1, 14, 15}.
Therefore, the maximum absolute difference = (60 + 14) – (15 + 1) = 58.
方法:这个想法是使用任何设置位可以移动到任何其他位置的观察。请按照以下步骤解决问题:
- 定义一个函数,比如maximize() ,通过移动两个不相等的相邻位来最大化一个数字。
- 定义一个函数,比如minimum() ,通过移动两个不相等的相邻位来最小化一个数字。
- 执行以下操作:
- 初始化一个变量,比如ans ,以存储最小化的值。
- 要最小化数字,请将所有设置位移到右侧位置,将所有未设置位移到左侧位置。
- 遍历范围[0, count of set bit – 1]并将ans更新为ans | 1 .如果i不等于设置位的计数,则将ans左移1 。
- 返回ans的值。
- 首先,找出通过最大化放置在偶数索引处的元素和最小化放置在奇数索引处的元素而获得的差异。将它存储在一个变量中,比如caseOne 。
- 现在,找出通过最小化位于偶数索引处的元素和最大化位于奇数索引处的元素而获得的差异。将它存储在一个变量中,比如caseTwo 。
- 完成上述步骤后,打印caseOne和CaseTwo的最大值。
下面是上述方法的实现:
C++
// CPP program for the above approach
#include
using namespace std;
// Function to count total number
// of bits present in a number
int countBit(int n){
return int(log2(n))+1;
}
// Function to count total
// set bits in a number
int countSetBit(int n){
// Stores the count
// of set bits
int ans = 0;
while(n > 0){
ans += (n & 1);
// Right shift by 1
n >>= 1;
}
// Return the final count
return ans;
}
// Function to find maximum number
// by shifting two unequal bits
int maximize(int n){
// Count set bits in number n
int bits = countBit(n);
int setBits = countSetBit(n);
int ans = 0;
// Iterate the string bits
for(int i = 0; i < bits; i++){
if (i < setBits)
ans |= 1;
if(i != setBits - 1)
ans <<= 1;
}
return ans;
}
// Function to find minimum number
// by shifting two unequal bits
int minimize(int n){
int setBits = countSetBit(n);
int ans = 0;
// Iterate the set bit
for (int i = 0; i < setBits; i++){
ans |= 1;
if (i != setBits - 1)
ans <<= 1;
}
return ans;
}
// Function to find the maximum difference
int maxDiff(vector arr){
// Stores the maximum difference
int caseOne = 0;
// Stores the sum of elements
// placed at odd positions
int SumOfOdd = 0;
// Stores the sum of elements
// placed at even positions
int SumOfeven = 0;
// Traverse the array
for(int i = 0; i < arr.size(); i++){
if (i % 2)
SumOfOdd += minimize(arr[i]);
else
SumOfeven += maximize(arr[i]);
}
// Update CaseOne
caseOne = abs(SumOfOdd - SumOfeven);
// Stores the maximum difference
int caseTwo = 0;
// Assign value O
SumOfOdd = 0;
SumOfeven = 0;
// Traverse the array
for(int i = 0; i < arr.size(); i++)
{
if (i % 2)
SumOfOdd += maximize(arr[i]);
else
SumOfeven += minimize(arr[i]);
}
// Update caseTwo
caseTwo = abs(SumOfOdd - SumOfeven);
// Return maximum of caseOne and CaseTwo
return max(caseOne, caseTwo);
}
// Drivers Code
int main()
{
vector arr{54, 32, 11, 23};
// Function Call
cout<
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count total number
// of bits present in a number
static int countBit(int n){
return (int)((Math.log(n) / Math.log(2))+1);
}
// Function to count total
// set bits in a number
static int countSetBit(int n){
// Stores the count
// of set bits
int ans = 0;
while(n > 0){
ans += (n & 1);
// Right shift by 1
n >>= 1;
}
// Return the final count
return ans;
}
// Function to find maximum number
// by shifting two unequal bits
static int maximize(int n){
// Count set bits in number n
int bits = countBit(n);
int setBits = countSetBit(n);
int ans = 0;
// Iterate the string bits
for(int i = 0; i < bits; i++){
if (i < setBits)
ans |= 1;
if(i != setBits - 1)
ans <<= 1;
}
return ans;
}
// Function to find minimum number
// by shifting two unequal bits
static int minimize(int n){
int setBits = countSetBit(n);
int ans = 0;
// Iterate the set bit
for (int i = 0; i < setBits; i++){
ans |= 1;
if (i != setBits - 1)
ans <<= 1;
}
return ans;
}
// Function to find the maximum difference
static int maxDiff(int[] arr){
// Stores the maximum difference
int caseOne = 0;
// Stores the sum of elements
// placed at odd positions
int SumOfOdd = 0;
// Stores the sum of elements
// placed at even positions
int SumOfeven = 0;
// Traverse the array
for(int i = 0; i < arr.length; i++){
if ((i % 2) != 0)
SumOfOdd += minimize(arr[i]);
else
SumOfeven += maximize(arr[i]);
}
// Update CaseOne
caseOne = Math.abs(SumOfOdd - SumOfeven);
// Stores the maximum difference
int caseTwo = 0;
// Assign value O
SumOfOdd = 0;
SumOfeven = 0;
// Traverse the array
for(int i = 0; i < arr.length; i++)
{
if ((i % 2) != 0)
SumOfOdd += maximize(arr[i]);
else
SumOfeven += minimize(arr[i]);
}
// Update caseTwo
caseTwo = Math.abs(SumOfOdd - SumOfeven);
// Return maximum of caseOne and CaseTwo
return Math.max(caseOne, caseTwo);
}
// Driver code
public static void main(String[] args)
{
int[] arr = {54, 32, 11, 23};
// Function Call
System.out.println(maxDiff(arr));
}
}
// This code is contributed by souravghosh0416.
Python3
# Python program for the above approach
import math
# Function to count total number
# of bits present in a number
def countBit(n):
return int(math.log(n, 2))+1
# Function to count total
# set bits in a number
def countSetBit(n):
# Stores the count
# of set bits
ans = 0
while n:
ans += n & 1
# Right shift by 1
n >>= 1
# Return the final count
return ans
# Function to find maximum number
# by shifting two unequal bits
def maximize(n):
# Count set bits in number n
bits = countBit(n)
setBits = countSetBit(n)
ans = 0
# Iterate the string bits
for i in range(bits):
if i < setBits:
ans |= 1
if i != setBits - 1:
ans <<= 1
return ans
# Function to find minimum number
# by shifting two unequal bits
def minimize(n):
setBits = countSetBit(n)
ans = 0
# Iterate the set bit
for i in range(setBits):
ans |= 1
if i != setBits-1:
ans <<= 1
return ans
# Function to find the maximum difference
def maxDiff(arr):
# Stores the maximum difference
caseOne = 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 i % 2:
SumOfOdd += minimize(arr[i])
else:
SumOfeven += maximize(arr[i])
# Update CaseOne
caseOne = abs(SumOfOdd - SumOfeven)
# Stores the maximum difference
caseTwo = 0
# Assign value O
SumOfOdd = 0
SumOfeven = 0
# Traverse the array
for i in range(len(arr)):
if i % 2:
SumOfOdd += maximize(arr[i])
else:
SumOfeven += minimize(arr[i])
# Update caseTwo
caseTwo = abs(SumOfOdd - SumOfeven)
# Return maximum of caseOne and CaseTwo
return max(caseOne, caseTwo)
# Drivers Code
arr = [54, 32, 11, 23]
# Function Call
print(maxDiff(arr))
C#
// C# program for the above approach
using System;
class GFG{
// Function to count total number
// of bits present in a number
static int countBit(int n){
return (int)((Math.Log(n) / Math.Log(2))+1);
}
// Function to count total
// set bits in a number
static int countSetBit(int n){
// Stores the count
// of set bits
int ans = 0;
while(n > 0){
ans += (n & 1);
// Right shift by 1
n >>= 1;
}
// Return the final count
return ans;
}
// Function to find maximum number
// by shifting two unequal bits
static int maximize(int n){
// Count set bits in number n
int bits = countBit(n);
int setBits = countSetBit(n);
int ans = 0;
// Iterate the string bits
for(int i = 0; i < bits; i++){
if (i < setBits)
ans |= 1;
if(i != setBits - 1)
ans <<= 1;
}
return ans;
}
// Function to find minimum number
// by shifting two unequal bits
static int minimize(int n){
int setBits = countSetBit(n);
int ans = 0;
// Iterate the set bit
for (int i = 0; i < setBits; i++){
ans |= 1;
if (i != setBits - 1)
ans <<= 1;
}
return ans;
}
// Function to find the maximum difference
static int maxDiff(int[] arr){
// Stores the maximum difference
int caseOne = 0;
// Stores the sum of elements
// placed at odd positions
int SumOfOdd = 0;
// Stores the sum of elements
// placed at even positions
int SumOfeven = 0;
// Traverse the array
for(int i = 0; i < arr.Length; i++){
if ((i % 2) != 0)
SumOfOdd += minimize(arr[i]);
else
SumOfeven += maximize(arr[i]);
}
// Update CaseOne
caseOne = Math.Abs(SumOfOdd - SumOfeven);
// Stores the maximum difference
int caseTwo = 0;
// Assign value O
SumOfOdd = 0;
SumOfeven = 0;
// Traverse the array
for(int i = 0; i < arr.Length; i++)
{
if ((i % 2) != 0)
SumOfOdd += maximize(arr[i]);
else
SumOfeven += minimize(arr[i]);
}
// Update caseTwo
caseTwo = Math.Abs(SumOfOdd - SumOfeven);
// Return maximum of caseOne and CaseTwo
return Math.Max(caseOne, caseTwo);
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = {54, 32, 11, 23};
// Function Call
Console.Write(maxDiff(arr));
}
}
// This code is contributed by code_hunt.
Javascript
58
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live