Saturday, November 16, 2024
HomeSoftware DevelopmentMinimal bitwise OR after eradicating at most Ok parts from given Array

Minimal bitwise OR after eradicating at most Ok parts from given Array

[ad_1]

Given an array A[] of size N, the duty is to search out the minimal attainable worth of bitwise OR of the weather after eradicating at most Ok parts.

Examples: 

Enter: A = {1, 10, 9, 4, 5, 16, 8}, Ok = 3
Output: 11
Clarification: Take away 4, 5, 16 for the given array.
The remaining parts are {1, 10, 9, 5}. 
The OR of those parts are 11 which is the minimal attainable.

Enter: A = {1, 2, 4, 8}, Ok = 1
Output: 7
Clarification: Take away 8 type the Array, Minimal OR= 1 | 2 | 4 = 7

 

Strategy: This downside will be solved utilizing bit manipulation on the idea of the next concept:

Attempt to take away the very best MSB parts first, then the weather having 2nd highest MSB and so forth till Ok parts are eliminated.

Comply with the illustration given under for a greater understanding.

Illustration :

Think about A[] = [1, 10, 9, 4, 5, 16, 8 ]

  • Binary illustration of the weather are:
    1 – 00001
    10 – 01010
    9 – 01001
    4 – 00100
    5 – 00101
    16  – 10000
    8 – 01000
  • The positions of the MSB of each parts are:
    1 -> 0, 10 -> 3, 9 -> 3, 4 -> 2, 5 -> 2, 16  -> 4, 8 -> 3
  • Due to this fact depend of parts having MSB at ith place are as given under:
    setBitCount = [1, 0, 2, 3, 1 ]
    MSB place    0, 1, 2, 3, 4
  • Traverse array and examine if present setBitCount is lower than ok, then take away all parts with present Bit as FirstSetBit.
    For, Ok = 3, traverse array: 
    =>When i = 4: setBitCount[i] = 1, which is lower than Ok so take away 16, and now Ok = 2.
    =>When i = 3: Ok < setBitCount[i] (i.e 3). So don’t take away it.
    =>When i = 2: setBitCount[i] = 2 ≤ Ok, so take away 4 and 5. Now, Ok =0.
  • Calculate OR for all remaining parts i.e (1, 5, 9, 10) = 11

Comply with the steps talked about under to implement the above remark:

  • Create a hash array.
  • Traverse the given array and increment the MSB Index of each aspect into the hash array.
  • Now, traverse the hash array from MSB and in every iteration:
    • Test whether it is attainable to take away all parts having the ith bit as MSB  i.e. setBitCount ≤ Ok.
    • If setBitCount ≤ Ok, don’t calculate OR for that parts and, simply lower the Ok.
    • if not, calculate the OR for parts with ith bit as MSB.
  • Return Calculate OR after eradicating Ok parts.

Under is the implementation of the above method:

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

int minimumOR(vector<int> A, int N, int Ok)

{

    vector<int> SetBitCount(32, 0);

    int OR = 0, FirstSetBit = 0;

  

    

    kind(A.rbegin(), A.rend());

    for (int i = 0; i < N; i++) {

        FirstSetBit = log2(A[i]) + 1;

        SetBitCount[FirstSetBit]++;

    }

  

    

    for (int i = 31, j = 0; i >= 0; i--) {

        if (SetBitCount[i] <= Ok) {

            Ok -= SetBitCount[i];

            j += SetBitCount[i];

        }

        else A[j];

        

    }

  

    return OR;

}

  

int major()

{

    int N = 7;

    vector<int> A = { 1, 10, 9, 4, 5, 16, 8 };

    int Ok = 3;

  

    cout << minimumOR(A, N, Ok);

    return 0;

}

Time Complexity: O(N* log(N))
Auxiliary Area: O(N)

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments