Find a number containing N | C ++ implementation of the approach ; Function to return the string generated by appending "10" n - 1 times ; Initialising string as empty ; Function to return the decimal equivalent of the given binary string ; Initializing base value to 1 i . e 2 ^ 0 ; Function that calls the constructString and binarytodecimal and returns the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE string constructString ( ll n ) { string s = " " ; for ( ll i = 0 ; i < n ; i ++ ) { s += "10" ; } return s ; } ll binaryToDecimal ( string n ) { string num = n ; ll dec_value = 0 ; ll base = 1 ; ll len = num . length ( ) ; for ( ll i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } ll findNumber ( ll n ) { string s = constructString ( n - 1 ) ; ll num = binaryToDecimal ( s ) ; return num ; } int main ( ) { ll n = 4 ; cout << findNumber ( n ) ; return 0 ; }