Interview Question
@ak
what we we have to find whether the number is divisible by 3 or we want to implement this number /3.
for checking divisibility solution given by "kiran" is good.
Otherwise. (n>>1) -1
we can definitely use repeated subtraction to do the division.
But in question it is given that itoa() is availbale.
What is the use of this information here ?
Assuming it is a positive integer and you are looking for integer (i.e. 5/3 = 1 and not 1.666666) then we can use binary search.
You double your estimate(x) for n/3 till you hit one for which x+x+x>n. Now you can binary search the range based on the previous value and current value of x.
convert int to char* and add all digits.. continue this recursively until the sum reaches 1 digit. if the sum is 3/6/9, then it is divisible by 3
- kiran April 28, 2010