-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankJava0040.java
More file actions
63 lines (57 loc) · 1.29 KB
/
HackerrankJava0040.java
File metadata and controls
63 lines (57 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.BitSet;
import java.util.Scanner;
public class HackerrankJava0040 {
public static void main(String[] args) {
// Java BitSet
// https://www.hackerrank.com/challenges/java-bitset/problem?isFullScreen=true
Scanner scanner = new Scanner(System.in);
int sizeOfSets = scanner.nextInt();
int numberOfOperations = scanner.nextInt();
BitSet bitSet1 = new BitSet(sizeOfSets);
BitSet bitSet2 = new BitSet(sizeOfSets);
for (int i = 0; i < numberOfOperations; i++) {
String operation = scanner.next();
int set1 = scanner.nextInt();
int set2 = scanner.nextInt();
switch (operation) {
case "AND":
if (set1 == 1) {
bitSet1.and(bitSet2);
} else {
bitSet2.and(bitSet1);
}
break;
case "OR":
if (set1 == 1) {
bitSet1.or(bitSet2);
} else {
bitSet2.or(bitSet1);
}
break;
case "XOR":
if (set1 == 1) {
bitSet1.xor(bitSet2);
} else {
bitSet2.xor(bitSet1);
}
break;
case "FLIP":
if (set1 == 1) {
bitSet1.flip(set2);
} else {
bitSet2.flip(set2);
}
break;
case "SET":
if (set1 == 1) {
bitSet1.set(set2);
} else {
bitSet2.set(set2);
}
break;
}
System.out.println(bitSet1.cardinality() + " " + bitSet2.cardinality());
}
scanner.close();
}
}