LeetCode | Bitwise ORs of Subarrays

So this problem is bugging me for a while,

class Solution {
public:
    int subarrayBitwiseORs(vector<int>& A) {
        if(A.size()<=1) return A.size();
        set<int> s;
        for(int i=0;i<A.size();i++){
            s.insert(A[i]);
            int res=A[i];
            for(int j=i+1;j<A.size();j++){
                res|=A[j];
                s.insert(res);
            }
        }
        return s.size();
    }
};

This results in TLE as expected, more efficient solution ideas are welcome

Bro there are many solutions discuss in discussion tab on leetcode, please read them.

1 Like

yeah i know, the problem is that the top answers are usually not the most useful ones…and i need to spend some time to surf through the answers.

You posted here 3 hours before, still haven’t received any answer. I guess surfing through solutions would be more time efficient than this.

1 Like

Yeah, I mean I did not spend the 2 hours just waiting for the answer, but eventually I have to search myself ig

Completely disagree, they have very clear explaination u just have to read bro :slight_smile:

https://leetcode.com/problems/bitwise-ors-of-subarrays/discuss/165881/C%2B%2BJavaPython-O(30N)

2 Likes