给定一个由N个正整数组成的数组arr [] ,任务是找到前N个自然数和数组arr []置换的相同索引元素的按位与的最大和。
例子:
Input: arr[] = {4, 2, 3, 6}
Output: 5
Explanation: Consider the permutation {1, 0, 3, 2}. Sum of Bitwise AND of the above permutation and the given array = 1 & 4 + 0 & 2 + 3 & 3 + 2 & 6 = 0 + 0 + 3 + 2 = 5, which is maximum among all permutations.
Input: arr[] = {3, 4, 1, 0, 5}
Output: 8
方法:解决给定问题的想法是生成前N个自然数的所有置换,并使用数组arr []计算生成的置换的按位与之和。检查每个排列后,打印获得的按位与之和的最大值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate sum of
// Bitwise AND of same indexed
// elements of the arrays p[] and arr[]
int calcScore(vector p, int arr[], int N)
{
// Stores the resultant sum
int ans = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update sum of Bitwise AND
ans += (p[i] & arr[i]);
}
// Return the value obtained
return ans;
}
// Function to generate all permutations
// and calculate the maximum sum of Bitwise
// AND of same indexed elements present in
// any permutation and an array arr[]
int getMaxUtil(vector p, int arr[], int ans,
bool chosen[], int N)
{
// If the size of the array is N
if (p.size() == N) {
// Calculate cost of permutation
ans = max(ans, calcScore(p, arr, N));
return ans;
}
// Generate all permutations
for (int i = 0; i < N; i++) {
if (chosen[i]) {
continue;
}
// Update chosen[i]
chosen[i] = true;
// Update the permutation p[]
p.push_back(i);
// Generate remaining permutations
ans = getMaxUtil(p, arr, ans, chosen, N);
chosen[i] = false;
p.pop_back();
}
// Return the resultant sum
return ans;
}
// Function to find the maximum sum of Bitwise
// AND of same indexed elements in a permutation
// of first N natural numbers and arr[]
void getMax(int arr[], int N)
{
// Stores the resultant maximum sum
int ans = 0;
bool chosen[N];
for (int i = 0; i < N; i++)
chosen[i] = false;
// Stores the generated permutation P
vector p;
// Function call to store result
int res = getMaxUtil(p, arr, ans, chosen, N);
// Print the result
cout << res;
}
// Driven Program
int main()
{
int arr[] = { 4, 2, 3, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
getMax(arr, N);
return 0;
}
// This code is contributed by Kingash.
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to calculate sum of
// Bitwise AND of same indexed
// elements of the arrays p[] and arr[]
static int calcScore(ArrayList p, int arr[])
{
// Stores the resultant sum
int ans = 0;
// Traverse the array
for (int i = 0; i < arr.length; i++) {
// Update sum of Bitwise AND
ans += (p.get(i) & arr[i]);
}
// Return the value obtained
return ans;
}
// Function to generate all permutations
// and calculate the maximum sum of Bitwise
// AND of same indexed elements present in
// any permutation and an array arr[]
static int getMaxUtil(ArrayList p, int arr[],
int ans, boolean chosen[], int N)
{
// If the size of the array is N
if (p.size() == N) {
// Calculate cost of permutation
ans = Math.max(ans, calcScore(p, arr));
return ans;
}
// Generate all permutations
for (int i = 0; i < N; i++) {
if (chosen[i]) {
continue;
}
// Update chosen[i]
chosen[i] = true;
// Update the permutation p[]
p.add(i);
// Generate remaining permutations
ans = getMaxUtil(p, arr, ans, chosen, N);
chosen[i] = false;
p.remove(p.size() - 1);
}
// Return the resultant sum
return ans;
}
// Function to find the maximum sum of Bitwise
// AND of same indexed elements in a permutation
// of first N natural numbers and arr[]
static void getMax(int arr[], int N)
{
// Stores the resultant maximum sum
int ans = 0;
boolean chosen[] = new boolean[N];
// Stores the generated permutation P
ArrayList p = new ArrayList<>();
// Function call to store result
int res = getMaxUtil(p, arr, ans, chosen, N);
// Print the result
System.out.println(res);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 2, 3, 6 };
int N = arr.length;
// Function Call
getMax(arr, N);
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to calculate sum of
# Bitwise AND of same indexed
# elements of the arrays p[] and arr[]
def calcScore(p, arr):
# Stores the resultant sum
ans = 0
# Traverse the array
for i in range(len(arr)):
# Update sum of Bitwise AND
ans += (p[i] & arr[i])
# Return the value obtained
return ans
# Function to generate all permutations
# and calculate the maximum sum of Bitwise
# AND of same indexed elements present in
# any permutation and an array arr[]
def getMaxUtil(p, arr, ans, chosen, N):
# If the size of the array is N
if len(p) == N:
# Calculate cost of permutation
ans = max(ans, calcScore(p, arr))
return ans
# Generate all permutations
for i in range(N):
if chosen[i]:
continue
# Update chosen[i]
chosen[i] = True
# Update the permutation p[]
p.append(i)
# Generate remaining permutations
ans = getMaxUtil(p, arr, ans, chosen, N)
chosen[i] = False
p.pop()
# Return the resultant sum
return ans
# Function to find the maximum sum of Bitwise
# AND of same indexed elements in a permutation
# of first N natural numbers and arr[]
def getMax(arr, N):
# Stores the resultant maximum sum
ans = 0
chosen = [False for i in range(N)]
# Stores the generated permutation P
p = []
# Function call to store result
res = getMaxUtil(p, arr, ans, chosen, N)
# Print the result
print(res)
# Driver Code
if __name__ == '__main__':
# Given array
arr = [4, 2, 3, 6]
N = len(arr)
# Function Call
getMax(arr, N)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate sum of
// Bitwise AND of same indexed
// elements of the arrays p[] and arr[]
static int calcScore(List p, int[] arr)
{
// Stores the resultant sum
int ans = 0;
// Traverse the array
for(int i = 0; i < arr.Length; i++)
{
// Update sum of Bitwise AND
ans += (p[i] & arr[i]);
}
// Return the value obtained
return ans;
}
// Function to generate all permutations
// and calculate the maximum sum of Bitwise
// AND of same indexed elements present in
// any permutation and an array arr[]
static int getMaxUtil(List p, int[] arr,
int ans, bool[] chosen,
int N)
{
// If the size of the array is N
if (p.Count == N)
{
// Calculate cost of permutation
ans = Math.Max(ans, calcScore(p, arr));
return ans;
}
// Generate all permutations
for(int i = 0; i < N; i++)
{
if (chosen[i])
{
continue;
}
// Update chosen[i]
chosen[i] = true;
// Update the permutation p[]
p.Add(i);
// Generate remaining permutations
ans = getMaxUtil(p, arr, ans, chosen, N);
chosen[i] = false;
p.Remove(p.Count - 1);
}
// Return the resultant sum
return ans;
}
// Function to find the maximum sum of Bitwise
// AND of same indexed elements in a permutation
// of first N natural numbers and arr[]
static void getMax(int[] arr, int N)
{
// Stores the resultant maximum sum
int ans = 0;
bool[] chosen = new bool[N];
// Stores the generated permutation P
List p = new List();
// Function call to store result
int res = getMaxUtil(p, arr, ans, chosen, N);
// Print the result
Console.Write(res);
}
// Driver Code
public static void Main()
{
int[] arr = { 4, 2, 3, 6 };
int N = arr.Length;
// Function Call
getMax(arr, N);
}
}
// This code is contributed by sanjoy_62
Javascript
输出:
5
时间复杂度: O(N * N!)
辅助空间: O(N)