最大化给定数组中所有可能的 N/2 对的按位或的 LSB 总和
给定一个由N个正整数组成的数组arr[] ,其中N是偶数,任务是形成N/2对,使得所有这些对的按位或的最低有效位之和最大。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 8
Explanation:
On forming the pairs as (8, 4),(6, 2),(1, 3),(5, 7), the Bitwise OR of the pair is given by:
8 OR 4 = 12 and LSB = 4
6 OR 2 = 6 and LSB = 2
1 OR 3 = 3 and LSB = 1
5 OR 7 = 7 and LSB = 1
The sum of all the LSB is 4 + 2 + 1 + 1 = 8, which is maximum possible sum.
Input: arr[] = {1, 2, 3, 4, 5}
Output: 3
方法:给定的问题可以通过找到每个数组元素arr[i]的 LSB 并将它们存储在另一个数组中来解决,比如lsb_arr[]并按降序对该数组进行排序。现在,只存储每个数组元素的 LSB 就足够了,因为在答案中,只需要考虑LSB 。因此,只有 LSB 可用于按位或运算。现在,考虑每一对(i, i + 1)并将这两个中的最小值添加到结果中。请按照以下步骤解决给定的问题:
- 初始化一个列表lsb_arr[]以存储所有数组元素arr[i]的最低有效位。
- 遍历范围[0, N)并将每个arr[i]的 LSB 存储在lsb_arr[]中。
- 按降序对列表lsb_arr[]进行排序。
- 将变量ans初始化为0以存储 Least Significant Bits 的结果总和。
- 使用变量i遍历范围[0, N)并将(i + 1)位置的元素的值添加到变量ans并将i的值增加2 。
- 完成上述步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function top get LSB value of v
int chk(int n)
{
// Binary conversion
vector v;
while (n != 0) {
v.push_back(n % 2);
n = n / 2;
}
for (int i = 0; i < v.size(); i++) {
if (v[i] == 1) {
return pow(2, i);
}
}
return 0;
}
// Function to find the sum of LSBs of
// all possible pairs of the given array
void sumOfLSB(int arr[], int N)
{
// Stores the LSB of array elements
vector lsb_arr;
for (int i = 0; i < N; i++) {
// Storing the LSB values
lsb_arr.push_back(chk(arr[i]));
}
// Sort the array lab_arr[]
sort(lsb_arr.begin(), lsb_arr.end(), greater());
int ans = 0;
for (int i = 0; i < N - 1; i += 2) {
// Taking pairwise sum to get
// the maximum sum of LSB
ans += (lsb_arr[i + 1]);
}
// Print the result
cout << (ans);
}
// Driver Code
int main()
{
int N = 5;
int arr[] = { 1, 2, 3, 4, 5 };
// Function Call
sumOfLSB(arr, N);
}
// This code is contributed by Potta Lokesh
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function top get LSB value of v
static int chk(int n)
{
// Binary conversion
Vector v = new Vector();
while (n != 0) {
v.add(n % 2);
n = n / 2;
}
for (int i = 0; i < v.size(); i++) {
if (v.get(i) == 1) {
return (int) Math.pow(2, i);
}
}
return 0;
}
// Function to find the sum of LSBs of
// all possible pairs of the given array
static void sumOfLSB(int arr[], int N)
{
// Stores the LSB of array elements
Vector lsb_arr = new Vector() ;
for (int i = 0; i < N; i++) {
// Storing the LSB values
lsb_arr.add(chk(arr[i]));
}
// Sort the array lab_arr[]
Collections.sort(lsb_arr);
int ans = 0;
for (int i = 0; i < N - 1; i += 2) {
// Taking pairwise sum to get
// the maximum sum of LSB
ans += (lsb_arr.get(i + 1));
}
// Print the result
System.out.print(ans);
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
int arr[] = { 1, 2, 3, 4, 5 };
// Function Call
sumOfLSB(arr, N);
}
}
// This code contributed by shikhasingrajput
Python3
# Python program for the above approach
# Function top get LSB value of v
def chk(v):
# Binary conversion
v = list(bin(v)[2:])
v.reverse()
if('1' in v):
v = v.index('1')
return (2**v)
else:
return 0
# Function to find the sum of LSBs of
# all possible pairs of the given array
def sumOfLSB(arr, N):
# Stores the LSB of array elements
lsb_arr = []
for i in range(N):
# Storing the LSB values
lsb_arr.append(chk(arr[i]))
# Sort the array lab_arr[]
lsb_arr.sort(reverse=True)
ans = 0
for i in range(0, N-1, 2):
# Taking pairwise sum to get
# the maximum sum of LSB
ans += (lsb_arr[i+1])
# Print the result
print(ans)
# Driver Code
N = 5
arr = [1, 2, 3, 4, 5]
# Function Call
sumOfLSB(arr, N)
Javascript
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function top get LSB value of v
static int chk(int n)
{
// Binary conversion
List v = new List();
while (n != 0) {
v.Add(n % 2);
n = n / 2;
}
int j = 0;
foreach(int i in v) {
if (i == 1) {
return (int) Math.Pow(2.0, (double)j);
}
j++;
}
return 0;
}
// Function to find the sum of LSBs of
// all possible pairs of the given array
static void sumOfLSB(int[] arr, int N)
{
// Stores the LSB of array elements
int[] lsb_arr = new int[N];
for (int i = 0; i < N; i++) {
// Storing the LSB values
lsb_arr[i] = chk(arr[i]);
}
// Sort the array lab_arr[]
Array.Sort(lsb_arr);
int ans = 0;
for (int i = 0; i < N - 1; i += 2) {
// Taking pairwise sum to get
// the maximum sum of LSB
ans += (lsb_arr[i + 1]);
}
// Print the result
Console.WriteLine(ans);
}
// Driver Code
static public void Main (){
int N = 5;
int[] arr = { 1, 2, 3, 4, 5 };
// Function Call
sumOfLSB(arr, N);
}
}
// This code is contributed by Dharanendra L V.
3
时间复杂度: O(N*log N)
辅助空间: O(N)