Bit Masking+ Implementation (Easy understanding) | C++
I have used concept of bitmasking:\n1. Convert each row into a number(convert binary to decimal)\n2. Then we select column by using set bits of 1 to 2^(number o
I have used concept of bitmasking:\n1. Convert each row into a number(convert binary to decimal)\n2. Then we select column by using set bits of 1 to 2^(number of columns)[concept of subset generation]\n3. Then take ```AND``` and then see if the count of bits is same or not. \n\nEx: \n0 0 0 -> 0\n1 0 1 -> 5\n0 1 1 -> 3\n0 0 1 -> 1\n\n\n```\nclass Solution {\npublic:\n    int maximumRows(vector<vector<int>>& mat, int cols) {\n        \n        int n=mat.size();\n        int m=mat[0].size();\n        \n        vector<int> bit; // to store number formed from  each row\n        \n        for(int i=0;i<n;i++){\n            \n            int num=0;\n            int b=0;\n            for(int j=m-1;j>=0;j--){\n                \n        \n                int pow=(1<<b);\n    \n                num=num+(mat[i][j]*pow);// converting binary to decimal\n                b++;\n                \n            }\n            bit.push_back(num);//storing formed number in bit array\n        }\n        \n        int loop=(1<<m);\n        \n        int mx=INT_MIN;\n        for(int i=1;i<loop;i++){// loop to generate all subsets\n            int bit_count=__builtin_popcount(i);\n            \n           \n            int cnt=0;\n            if(bit_count==cols){\n                \n                for(int j=0;j<bit.size();j++){\n                    \n                    int check=bit[j]&i;\n                    int bit1=__builtin_popcount(bit[j]);\n                    int bit2=__builtin_popcount(check);\n                    \n                    \n                    if(bit1==bit2 || bit[j]==0){\n                        cnt++;\n                    }\n                }\n                \n            }\n            mx=max(cnt,mx);\n        }\n        \n        return mx;\n    }\n};\n```
['C', 'Bitmask']