@@ -0,0 +1,1902 @@
+// Naive solution; time complexity is O(N%5E3), space complexity is O(W), where W is the maximum%0A// number of bits that a number is represented by in array A.%0Aclass Solution %7B%0A  public int subarrayBitwiseORs(int%5B%5D A) %7B%0A    HashSet%3CInteger%3E orResults = new HashSet%3CInteger%3E();%0A    for (int i = 0; i %3C A.length; i++) %7B%0A      for (int j = i; j %3C A.length; j++) %7B%0A        int tmp = 0;%0A        for (int k = i; k %3C= j; k++) %7B%0A          tmp %7C= A%5Bk%5D;%0A        %7D%0A        orResults.add(tmp);%0A      %7D%0A    %7D%0A    return orResults.size();%0A  %7D%0A%7D%0A%0A// Slightly better solution.%0A// Time complexity is O(N%5E2) and space complexity is O(W), where W is the meximum number of bits%0A// that a number is represented by in array A.%0Aclass Solution %7B%0A  public int subarrayBitwiseORs(int%5B%5D A) %7B%0A    HashSet%3CInteger%3E results = new HashSet%3CInteger%3E(32);%0A    for (int i = 0; i %3C A.length; i++) %7B%0A      int res = A%5Bi%5D;%0A      for (int j = i; j %3C A.length; j++) %7B%0A        res %7C= A%5Bj%5D;%0A        results.add(res);%0A      %7D%0A    %7D%0A    return results.size();%0A  %7D%0A%7D%0A%0A// Even better version.%0A// Time complexity is O(N * W), where N is the length of the array A and W is the maximum number%0A// of bits that a number is represented by in array A.%0A// Space complexity is O(W), where W is %22see above%22. Here we reuse hashsets.%0Aclass Solution %7B%0A  public int subarrayBitwiseORs(int%5B%5D A) %7B%0A    HashSet%3CInteger%3E results = new HashSet%3CInteger%3E(32); // there are at most 32 different results%0A    HashSet%3CInteger%3E curr = new HashSet%3CInteger%3E(32);%0A    HashSet%3CInteger%3E tmp = new HashSet%3CInteger%3E(32);%0A%0A    for (int i = 0; i %3C A.length; i++) %7B%0A      curr.add(A%5Bi%5D);%0A      tmp.clear();%0A%0A      for (int value : curr) %7B%0A        tmp.add(value %7C A%5Bi%5D);%0A      %7D%0A%0A      results.addAll(tmp);%0A%0A      // swap curr and tmp instead of creating a new set every time.%0A      HashSet%3CInteger%3E swap = curr;%0A      curr = tmp;%0A      tmp = swap;%0A    %7D%0A    return results.size();%0A  %7D%0A%7D%0A

add solution