给定一个由N 个整数组成的数组arr[] ,任务是在将数组拆分为子数组(可能为零子数组)后找到每个子数组的按位或的最大按位异或。
例子:
Input: arr[] = {1, 5, 7}, N = 3
Output: 7
Explanation:
The given array can be expressed as the 1 subarray i.e., {1, 5, 7}.
The Bitwise XOR of the Bitwise OR of the formed subarray is 7, which is the maximum possible value.
Input: arr[] = {1, 2}, N = 2
Output: 3
朴素方法:解决上述给定问题的最简单方法是使用递归生成所有可能的子数组破坏组合,并在每次递归调用时,找到所有可能形成的子数组的按位或的按位异或的最大值并打印。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Recursive function to find all the
// possible breaking of arrays into
// subarrays and find the maximum
// Bitwise XOR
int maxXORUtil(int arr[], int N,
int xrr, int orr)
{
// If the value of N is 0
if (N == 0)
return xrr ^ orr;
// Stores the result if the new
// group is formed with the first
// element as arr[i]
int x = maxXORUtil(arr, N - 1,
xrr ^ orr,
arr[N - 1]);
// Stores if the result if the
// arr[i] is included in the
// last group
int y = maxXORUtil(arr, N - 1,
xrr, orr | arr[N - 1]);
// Returns the maximum of
// x and y
return max(x, y);
}
// Function to find the maximum possible
// Bitwise XOR of all possible values of
// the array after breaking the arrays
// into subarrays
int maximumXOR(int arr[], int N)
{
// Return the result
return maxXORUtil(arr, N, 0, 0);
}
// Driver Code
int main()
{
int arr[] = { 1, 5, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maximumXOR(arr, N);
return 0;
}
Java
// Java program for the above approach
public class GFG{
// Recursive function to find all the
// possible breaking of arrays into
// subarrays and find the maximum
// Bitwise XOR
static int maxXORUtil(int arr[], int N, int xrr,
int orr)
{
// If the value of N is 0
if (N == 0)
return xrr ^ orr;
// Stores the result if the new
// group is formed with the first
// element as arr[i]
int x
= maxXORUtil(arr, N - 1, xrr ^ orr, arr[N - 1]);
// Stores if the result if the
// arr[i] is included in the
// last group
int y
= maxXORUtil(arr, N - 1, xrr, orr | arr[N - 1]);
// Returns the maximum of
// x and y
return Math.max(x, y);
}
// Function to find the maximum possible
// Bitwise XOR of all possible values of
// the array after breaking the arrays
// into subarrays
static int maximumXOR(int arr[], int N)
{
// Return the result
return maxXORUtil(arr, N, 0, 0);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 5, 7 };
int N = arr.length;
System.out.println(maximumXOR(arr, N));
}
}
// This code is contributed by abhinavjain194
Python3
# C++ program for the above approach
# Recursive function to find all the
# possible breaking of arrays o
# subarrays and find the maximum
# Bitwise XOR
def maxXORUtil(arr, N, xrr, orr):
# If the value of N is 0
if (N == 0):
return xrr ^ orr
# Stores the result if the new
# group is formed with the first
# element as arr[i]
x = maxXORUtil(arr, N - 1, xrr ^ orr, arr[N - 1])
# Stores if the result if the
# arr[i] is included in the
# last group
y = maxXORUtil(arr, N - 1, xrr, orr | arr[N - 1])
# Returns the maximum of
# x and y
return max(x, y)
# Function to find the maximum possible
# Bitwise XOR of all possible values of
# the array after breaking the arrays
# o subarrays
def maximumXOR(arr, N):
# Return the result
return maxXORUtil(arr, N, 0, 0)
# Driver Code
arr = 1, 5, 7
N = len(arr)
print(maximumXOR(arr, N))
# this code is contributed by shivanisinghss2110
C#
// C# program for the above approach
using System;
class GFG
{
// Recursive function to find all the
// possible breaking of arrays into
// subarrays and find the maximum
// Bitwise XOR
static int maxXORUtil(int[] arr, int N, int xrr,
int orr)
{
// If the value of N is 0
if (N == 0)
return xrr ^ orr;
// Stores the result if the new
// group is formed with the first
// element as arr[i]
int x
= maxXORUtil(arr, N - 1, xrr ^ orr, arr[N - 1]);
// Stores if the result if the
// arr[i] is included in the
// last group
int y
= maxXORUtil(arr, N - 1, xrr, orr | arr[N - 1]);
// Returns the maximum of
// x and y
return Math.Max(x, y);
}
// Function to find the maximum possible
// Bitwise XOR of all possible values of
// the array after breaking the arrays
// into subarrays
static int maximumXOR(int[] arr, int N)
{
// Return the result
return maxXORUtil(arr, N, 0, 0);
}
// Driver code
static void Main()
{
int[] arr = { 1, 5, 7 };
int N = arr.Length;
Console.Write(maximumXOR(arr, N));
}
}
// This code is contributed by sanjoy_62.
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the bitwise OR of
// array elements
int MaxXOR(int arr[], int N)
{
// Stores the resultant maximum
// value of Bitwise XOR
int res = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
res |= arr[i];
}
// Return the maximum value res
return res;
}
// Driver Code
int main()
{
int arr[] = { 1, 5, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << MaxXOR(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the bitwise OR of
// array elements
static int MaxXOR(int arr[], int N)
{
// Stores the resultant maximum
// value of Bitwise XOR
int res = 0;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
res |= arr[i];
}
// Return the maximum value res
return res;
}
public static void main(String[] args)
{
int arr[] = { 1, 5, 7 };
int N = arr.length;
System.out.println(MaxXOR(arr, N));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to find the bitwise OR of
# array elements
def MaxXOR(arr, N):
# Stores the resultant maximum
# value of Bitwise XOR
res = 0
# Traverse the array arr[]
for i in range(N):
res |= arr[i]
# Return the maximum value res
return res
# Driver Code
if __name__ == '__main__':
arr = [ 1, 5, 7 ]
N = len(arr)
print (MaxXOR(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the bitwise OR of
// array elements
static int MaxXOR(int []arr, int N)
{
// Stores the resultant maximum
// value of Bitwise XOR
int res = 0;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
res |= arr[i];
}
// Return the maximum value res
return res;
}
public static void Main(String[] args)
{
int []arr = { 1, 5, 7 };
int N = arr.Length;
Console.Write(MaxXOR(arr, N));
}
}
// This code is contributed by shivanisinghss2110
Javascript
7
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:通过观察Bitwise XOR和Bitwise OR之间的关系可以优化上述方法,即N个元素的Bitwise XOR值最多是N个元素的Bitwise OR值。因此,要找到最大值,想法是将组拆分为整个数组的仅 1 组。
因此,打印数组元素arr[]的按位 OR的值作为结果最大值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the bitwise OR of
// array elements
int MaxXOR(int arr[], int N)
{
// Stores the resultant maximum
// value of Bitwise XOR
int res = 0;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
res |= arr[i];
}
// Return the maximum value res
return res;
}
// Driver Code
int main()
{
int arr[] = { 1, 5, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << MaxXOR(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the bitwise OR of
// array elements
static int MaxXOR(int arr[], int N)
{
// Stores the resultant maximum
// value of Bitwise XOR
int res = 0;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
res |= arr[i];
}
// Return the maximum value res
return res;
}
public static void main(String[] args)
{
int arr[] = { 1, 5, 7 };
int N = arr.length;
System.out.println(MaxXOR(arr, N));
}
}
// This code is contributed by offbeat
蟒蛇3
# Python3 program for the above approach
# Function to find the bitwise OR of
# array elements
def MaxXOR(arr, N):
# Stores the resultant maximum
# value of Bitwise XOR
res = 0
# Traverse the array arr[]
for i in range(N):
res |= arr[i]
# Return the maximum value res
return res
# Driver Code
if __name__ == '__main__':
arr = [ 1, 5, 7 ]
N = len(arr)
print (MaxXOR(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the bitwise OR of
// array elements
static int MaxXOR(int []arr, int N)
{
// Stores the resultant maximum
// value of Bitwise XOR
int res = 0;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
res |= arr[i];
}
// Return the maximum value res
return res;
}
public static void Main(String[] args)
{
int []arr = { 1, 5, 7 };
int N = arr.Length;
Console.Write(MaxXOR(arr, N));
}
}
// This code is contributed by shivanisinghss2110
Javascript
7
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。