We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Java
  3. Data Structures
  4. Java BitSet
  5. Discussions

Java BitSet

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 98 Discussions, By:

votes

Please Login in order to post a comment

  • tomkri76 5 years ago+ 0 comments

    Using lambdas:

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int setSize = sc.nextInt();
        int noOps = sc.nextInt();
        BitSet[] bitSets = new BitSet[]{new BitSet(setSize), new BitSet(setSize)};
        Map<String, BiConsumer<Integer,Integer>> ops = new HashMap<>();
        ops.put("AND", (index1, index2) -> bitSets[index1-1].and(bitSets[index2-1]));
        ops.put("OR", (index1, index2) -> bitSets[index1-1].or(bitSets[index2-1]));
        ops.put("XOR", (index1, index2) -> bitSets[index1-1].xor(bitSets[index2-1]));
        ops.put("SET", (index1, index2) -> bitSets[index1-1].set(index2));
        ops.put("FLIP", (index1, index2) -> bitSets[index1-1].flip(index2));
        for (int i = 0; i < noOps; i++){
            ops.get(sc.next()).accept(sc.nextInt(), sc.nextInt());    
            System.out.println(bitSets[0].cardinality() + " " + bitSets[1].cardinality());
        }
    }
    
    66|
    Permalink
  • kevinbrewer04 4 years ago+ 0 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());
            }
        }
    }
    
    42|
    Permalink
  • simongarton 4 years ago+ 0 comments

    I think there's a tricky "bug" here which can catch out the unwary, particularly those that didn't know about BitSet.cardinality().

    The bit addresses for SET and FLIP : are they 0 based or 1 based ?

    The 7th operation on TestCase 2 is SET 1 0, which implies it's 0 based. The 4840th operation on TestCase 6 - with a BitSet size of 523 - is FLIP 2 523, which implies it's 1 based.

    The BitSet automatically grows to a nice size for storage : so new BitSet(523) actually gives you a BitSet of 576 bits long - which means it works with either 0 based or 1 based, and you can set or flip bits at higher addresses than you should really be able to.

    I initially didn't know about BitSet.cardinality(), so was manually counting the bits in the original range : which meant I looped over 0 -> 522 and so didn't picked up the flipped bit in test set 6.

    Can I suggest the datasets be reviewed, so they are consistently 0 based ? In which case Test Case 6 (at least) is invalid ?

    6|
    Permalink
  • iyontihiy 4 years ago+ 0 comments

    Didn't want to use switch case. Some reflection instead.

    import java.util.ArrayList;
    import java.util.BitSet;
    import java.util.Scanner;
    
    public class Solution {
    
        public static void main(String[] args) throws Exception {
            ArrayList<BitSet> bitSets = new ArrayList<>();
            Scanner in = new Scanner(System.in);
            bitSets.add(new BitSet(in.nextInt()));
            bitSets.add(new BitSet(in.nextInt()));
            while (in.hasNext()) {
                String operation = in.next();
                int left = in.nextInt();
                int right = in.nextInt();
    
                try {
                    BitSet.class.getMethod(operation.toLowerCase(), BitSet.class).invoke(bitSets.get(left - 1), bitSets.get(right - 1));
                } catch (NoSuchMethodException e) {
                    BitSet.class.getMethod(operation.toLowerCase(), int.class).invoke(bitSets.get(left - 1), right);
                } finally {
                    System.out.println(bitSets.get(0).cardinality() + " " + bitSets.get(1).cardinality());
                }
            }
        }
    }
    
    4|
    Permalink
  • ilashree 5 years ago+ 0 comments

    i am getting "error : illegal character :\127" please tell me what is wrong with my code

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        BitSet b1 = new BitSet(size);
        BitSet b2 = new BitSet(size);
        int op = sc.nextInt();
        String s = null;
        int i,j;
        while(op>0){
            s = sc.next();
            i = sc.nextInt();
            j = sc.nextInt();
            switch(s){
                case("AND") : if(i==1) { b1.and(b2);} else b2.and(b1);
                  break;
                case("OR") : if(i==1) { b1.or(b2);} else b2.or(b1);
                  break;
                case("XOR") :  if(i==1) { b1.xor(b2);} else b2.xor(b1);
                  break;
                case("FLIP") : if(i==1) { b1.flip(j);} else b2.flip(j);
                  break;
                case("SET") : if(i==1) { b1.set(j);} else b2.set(j);
                break;
                default : ;
            }
            System.out.println(b1.cardinality()+"  "+b2.cardinality());
            op--;
        }
    
    }
    
    4|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature