Skip to content

Instantly share code, notes, and snippets.

@svraghavan
Created December 10, 2019 01:16
Show Gist options
  • Select an option

  • Save svraghavan/da1f3ce292bb4d892a6fa0fd7683ad8d to your computer and use it in GitHub Desktop.

Select an option

Save svraghavan/da1f3ce292bb4d892a6fa0fd7683ad8d to your computer and use it in GitHub Desktop.
#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