Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Java BitSet problem solution

YASH PAL, 31 July 2024

In this HackerRank Java BitSet problem in the java programming language you have Given 2 BitSets, B1 and B2, of size N where all bits in both BitSets are initialized to 0, perform a series of M operations. After each operation, print the number of set bits in the respective BitSets as two space-separated integers on a new line.

HackerRank Java BitSet problem solution

HackerRank Java BitSet problem solution.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    int m = scan.nextInt();

    BitSet b1 = new BitSet(n);
    BitSet b2 = new BitSet(n);

    for (int i = 0; i < m; i++) {
      String opcode = scan.next();
      int num1 = scan.nextInt();
      int num2 = scan.nextInt();

      switch(opcode) {
        case "AND":
          if (num1 == 1 && num2 == 2) {
            b1.and(b2);
          } else if (num1 == 2 && num2 == 1) {
            b2.and(b1);
          } else if (num1 == 1 && num2 == 1) {
            b1.and(b1);
          } else if (num1 == 2 && num2 == 2) {
            b2.and(b2);
          }
          break;
        case "OR":
          if (num1 == 1 && num2 == 2) {
            b1.or(b2);
          } else if (num1 == 2 && num2 == 1) {
            b2.or(b1);
          } else if (num1 == 1 && num2 == 1) {
            b1.or(b1);
          } else if (num1 == 2 && num2 == 2) {
            b2.or(b2);
          }
          break;
        case "XOR":
          if (num1 == 1 && num2 == 2) {
            b1.xor(b2);
          } else if (num1 == 2 && num2 == 1) {
            b2.xor(b1);
          } else if (num1 == 1 && num2 == 1) {
            b1.xor(b1);
          } else if (num1 == 2 && num2 == 2) {
            b2.xor(b2);
          }
          break;
        case "FLIP":
          if (num1 == 1) {
            b1.flip(num2);
          } else if (num1 == 2) {
            b2.flip(num2);
          }
          break;
        case "SET":
          if (num1 == 1) {
            b1.set(num2);
          } else if (num1 == 2) {
            b2.set(num2);
          }
          break;
      }
      System.out.println(b1.cardinality() + " " + b2.cardinality());
    }
    }
}

Second solution

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        BitSet[] a = new BitSet[] {null, new BitSet(n), new BitSet(n)};
        int m = sc.nextInt();
        
        for (int k = 0; k < m; k++) {
            String cmd = sc.next();
            int k1 = sc.nextInt();
            int k2 = sc.nextInt();
            if (cmd.equals("AND")) {
                a[k1].and(a[k2]);
            } else if (cmd.equals("OR")) {
                a[k1].or(a[k2]);
            } else if (cmd.equals("XOR")) {
                a[k1].xor(a[k2]);
            } else if (cmd.equals("FLIP")) {
                a[k1].flip(k2);
            } else if (cmd.equals("SET")) {
                a[k1].set(k2);
            }
            System.out.println(a[1].cardinality() + " " + a[2].cardinality());
        }
    }
}

The solution in java8 programming

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int M = in.nextInt();
        BitSet[] b = new BitSet[]{new BitSet(N), new BitSet(N)};
        for(int i = 0; i < M; i++) {
            String q = in.next();
            int left = in.nextInt() - 1;
            int right = in.nextInt() - 1;
            if(q.equals("AND")) {
                b[left].and(b[right]);
            }
            if(q.equals("OR")) {
                b[left].or(b[right]);
}
            if(q.equals("XOR")) {
                b[left].xor(b[right]);
}   
            if(q.equals("FLIP")) {
                b[left].flip(N - right - 1);
            }
            if(q.equals("SET")) {
                b[left].set(N - right - 1);
            }
            System.out.println(b[0].cardinality() + " " + b[1].cardinality());
        }
    }
}
coding problems solutions Hackerrank Problems Solutions Java Programming Tutorials

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes