**Java BitSet**

### Problem Statement :

Java's BitSet class implements a vector of bit values (i.e.: false (0) or true (1)) 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 1 is called a set bit. 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. Input Format The first line contains 2 space-separated integers, N (the length of both BitSets B1 and B2) and M (the number of operations to perform), respectively. The M subsequent lines each contain an operation in one of the following forms: AND <set><set> OR <set><set> XOR <set><set> FLIP<set><index> SET <set><index> In the list above, <set> is the integer 1 or 2, where 1 denotes B1 and 2 denotes B2. <index> is an integer denoting a bit's index in the BitSet corresponding to <set>. For the binary operations AND, OR, and XOR, 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 B2 is the left operand, and B1 is the right operand. This operation should assign the result of B2^B1 to B2. Constraints 1<=N<=1000 1<=M<=10000 Output Format After each operation, print the respective number of set bits in BitSet B1 and BitSet B2 as 2 space-separated integers on a new line.

### Solution :

` ````
Solution in C :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
public class Solution {
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine().trim();
StringTokenizer st=new StringTokenizer(s);
int N=Integer.parseInt(st.nextToken());
int M=Integer.parseInt(st.nextToken());
BitSet bit1=new BitSet(N);
BitSet bit2=new BitSet(N);
while(M-->0)
{
s=br.readLine().trim();
st=new StringTokenizer(s);
String ins=st.nextToken();
int a=Integer.parseInt(st.nextToken());
int b=Integer.parseInt(st.nextToken());
switch(ins)
{
case "AND":
if(a==1)
bit1.and(bit2);
else
bit2.and(bit1);
break;
case "OR":
if(a==1)
bit1.or(bit2);
else
bit2.or(bit1);
break;
case "XOR":
if(a==1)
bit1.xor(bit2);
else
bit2.xor(bit1);
break;
case "FLIP":
if(a==1)
bit1.flip(b);
else
bit2.flip(b);
break;
case "SET":
if(a==1)
bit1.set(b);
else
bit2.set(b);
break;
}
System.out.println(bit1.cardinality()+" "+bit2.cardinality());
}
}
}
```

## View More Similar Problems

## Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →## Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →## Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →## Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →## Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →## Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →