📌  相关文章
📜  通过将元素最多增加 K 来最大化数组的按位或

📅  最后修改于: 2022-05-13 01:56:10.474000             🧑  作者: Mango

通过将元素最多增加 K 来最大化数组的按位或

给定一个数组arr[]和一个整数K ,任务是最大化数组arr[]的按位或,其中arr [] 的每个元素几乎可以递增K

例子:

方法:这个问题可以通过 使用按位运算。请按照以下步骤解决给定的问题。

  • 找到所有元素的按位或,这将告诉每个位的状态。
  • MSB (Most Significant bit)开始,因为 MSB 在最终的按位 OR 中做了很多改变。
  • 如果按位或未设置特定位。找到设置该位的最小步骤。
  • 设置第 i 位的最小步骤是(1<
  • 如果最小步数小于等于K ,则计算最小步数并更新数组。

下面是上述方法的实现。

C++
// C++ code for the above approach
#include 
using namespace std;
 
void MaximizeBitwiseOR(long a[], int k, int n)
{
 
  // For storing initial
  // bitwise OR of array arr[]
  long oris = 0;
  long bitwiseOr = 0;
  for (int i = 0; i < n; ++i)
  {
    bitwiseOr |= a[i];
  }
 
  for (int i = 60; i >= 0; i--)
  {
    if (((1L << i) & bitwiseOr) == 0)
    {
 
      long minSteps = LONG_MAX;
      int mini = -1;
      for (int j = 0; j < n; j++)
      {
 
        long y = ((1L << i) - 1) & a[j];
        long steps = (1L << i) - y;
 
        if (steps <= k && steps < minSteps)
        {
          minSteps = steps;
          mini = j;
 
        }
      }
      if (mini != -1)
      {
 
        k -= minSteps;
        a[mini] += minSteps;
        long orr = 0;
        for (int j = 0; j < n; j++)
        {
          orr |= a[j];
        }
        bitwiseOr = orr;
      }
    }
  }
 
  // Print the required result
  for (long elements = 0; elements < n; elements++)
  {
    if(a[elements]>0)
      cout << a[elements] << " ";
    else
      cout<<0<<" ";
  }
}
 
// Driver code
int main()
{
  int N = 6;
  int K = 2;
  long arr[] = {1, 3, 7, 0, 6, 1};
 
  MaximizeBitwiseOR(arr, K, N);
}
 
// This code is contributed by Potta Lokesh


Java
import java.io.*;
 
class Main {
 
    static void MaximizeBitwiseOR(long[] a, int k, int n)
    {
 
        // For storing initial
        // bitwise OR of array arr[]
        long or = 0;
        long bitwiseOr = 0;
        for (int i = 0; i < a.length; ++i) {
            bitwiseOr |= a[i];
        }
 
        for (int i = 60; i >= 0; i--) {
            if (((1L << i) & bitwiseOr) == 0) {
 
                long minSteps = Long.MAX_VALUE;
                int mini = -1;
                for (int j = 0; j < n; j++) {
 
                    long y = ((1L << i) - 1) & a[j];
                    long steps = (1L << i) - y;
 
                    if (steps <= k
                        && steps < minSteps) {
                        minSteps = steps;
                        mini = j;
                    }
                }
                if (mini != -1) {
 
                    k -= minSteps;
                    a[mini] += minSteps;
                    long orr = 0;
                    for (int j = 0; j < n; j++) {
                        orr |= a[j];
                    }
                    bitwiseOr = orr;
                }
            }
        }
 
        // Print the required result
        for (long elements : a) {
            System.out.print(elements + " ");
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 6;
        int K = 2;
        long arr[] = { 1, 3, 7, 0, 6, 1 };
 
        MaximizeBitwiseOR(arr, K, N);
    }
}


Python3
# Python3 code for the above approach
import sys
 
def MaximizeBitwiseOR(a, k, n):
 
    # For storing initial
    # bitwise OR of array arr[]
    oris = 0
    bitwiseOr = 0
    for i in range(n):
 
        bitwiseOr |= a[i]
 
    for i in range(60, -1, -1):
 
        if (((1 << i) & bitwiseOr) == 0):
 
            minSteps = sys.maxsize
            mini = -1
            for j in range(n):
 
                y = ((1 << i) - 1) & a[j]
                steps = (1 << i) - y
 
                if (steps <= k and steps < minSteps):
 
                    minSteps = steps
                    mini = j
 
            if (mini != -1):
 
                k -= minSteps
                a[mini] += minSteps
                orr = 0
                for j in range(n):
 
                    orr |= a[j]
 
                bitwiseOr = orr
 
    # Print the required result
    for elements in range(n):
 
        if(a[elements] > 0):
            print(a[elements], end=" ")
        else:
            print(0, end=" ")
 
# Driver code
if __name__ == "__main__":
 
    N = 6
    K = 2
    arr = [1, 3, 7, 0, 6, 1]
 
    MaximizeBitwiseOR(arr, K, N)
 
    # This code is contributed by ukasp.


C#
using System;
 
class GFG {
 
  static void MaximizeBitwiseOR(long[] a, int k, int n)
  {
 
    // For storing initial
    // bitwise OR of array arr[]
    long or = 0;
    long bitwiseOr = 0;
    for (int i = 0; i < a.Length; ++i) {
      bitwiseOr |= a[i];
    }
 
    for (int i = 60; i >= 0; i--) {
      if (((1L << i) & bitwiseOr) == 0) {
 
        long minSteps = Int32.MaxValue;
        int mini = -1;
        for (int j = 0; j < n; j++) {
 
          long y = ((1L << i) - 1) & a[j];
          long steps = (1L << i) - y;
 
          if (steps <= k
              && steps < minSteps) {
            minSteps = steps;
            mini = j;
          }
        }
        if (mini != -1) {
 
          k -= (int)minSteps;
          a[mini] += minSteps;
          long orr = 0;
          for (int j = 0; j < n; j++) {
            orr |= a[j];
          }
          bitwiseOr = orr;
        }
      }
    }
 
    // Print the required result
    foreach (long elements in a) {
      Console.Write(elements + " ");
    }
  }
 
  // Driver code
  public static void Main()
  {
    int N = 6;
    int K = 2;
    long []arr = { 1, 3, 7, 0, 6, 1 };
 
    MaximizeBitwiseOR(arr, K, N);
  }
}
 
// This code is contributed by Samim Hossain Mondal.



输出
1 3 8 0 6 1 

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