🎉 One-stop destination for all your technical interview Preparation 🎉
Given a number N. The task is to generate and print all binary numbers with decimal values from 1 to N.
vector<string> generatePrintBinary(int n)
{
// Create queue and answer vector
queue<string> q;
vector<string> ans;
// Initially push one in the queue
q.push("1");
// while n is not 0 run the loop
while (n--)
{
// store front in temp and pop front
string temp = q.front();
q.pop();
// push into the vector
ans.push_back(temp);
// first push temp with 0 then with 1
q.push(temp+'0');
q.push(temp+'1');
}
return ans;
}