Tavas and SaDDas
You are given a lucky number n. Lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
If we sort all lucky numbers in increasing order, what's the 1-based index of n?
Tavas is not as smart as SaDDas, so he asked you to do him a favor and solve this problem so he can have his headphones back.
Input
The first and only line of input contains a lucky number n (1 ≤ n ≤ 109).
Output
Print the index of n among all lucky numbers.
Sample test(s)
input
4
output
1
input
7
output
2
input
77
output
6
####################################edtorial ########################
in this quetion we need to find different possible arrangement of degits 4 and 7
of length 1 to len-1 where len = string length (max up to 9 in this prob )
as we can see that for length 1 there are 2 possibilities 4,7
for length 2 there are 4 possibilities 44,47,74,77 for length = 3 there are 8 possibilities 444,447,474,477,744,747,774,777 similarly for len=4 there are pow(4,2) no possible ..
now suppose last no is given 77744 its length is 6 means it certainly include all possible combination till length 5 , now we need to just decide possible combinations of length 6 which is less than or equal to 77744
for that we iterate from length i=0,to i=len if it position contain 7 means all position greater than i and equal to len can contain any no 7 or 4
ex - 444744
since 4th position from left have 7 so it gurant that 5th and 6th position can contain its all possile combinations and they will be less than 444744 ie -- 444444,444447,444474,444477..
so it is clear that if ith position is 7 than all string from index i+1 to len can contain all of its combinations . using this concept we can calculate the no of possible arrangement of lengh equal to len and value less than equal to given no ..
finally add up result of 1 to len-1 and result of possible arrangement of length = len ..
########################################code ##############################
#include<bits/stdc++.h>
using namespace std;
int main()
{
char sre[11];
cin>>sre;
int klen=strlen(sre),c=0,d=0;
for(int i=1;i<=klen-1;i++)// combination till len-1 is must covered soo count it
c+=pow(2,i);
int rer=pow(2,klen);// combination possible of lenth =len
int j=0;
while(j<klen)
{
if(sre[j]-'0'==7)// caculating
{
d+=rer/2;
}
rer/=2;
j++;
}
cout<<c+d+1;
return 0;
}
################################### GRAPHICAL APPROACH ###############
APPROACH -- start form a empty queue and initially push 4 and 7 in the queue now pop 1 element from queue and multiply it with 10 and add 4 and if this no is < =[ maximum no than push it . anain also multiply tyhat no with 10 and add 7 and if that no is less than max num ber than push it in the queue ..
continue untill queue is not empty ...
EX-
let maximum no= 47
initially push 4 and 7 and count of number pushed is 2 now pop 1 no ie 4 and try to push 10*4+4 and 10*4+7 if any no is less than or equal to 47 push it so push 44 and 47 and count =4 now pop 7 and try to push 74 and 77 no one will be pushed , now pop 44 and try to push 447 and 444 to push no one can be pushed now pop 47 and try to push 477 and 474 to push and no one can push finally only 4 no can push soo rank of 47 is 4 ..
###############CODE ###################################################
#include <bits/stdc++.h>using namespace std;
long long int mx=1e10;
long long int n;
int bfs()
{
int ct=0;
queue<long long int> q;
q.push(0);
while(!q.empty())
{
long long int x,y;
x=q.front();
q.pop();
y=x*10+4;
if(y==n)
return ct+1;
if(y<mx)
{
ct++;
q.push(y);
}
y=x*10+7;
if(y==n)
return ct+1;
if(y<mx)
{
ct++;
q.push(y);
}
}
}
int main() {
cin>>n;
cout<<bfs()<<endl;
// your code goes here
return 0;
}
No comments:
Post a Comment