<p>Given an integer array <code>arr</code>, return <em>the number of distinct bitwise ORs of all the non-empty subarrays of</em> <code>arr</code>.</p>

<p>The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.</p>

<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is only one possible result: 0.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,1,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 3:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    int subarrayBitwiseORs(vector<int>& arr) {
        int n=arr.size();
        
        // Intuition -> we will take the consider ith element as the end of the 
        
        // subarray , Now we will take prev_or -> checking  the OR from i-1th value 
        
        // to index j(backward direction), where the OR of the (i-1th to jth index) & (ith to jth index)
        
        // we have to check until both equals , till that element that subarray
        
        // will make its effect.
        
        set<int> st;
        
        for(int i=0;i<arr.size();i++)
        {
            // take this element in curr_OR , now calculate OR value in backward 
            
            // direction till that both curr_OR & prev_OR equals 
            
            // Why we consider till only that -> because curr_subarray considering 
            
            // arr[i] , then give same OR value which is previously calculated 
            
            // from arr[i-1] , so no new subarray value will be founded.
            
            
            int curr_OR=arr[i];
            int prev_OR=0;
            int j=i-1;
            st.insert(curr_OR);
            
            while(j>=0 && curr_OR != prev_OR)
            {
                curr_OR= curr_OR | arr[j];
                prev_OR= prev_OR | arr[j];
                st.insert(curr_OR);
                j--;
            }
        }
        
        return (int) st.size();
    }
};
115187