将 Array 的每个元素与分区号的 XOR 和最大化
给定一个大小为N的正整数数组arr ,任务是将数组拆分为 3 个分区,使得数组中每个元素与其分区号的按位 XOR 之和最大。
例子:
Input: arr[] = { 2, 4, 7, 1, 8, 7, 2 }
Output: First partition: 2 4 7 1 8
Second partition: 7
Third partition: 2
Sum: 244
Input: arr[] = {95, 2, 86, 12, 9, 14, 45, 11}
Output: First partition: 95 2 86 12 9 14
Second partition: 45
Third partition: 11
Sum: 1994
方法:想法是对三个分区使用嵌套循环。
- 计算每个分区的每个元素与其分区号的 XOR 和。
- 求所有三个分区的 XOR 和的全局最大和。
- 返回并打印三个分区及其最大 XOR 和。
下面是上述方法的实现:
C++
// C++ program for maximize the sum of
// bitwise XOR of each element of the array
// with it's partition number
#include
using namespace std;
// Utility function to print the partitions
void ShowPartition(vector sum, vector arr)
{
cout << "First partition: ";
for (int i = 0; i <= sum[0]; i++)
cout << arr[i] << " ";
cout << "\nSecond partition: ";
for (int i = sum[0] + 1; i <= sum[1]; i++)
cout << arr[i] << " ";
cout << "\nThird partition: ";
for (int i = sum[1] + 1; i <= sum[2]; i++)
cout << arr[i] << " ";
cout << "\nSum: ";
cout << sum[3];
}
// Function to maximise the partitions sum
vector MaximumSumPartition(vector arr)
{
int i, j, k;
int n = arr.size();
vector sum(4, 0);
// initialise the dummy sum values.
int s1 = 0, s2 = 0, s3 = 0, s = INT_MIN;
int x, y, z;
// nested for loop
for (i = 0; i <= n - 3; i++) {
// XOR sum of first partition.
s1 += 1 ^ arr[i];
x = i;
for (j = i + 1; j <= n - 2; j++) {
// XOR sum of second partition.
s2 += 2 ^ arr[j];
y = j;
for (k = j + 1; k <= n - 1; k++) {
// XOR sum of third partition.
s3 += 3 ^ arr[k];
z = k;
// XOR sum of all three partition.
if (s1 + s2 + s3 > s) {
s = s1 + s2 + s3;
sum[0] = x;
sum[1] = y;
sum[2] = z;
sum[3] = s;
}
}
}
}
// return the vector.
return sum;
}
// Driver code
int main()
{
vector sum, arr{ 2, 4, 7, 1, 8, 7, 2 };
sum = MaximumSumPartition(arr);
ShowPartition(sum, arr);
return 0;
}
Java
// Java program for maximize the sum of
// bitwise XOR of each element of the array
// with it's partition number
import java.io.*;
class GFG {
// Utility function to print the partitions
static void ShowPartition(int []sum, int []arr)
{
System.out.print("First partition: ");
for (int i = 0; i <= sum[0]; i++)
System.out.print(arr[i] + " ");
System.out.print("\nSecond partition: ");
for (int i = sum[0] + 1; i <= sum[1]; i++)
System.out.print(arr[i] + " ");
System.out.print("\nThird partition: ");
for (int i = sum[1] + 1; i <= sum[2]; i++)
System.out.print(arr[i] + " ");
System.out.print("\nSum: ");
System.out.print(sum[3]);
}
// Function to maximise the partitions sum
static int[] MaximumSumPartition(int []arr)
{
int i = 0, j = 0, k = 0;
int n = arr.length;
int []sum = new int[4];
for(i = 0; i < 4; i++) {
sum[i] = 0;
}
// initialise the dummy sum values.
int s1 = 0, s2 = 0, s3 = 0, s = Integer.MIN_VALUE;
int x = 0, y = 0, z = 0;
// nested for loop
for (i = 0; i <= n - 3; i++) {
// XOR sum of first partition.
s1 += 1 ^ arr[i];
x = i;
for (j = i + 1; j <= n - 2; j++) {
// XOR sum of second partition.
s2 += 2 ^ arr[j];
y = j;
for (k = j + 1; k <= n - 1; k++) {
// XOR sum of third partition.
s3 += 3 ^ arr[k];
z = k;
// XOR sum of all three partition.
if (s1 + s2 + s3 > s) {
s = s1 + s2 + s3;
sum[0] = x;
sum[1] = y;
sum[2] = z;
sum[3] = s;
}
}
}
}
// return the vector.
return sum;
}
// Driver code
public static void main (String[] args) {
int []arr = { 2, 4, 7, 1, 8, 7, 2 };
int []sum = MaximumSumPartition(arr);
ShowPartition(sum, arr);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python code to implement the approach
import sys
# Utility function to print the partitions
def ShowPartition(sum, arr) :
print("First partition: ", end = '')
for i in range(sum[0]+1) :
print(arr[i] , end = " ")
print("\nSecond partition: ", end = '')
for i in range(sum[0]+1, sum[1]+1) :
print(arr[i] , end = " ")
print("\nThird partition: ", end = '')
for i in range(sum[1]+1, sum[2]+1) :
print(arr[i] , end = " ")
print("\nSum: ", end = '')
print(sum[3])
# Function to maximise the partitions sum
def MaximumSumPartition(arr) :
i = 0
j = 0
k = 0
n = len(arr)
sum = [0] * 4
for i in range(0, 4):
sum[i] = 0
# initialise the dummy sum values.
s1 = 0
s2 = 0
s3 = 0
s = -sys.maxsize -1
x = 0
y = 0
z = 0
# nested for loop
for i in range(0, n-2, 1):
# XOR sum of first partition.
s1 += 1 ^ arr[i]
x = i
for j in range(i + 1, n - 1, 1) :
# XOR sum of second partition.
s2 += 2 ^ arr[j]
y = j
for k in range(j + 1, n, 1) :
# XOR sum of third partition.
s3 += 3 ^ arr[k]
z = k
# XOR sum of all three partition.
if (s1 + s2 + s3 > s) :
s = s1 + s2 + s3
sum[0] = x
sum[1] = y
sum[2] = z
sum[3] = s
# return the vector.
return sum
# Driver code
arr = [ 2, 4, 7, 1, 8, 7, 2 ]
sum = MaximumSumPartition(arr);
ShowPartition(sum, arr);
# This code is contributed by code_hunt.
C#
// C# program for maximize the sum of
// bitwise XOR of each element of the array
// with it's partition number
using System;
class GFG
{
// Utility function to print the partitions
static void ShowPartition(int []sum, int []arr)
{
Console.Write("First partition: ");
for (int i = 0; i <= sum[0]; i++)
Console.Write(arr[i] + " ");
Console.Write("\nSecond partition: ");
for (int i = sum[0] + 1; i <= sum[1]; i++)
Console.Write(arr[i] + " ");
Console.Write("\nThird partition: ");
for (int i = sum[1] + 1; i <= sum[2]; i++)
Console.Write(arr[i] + " ");
Console.Write("\nSum: ");
Console.Write(sum[3]);
}
// Function to maximise the partitions sum
static int[] MaximumSumPartition(int []arr)
{
int i = 0, j = 0, k = 0;
int n = arr.Length;
int []sum = new int[4];
for(i = 0; i < 4; i++) {
sum[i] = 0;
}
// initialise the dummy sum values.
int s1 = 0, s2 = 0, s3 = 0, s = Int32.MinValue;
int x = 0, y = 0, z = 0;
// nested for loop
for (i = 0; i <= n - 3; i++) {
// XOR sum of first partition.
s1 += 1 ^ arr[i];
x = i;
for (j = i + 1; j <= n - 2; j++) {
// XOR sum of second partition.
s2 += 2 ^ arr[j];
y = j;
for (k = j + 1; k <= n - 1; k++) {
// XOR sum of third partition.
s3 += 3 ^ arr[k];
z = k;
// XOR sum of all three partition.
if (s1 + s2 + s3 > s) {
s = s1 + s2 + s3;
sum[0] = x;
sum[1] = y;
sum[2] = z;
sum[3] = s;
}
}
}
}
// return the vector.
return sum;
}
// Driver code
public static void Main()
{
int []arr = { 2, 4, 7, 1, 8, 7, 2 };
int []sum = MaximumSumPartition(arr);
ShowPartition(sum, arr);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
First partition: 2 4 7 1 8
Second partition: 7
Third partition: 2
Sum: 244
时间复杂度: O(N 3 )
辅助空间: O(1)