Bit Set HackerRank Problem

The program is designed to handle bitwise operations on two BitSets. The user inputs the size of the BitSets and the number of operations to be performed. Then, for each operation, the user specifies the operation type and the BitSets to be operated on. The operation types include AND, OR, FLIP, SET, and XOR.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Bit Set HackerRank Problem HackerRank solution using CodeRankGPT:

The program uses a switch-case structure to handle different operation types. For each operation, it checks the operation type and performs the corresponding bitwise operation on the specified BitSet. If the operation is 'AND', it performs an AND operation; if 'OR', an OR operation; if 'FLIP', it flips the bits; if 'SET', it sets the bits; and if 'XOR', it performs an XOR operation. After each operation, it prints the number of set bits (cardinality) in each BitSet.


/**
 * 
 */
package com.javaaid.hackerrank.solutions.languages.java.datastructures;

import java.util.BitSet;
import java.util.Scanner;

/**
 *
 */
public class JavaBitSet {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		BitSet b1 = new BitSet(N);
		BitSet b2 = new BitSet(N);
		for (int i = 0; i < M; i++) {
			String op = sc.next();
			int x = sc.nextInt();
			int y = sc.nextInt();
			switch (op) {
			case "AND":
				if (x == 1) {
					b1.and(b2);
				} else {
					b2.and(b1);
				}
				break;
			case "OR":
				if (x == 1) {
					b1.or(b2);
				} else {
					b2.or(b1);
				}
				break;
			case "FLIP":
				if (x == 1) {
					b1.flip(y);
				} else {
					b2.flip(y);
				}
				break;
			case "SET":
				if (x == 1) {
					b1.set(y);
				} else {
					b2.set(y);
				}
				break;
			case "XOR":
				if (x == 1) {
					b1.xor(b2);
				} else {
					b2.xor(b1);
				}
				break;
			}
			System.out.println(b1.cardinality() + " " + b2.cardinality());

		}
		sc.close();
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform