Created
December 10, 2019 01:16
-
-
Save svraghavan/da1f3ce292bb4d892a6fa0fd7683ad8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| using namespace std; | |
| vector<string> store; | |
| void getStrings( string s, int digitsLeft ) | |
| { | |
| if( digitsLeft == 0 ) | |
| store.push_back( s ); | |
| else | |
| { | |
| getStrings( s + "0", digitsLeft - 1 ); | |
| getStrings( s + "1", digitsLeft - 1 ); | |
| } | |
| } | |
| int count_ones(string s) { | |
| size_t count = static_cast<size_t>(std::count_if(s.begin(), s.end(), [](char c) { | |
| if (c == '1') return true; | |
| return false; | |
| })); | |
| return (int)count; | |
| } | |
| int main() { | |
| //total length | |
| const int M = 4; | |
| //total ones | |
| const int N = 2; | |
| getStrings("",M); | |
| for (auto i: store) { | |
| if (count_ones(i) == N) | |
| std::cout << i << ' '; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment