We have an array arr of non-negative integers.

For every (contiguous) subarray sub = [arr[i], arr[i + 1], ..., arr[j]] (with i &lt;= j), we take the bitwise OR of all the elements in sub, obtaining a result arr[i] | arr[i + 1] | ... | arr[j].

Return the number of possible results. Results that occur more than once are only counted once in the final answer

&nbsp;
Example 1:

Input: arr = [0]
Output: 1
Explanation: There is only one possible result: 0.


Example 2:

Input: arr = [1,1,2]
Output: 3
Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
These yield the results 1, 1, 2, 1, 3, 3.
There are 3 unique values, so the answer is 3.


Example 3:

Input: arr = [1,2,4]
Output: 6
Explanation: The possible results are 1, 2, 3, 4, 6, and 7.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 5 * 104
	0 &lt;= nums[i]&nbsp;&lt;= 109


class Solution:
    def subarrayBitwiseORs(self, arr: List[int]) -> int:
        
        
        ans=set(arr)
        
        # each element is a subarry
        
        
        one = set()
        
        # to get the ans for the subarray of  size >1
        # starting from 0th element to the ending element
        
        
        one.add(arr[0])
        
        for i in  range(1,len(arr)):
            
            two=set()
            
            for j in one:
                
                two.add(j |  arr[i])
                
                # subarray from the element in one set to the current ele(i th one)
                
                ans.add(j| arr[i])
                
            
            two.add(arr[i])
            
            # adding curr element to set two so that from next iteration we can take sub array starting from curr element 
            
            one = two
            
        return len(ans)
        
class Solution {
    public int subarrayBitwiseORs(int[] arr) {
        int n = arr.length;
        Set<Integer> s = new HashSet();
        LinkedList<Integer> queue = new LinkedList();
        for(int i = 0; i< n; i++){
            int size = queue.size();
            if(!queue.contains(arr[i])){
                queue.offer(arr[i]);
                s.add(arr[i]);
            }
            int j = 0;
            while(j<size){
                int tmp = queue.poll()|arr[i];
                if(!queue.contains(tmp)){
                    queue.offer(tmp);
                    s.add(tmp);
                }
                j++;
            }
        }
        return s.size();
    }
}
class Solution {
public:
    int subarrayBitwiseORs(vector<int>& arr) {
        vector<int>s;
        int l=0;
        for(int a:arr) {
            int r=s.size();
            s.push_back(a);
            for(int i=l;i<r;i++)
              if(s.back()!=(s[i]|a))
                s.push_back(s[i] | a);
              l=r;
            }
    return unordered_set<int>(begin(s), end(s)).size();
    }
};
/** https://leetcode.com/problems/bitwise-ors-of-subarrays/
 * @param {number[]} arr
 * @return {number}
 */
var subarrayBitwiseORs = function(arr) {
  // Hashset to store the unique bitwise
  this.uniqueBw = new Set();
  
  // Dynamic programming
  dp(arr, arr.length - 1);
  
  return this.uniqueBw.size;
};

var dp = function(arr, currIdx) {
  // Base, reach beginning of the array
  if (currIdx === 0) {
    // Store the value to unique bitwise, since it's only single number, we store the actual value
    this.uniqueBw.add(arr[0]);
    
    // Return array
    return [arr[0]];
  }
  
  // DP to previous index
  let prev = dp(arr, currIdx - 1);
  
  // Number at current index
  let firstBw = arr[currIdx];
  
  // Add number at current index to hashset, since it's only single number, we store the actual value
  this.uniqueBw.add(firstBw);
  
  // Another hashset to store the result of bitwise operation between number at current index with result from previous index
  let currRes = new Set();
  currRes.add(firstBw);
  
  // Loop through result form previous index
  for (let i = 0; i < prev.length; i++) {
    // Perform bitwise operation OR
    let curr = arr[currIdx] | prev[i];
    
    // Add to unique bitwise collection
    this.uniqueBw.add(curr);
    
    // Add to current result
    currRes.add(curr);
  }

  // Return current result as an array
  return [...currRes];
};
Bitwise ORs of Subarrays