tinybells
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
A solution using bit manipulation:
The following code works for positive numbers. When the dividend or the divisor or both are negative, have flags to change the sign of the answer appropriately.
int divi(long long m, long long n)
{
if(m==0 || n==0 || m<n)
return 0;
long long a,b;
int f=0;
a=n;b=1;
while(a<=m)
{
b = b<<1;
a = a<<1;
f=1;
}
if(f)
{
b = b>>1;
a = a>>1;
}
b = b + divi(m-a,n);
return b;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Although very good, fails a corner case of input array having 2 zeros - that should be handled as well.
- tinybells April 09, 2014Expected Result : {3,1,4,0,0} ==> {0, 0, 0, 0, 0}
Your algo would give : {3,1,4,0,0} ==> {0, 0, 0, 12, 12}