📌  相关文章
📜  通过交换二进制表示形式中的不相等相邻位,最大程度地提高了奇数和偶数索引数组元素之间的差异

📅  最后修改于: 2021-04-17 18:26:19             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是通过交换任意数组的二进制表示形式中不相等的相邻位,找到位于数组偶数和奇数索引处的数组元素之和之间的最大绝对差值。元素多次。

例子:

方法:想法是观察到任何设置的位都可以移动到任何其他位置。请按照以下步骤解决问题:

  • 定义一个函数,例如maximum() ,通过移动两个不相等的相邻位来最大化一个数字。
  • 定义一个函数,例如minimum() ,通过移动两个不相等的相邻位来最小化一个数字。
  • 执行以下操作:
    • 初始化一个变量,例如ans ,以存储最小值。
    • 为了最大程度地减少数量,请将所有设置的位移到右侧,将所有未设置的位移到左侧。
    • 遍历范围[0,设置位的计数– 1],并将ans更新为ans | 1 。如果不等于设置的位数,则将ans左移1
    • 返回ans的值。
  • 首先,找到通过最大化放置在偶数索引处的元素并最小化放置在奇数索引处的元素而获得的差异。将其存储在一个变量中,例如caseOne
  • 现在,找到通过最小化放置在偶数索引处的元素并最大化放置在奇数索引处的元素而获得的差异。将其存储在一个变量中,例如caseTwo
  • 完成上述步骤后,打印出caseOneCaseTwo的最大值。

下面是上述方法的实现:

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 diffrence
  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 diffrence
  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 diffrence
    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 diffrence
    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)