Adobe Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
/*If i am getting the right question then My Answer is */
int main()
{
int endNum = 0, count = 1, result = 0;
cin>>"Enter the End Number"<<endNum;
while(count <= endNum)
{
if((count%10==3)||(count/10==3));
else
{
cout<<count<<"\t";
result++;
}
count++;
}
cout<<endl<<"Total numbers without 3 is "<<result;
return 0;
}
Here u people are checking each number.Optimize the solution,come up with some good solution...........:)
Hi Gaurav, I think u are having something in ur mind. if it is then pls share with us also.
This can be done with mathematics using java
length = log10n+1; //Total length of 'n'
sum=8 // sum of numbers without 3 and 0 starting with one digit no.
for(int i=1;i<length-2;i++)
{
sum+= sum*9; //9 is for adding 0 and excluding 3
}
//For last digit
int last = n/pow(10,length-1);
if(last<3)
sum+=last* pow(9,length-1)
if(last>=3)
sum+=(last-1)*pow(9,length-1);
Time: log10n;
Optimized solution is to subtract from n the number of cases where 3 exists. For 2 digit number, if decimal digit is greater than 3, then 3 can exist 10 times in decimal digit with 0-9 comes in 1 digit. For 1 digit, it can come till 4(0-4) ie - 03,13,23,33,43 - 5 times. In this way it can be optimized. Only in 3-4 comparisons we can find x (number of times 3 exists in the numbers from 1 - n) and then result = n - x.
@saurabh ... Why to subtract ?
I think if you are removing a element from the Decimal System .. then we should add (not subtract) the number of times that specific element was present before the given number.
like ... 3 will become 4 and the number system will look like .. 0,1,2,4,5 ... 12,14, 15, .. 29,40 ... and so on ...
@saurabh ... Why to subtract ?
I think if you are removing a element from the Decimal System .. then we should add (not subtract) the number of times that specific element was present before the given number.
like ... 3 will become 4 and the number system will look like .. 0,1,2,4,5 ... 12,14, 15, .. 29,40 ... and so on ...
int main()
{
cout<<"Decimal - 3"<<endl;
int i = 0, j = 0 , temp;
int n = 14 ;// Input your number here
while(i<=n)
{
temp = i;
// cout<<"In while1 - j :"<<j<<" temp :"<<temp<<endl;
bool Flag = false;
while(temp>0 && !Flag)
{
// cout<<" temp :"<<temp<<endl;
if(temp%10 == 3)
{
j++;
// cout<<" In while2 - j :"<<j<<endl;
Flag = true;
}
temp = temp/10;
}
i++,j++;
}
cout<<"j : "<<j-1<<endl;
return 0;
}
Suppose we consider we need to find no. of numbers from 1 t0 50 not divisible by 3.
Lets find the how many are divisible by 3 .
50 /3 =16
16 /3 =5
5 / 3 = 1
We keep on dividing the integral part of the quotient till the quotient is less than 3.
total count of numbers divisible by 3= 16+5+1= 22.
hence number not divisible by 3= 50-22 =28
In this in previous comments all are finding the numbers does not contain 3.
i don't think this is what the question says...
they want to define the new number system where 3 is not there.
So for defining new number system we have to define its new adding and subtraction properties.
0 1 2 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20.....
Addition means moving forward and subtraction means moving back.
So 1+2=4 not 3
2+2=5 not 4 as moving 2 position we reach at 5.
So i think we have to find one way to do this.
What exactly was the question ?
- ghantacoder February 29, 2012