Java BitSet || Hackerrank Solution

 Java BitSet:

Java's BitSet class implements a vector of bit values (i.e.:  () or  ()) that grows as needed, allowing us to easily manipulate bits while optimizing space (when compared to other collections). Any element having a bit value of  is called a set bit.

Given  BitSets,  and , of size  where all bits in both BitSets are initialized to , perform a series of  operations. After each operation, print the number of set bits in the respective BitSets as two space-separated integers on a new line.

Input Format

The first line contains  space-separated integers,  (the length of both BitSets  and ) and  (the number of operations to perform), respectively.
The  subsequent lines each contain an operation in one of the following forms:

In the list above,  is the integer  or , where  denotes  and  denotes .
 is an integer denoting a bit's index in the BitSet corresponding to .

For the binary operations , and , operands are read from left to right and the BitSet resulting from the operation replaces the contents of the first operand. For example:

AND 2 1

 is the left operand, and  is the right operand. This operation should assign the result of  to .

Constraints

Output Format

After each operation, print the respective number of set bits in BitSet  and BitSet  as  space-separated integers on a new line.

Sample Input

5 4
AND 1 2
SET 1 4
FLIP 2 2
OR 2 1

Sample Output

0 0
1 0
1 1
1 2

Explanation

Initially: , and . At each step, we print the respective number of set bits in  and  as a pair of space-separated integers on a new line.




The number of set bits in  and  is .


Set  to  ().
.
The number of set bits in  is  and  is .


Flip  from  () to  ().
.
The number of set bits in  is  and  is .


.
.
The number of set bits in  is  and  is .


  • Solution:

import java.util.Scanner;

import java.util.BitSet;


public class Solution {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int N = scan.nextInt();

        int M = scan.nextInt();

        BitSet B1 = new BitSet(N);

        BitSet B2 = new BitSet(N);

        while (M-- > 0) {

            String str = scan.next();

            int a      = scan.nextInt();

            int b      = scan.nextInt();

            switch (str) {

                case "AND":

                    if (a == 1) {

                        B1.and(B2);

                    } else {

                        B2.and(B1);

                    }

                    break;

                case "OR":

                    if (a == 1) {

                        B1.or(B2);

                    } else {

                        B2.or(B1);

                    }

                    break;

                case "XOR":

                    if (a == 1) {

                        B1.xor(B2);

                    } else {

                        B2.xor(B1);

                    }

                    break;

                case "FLIP":

                    if (a == 1) {

                        B1.flip(b);

                    } else {

                        B2.flip(b);

                    }

                    break;

                case "SET":

                    if (a == 1) {

                        B1.set(b);

                    } else {

                        B2.set(b);

                    }

                    break;

                default:

                    break;

            }

            System.out.println(B1.cardinality() + " " + B2.cardinality());

        }

        scan.close();

    }

}


🙏THANKS FOR VISIT🙏


Java BitSet || Hackerrank Solution Java BitSet || Hackerrank Solution Reviewed by CodexRitik on November 07, 2020 Rating: 5

1 comment:

Powered by Blogger.