You are given an array of N integers. The task is to partition the array into an arbitrary number of groups (subarrays) such that bitwise AND of beauties of the groups is maximized.
The beauty of a group (subarray) is defined by the bitwise XOR of all the numbers belonging to that group (subarray).
Example:
Input: n = 7, a = [4 ,1, 2, 5, 2, 1, 6]
Output: 7
Approach
C++
#include <bits/stdc++.h>using namespace std;int partition(int n, int a[]){int x = 0;for (int i = 0; i < n; i++)x = x ^ a[i];int y = 0, z = 0, max1 = x;for (int i = 0; i < n; i++){//parition at ith positiony = y ^ a[i];z = y & (y ^ x);if (z > max1)max1 = z;}return max1;}int main(){int n = 7;int a[n] = {4, 1, 2, 5, 2, 1, 6};cout << partition(n, a) << "\n";return 0;}
No comments:
Post a Comment