Sunday, September 25, 2022
HomeSoftware DevelopmentDiscover final component in Array fashioned from bitwise AND of array parts

Discover final component in Array fashioned from bitwise AND of array parts


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given an array A[] of measurement N, the duty is to seek out the final remaining component in a brand new array B containing all pairwise bitwise ANDs of parts from A i.e., B consists of N⋅(N − 1) / 2 parts, every of the shape Ai & Aj for some 1 ≤ i < j ≤ N. And we will carry out the next operation any variety of occasions on a brand new array until there is just one component remaining equivalent to:

  • Let X and Y be the present most and minimal parts of array B respectively and take away X and Y from array B and insert X|Y into it.

Examples:

Enter: A[] = {2, 7, 1}
Output: 3
Rationalization: Array B might be [A1 & A2, A1 & A3, A2 & A3] = [2 & 7, 2 & 1, 7 & 1] = [2, 0, 1].
Then, we do the next operations on B:
Take away 2 and 0 from B and insert 2|0=2 into it. Now, B=[1, 2].
Take away 2 and 1 from B and insert 2|1=3 into it. Now, B=[3].
The final remaining component is thus 3.

Enter: A[] = {4, 6, 7, 2}
Output: 6

Method: The issue might be solved primarily based on the next commentary:

Observations:

  • The property of bitwise or is that if we’re performing X | Y, bit i might be set if atleast one in all bit i in X or Y is ready.
  • This leads us to essentially the most essential commentary of the issue, for each bit i, if bit i is ready in atleast one in all B1, B2, …BN⋅(N−1)/2, then bit i might be set within the last remaining component of B when all of the operations are carried out. We don’t want to fret about how the operations are carried out.
  • For bit i to be set in atleast one component of B, we have to have atleast 2 parts in A say j and ok the place Aj and Aok each have bit i set. This units the bit i in Aj & Aok.
  • Now we’ve the next resolution, iterate over each legitimate bit i, rely the variety of parts in array A which have bit i set. If this rely is bigger than 1, the ultimate reply may have bit i set else it will likely be unset.

Observe the steps talked about under to implement the thought:

  • Create an array of measurement 32 to retailer the rely of bits set in ith place.
  • Traverse the array and for every array component:
    • Discover the positions through which the bit is ready.
    • Increment the set bit rely for that place by 1.
  • Traverse the array storing the set bit rely.
    • If the rely is no less than 2, set that bit within the last reply.
  • Return the quantity fashioned because the required reply.

Under is the implementation of the above strategy:

Java

  

import java.io.*;

import java.util.*;

  

public class GFG {

  

    

    

    public static int discover(int a[], int n)

    {

        int rely = 0;

        int b[] = new int[33];

        for (int bit = 30; bit >= 0; bit--) {

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

                if (((1 << bit) & (a[i])) != 0) {

                    b[bit]++;

                }

            }

        }

        for (int bit = 30; bit >= 0; bit--) {

            if (b[bit] > 1)

                rely = rely + (1 << bit);

        }

        return rely;

    }

  

    

    public static void important(String[] args)

    {

        int A[] = { 2, 7, 1 };

        int N = A.size;

  

        

        System.out.println(discover(A, N));

    }

}

Time Complexity: O(N) 
Auxiliary Area: O(1)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments