Backtracking C++
Logic :\n1. We have to take cols no. of columns so we have choice whether to take a particular column or not.\n2. If we chose a particular column ind then we ma
Logic :\n1. We have to take **cols** no. of columns so we have choice whether to take a particular column or not.\n2. If we chose a particular column **ind** then we make all the elements of the rows in which column value is **ind**.\n3. If we do not chose this column then we will backtrack and unvisit the elements which we visited.\n\n```\nclass Solution {\npublic:\n    \n    void solve(int ind,int cnt,vector<vector<int>>& mat,int cols,int &ans,vector<vector<bool>>& vis){\n        if(ind>mat[0].size()) return;\n\t\t\n\t\t// calculating the rows which are not covered by the chosen columns and substracting from the total rows ,\n\t\t//then we will get the no. of rows covered by the selected columns and then checking for the max value of ans;\n\t\t\n        if(cnt==cols && ind==mat[0].size()){\n            int t=0;\n            for(int i=0;i<mat.size();i++){\n                \n                for(int j=0;j<mat[0].size();j++){\n                    if(vis[i][j]==0&&mat[i][j]==1){\n                        t++;\n                        break;\n                    }\n                }\n            }\n            if(ans<mat.size()-t) ans=mat.size()-t;\n            return;\n        }\n        //taken the current column\n        for(int i=0;i<mat.size();i++){\n            vis[i][ind]=1;\n        }\n        solve(ind+1,cnt+1,mat,cols,ans,vis);\n        //not taken the current column \n        for(int i=0;i<mat.size();i++){\n            vis[i][ind]=0;\n        }\n        solve(ind+1,cnt,mat,cols,ans,vis);\n        \n    }\n    int maximumRows(vector<vector<int>>& mat, int cols) {\n        int n=mat.size();\n        int m=mat[0].size();\n        vector<vector<bool>> vis(n,vector<bool>(m,false));\n        int ans=0;\n        solve(0,0,mat,cols,ans,vis);\n        return ans;\n    }\n};\n```
['C']