Yet another partition problem

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 nint a[])
{

    int x = 0;
    for (int i = 0i < ni++)
        x = x ^ a[i];
    int y = 0z = 0max1 = x;
    for (int i = 0i < ni++)
    {
        //parition at ith position
        y = y ^ a[i];
        z = y & (y ^ x);
        if (z > max1)
            max1 = z;
    }
    return max1;
}
int main()
{

    int n = 7;
    int a[n] = {4125216};

    cout << partition(na<< "\n";
    return 0;
}


No comments:

Post a Comment