• + 7 comments

    Lots of clever solutions here. Here's my refactored version after stealing some gems from these discussions. Thanks guys! ;-)

    import java.util.BitSet;
    import java.util.Scanner;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner get = new Scanner(System.in);
            int n = get.nextInt();
            int m = get.nextInt();
          
            BitSet b1 = new BitSet(n);
            BitSet b2 = new BitSet(n);
            BitSet[] bitset = new BitSet[3];
          
            bitset[1] = b1;
            bitset[2] = b2;
          
            while ( 0 < m-- ) {
              String op = get.next();
              int x = get.nextInt();
              int y = get.nextInt();
              
              switch (op) {
                case "AND":
                  bitset[x].and(bitset[y]);
                  break;
                case "OR":
                  bitset[x].or(bitset[y]);
                  break;
                case "XOR":
                  bitset[x].xor(bitset[y]);
                  break;
                case "FLIP":
                  bitset[x].flip(y);
                  break;
                case "SET":
                  bitset[x].set(y);
              }
              
              System.out.printf("%d %d%n", b1.cardinality(), b2.cardinality());
            }
        }
    }
    
    • + 1 comment

      bitset[0] wasted!!😉

    • + 1 comment

      Why is the break clause necessary?

      • + 0 comments

        cause switch will implements all cases under what it called, for example switch calls "FLIP" case, it will implement it, and then it will implement the "SET" case without break

    • + 1 comment

      You can also used a if - else ladder here then why you use a switch case here???? is there any specific reason for this???????

      • + 0 comments

        Clean code

    • + 0 comments

      simple and readable

    • + 0 comments

      i have one doubt ,we are considering n as number of bits in bitstings B1 and B2 ,in this code we are just reading n but not using n anywhere to set n as size of B1 and B2,how code takes n as a size??

    • + 2 comments

      here the same code but with comments and a YT link, you're welcome future coders

      • + 0 comments

        /* import java.util.BitSet; //this code compares two bitset arrays of 1's and 0's import java.util.Scanner;

        public class javaBitsetwork {
        
            public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);
                int n = scan.nextInt();                    //asks for size of each bitset array
                int m = scan.nextInt();                   //asks for number of operations you're gonna give it for the bitsets
        
                BitSet b1 = new BitSet(n);              //first bitset array
                BitSet b2 = new BitSet(n);              //second bitset array
                BitSet[] bitset = new BitSet[3];        //larger bitset array that the first two are going into (making an "x,y" 2D array)
        
                bitset[1] = b1;                      //puts first bitset array in larger bitset array
                bitset[2] = b2;
        
                while ( 0 < m-- ) {                 //performs the operations
                    String op = scan.next();
                    int x = scan.nextInt();
                    int y = scan.nextInt();
        
                        switch (op) {
                                case "AND":                          //everything is 0 so everything is 0
                                    bitset[x].and(bitset[y]);       //https://www.youtube.com/watch?v=4hhWVy8VtxY
                                    break;
                                case "SET":
                                    bitset[x].set(y);               //this sets x index of the big array (so small array 1), with y index of 
                                                                     //the small array to true (any value above 0 makes it true)
                                    break;
                                case "FLIP":                    //flips a bit from false to true and vice versa I assume
                                    bitset[x].flip(y);
                                    break;  
                                case "OR":                        //if one is true then they're both true
                                    bitset[x].or(bitset[y]);
                                    break;             
                                case "XOR":                     //if one is false then they're both false
                                    bitset[x].xor(bitset[y]);  
        
                        }
        
                  System.out.println("this is x:" + x + "  b1: " + b1+ "  b2: " + b2);
                  System.out.printf("%d %d%n", b1.cardinality(), b2.cardinality());  //prints out the "cardinality" aka the number of elements..
                                                                                       //in each set 
                }
                scan.close();
            }
        }*/
        
      • + 0 comments

        /* import java.util.BitSet; //this code compares two bitset arrays of 1's and 0's import java.util.Scanner;

        public class javaBitsetwork {
        
            public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);
                int n = scan.nextInt();                    //asks for size of each bitset array
                int m = scan.nextInt();                   //asks for number of operations you're gonna give it for the bitsets
        
                BitSet b1 = new BitSet(n);              //first bitset array
                BitSet b2 = new BitSet(n);              //second bitset array
                BitSet[] bitset = new BitSet[3];        //larger bitset array that the first two are going into (making an "x,y" 2D array)
        
                bitset[1] = b1;                      //puts first bitset array in larger bitset array
                bitset[2] = b2;
        
                while ( 0 < m-- ) {                 //performs the operations
                    String op = scan.next();
                    int x = scan.nextInt();
                    int y = scan.nextInt();
        
                        switch (op) {
                                case "AND":                          //everything is 0 so everything is 0
                                    bitset[x].and(bitset[y]);       //https://www.youtube.com/watch?v=4hhWVy8VtxY
                                    break;
                                case "SET":
                                    bitset[x].set(y);               //this sets x index of the big array (so small array 1), with y index of 
                                                                     //the small array to true (any value above 0 makes it true)
                                    break;
                                case "FLIP":                    //flips a bit from false to true and vice versa I assume
                                    bitset[x].flip(y);
                                    break;  
                                case "OR":                        //if one is true then they're both true
                                    bitset[x].or(bitset[y]);
                                    break;             
                                case "XOR":                     //if one is false then they're both false
                                    bitset[x].xor(bitset[y]);  
        
                        }
        
                  System.out.println("this is x:" + x + "  b1: " + b1+ "  b2: " + b2);
                  System.out.printf("%d %d%n", b1.cardinality(), b2.cardinality());  //prints out the "cardinality" aka the number of elements..
                                                                                       //in each set 
                }
                scan.close();
            }
        }*/
        
    • + 0 comments

      quite cheeky way of using BitSet array :)