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. 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 hackerrank solutions java