I am getting TLE in Leetcode on question 29. Please help me optimize.

I am using the most efficient code that I could understand. Please suggest how to optimize it for the test case where the dividend is int_max and divisor is -1.
public int divide(int dividend, int divisor) {
// if(di(1<<31))
// if ((dividend < 0) ^ (divisor < 0)) {
// negative = 1;
// dividend = Math.abs(dividend);
// divisor = Math.abs(divisor);
// } else if ((dividend < 0) && (divisor < 0)) {
// dividend = Math.abs(dividend);
// divisor = Math.abs(divisor);
// }
// while (dividend >= divisor) {
// dividend -= divisor;
// answer++;
// }
// if (negative == 1) {
// answer *= -1;
// }
// ;
// return answer;
int answer = 0, negative = 0;
if ((dividend < 0) ^ (divisor < 0))
negative = 1;
if ((dividend == (1 << 31)) && (divisor == -1))
answer = ((1 << 31) - 1);
else {
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
while (dividend > divisor) {
int temp_divisor = divisor, counter = 0;
for (; dividend >= temp_divisor; counter++) {
temp_divisor = temp_divisor << 1;
}
counter--;
temp_divisor = temp_divisor >> 1;
dividend -= temp_divisor;
answer += (Math.pow((int) 2, (int) counter));
System.out.println(temp_divisor);
System.out.println(dividend);
System.out.println(answer);
System.out.println(counter + "\n");
}
}
if (negative == 1) {
answer *= -1;
}
return answer;
}
public int divide(int dividend, int divisor) {
// if(di(1<<31))
// if ((dividend < 0) ^ (divisor < 0)) {
// negative = 1;
// dividend = Math.abs(dividend);
// divisor = Math.abs(divisor);
// } else if ((dividend < 0) && (divisor < 0)) {
// dividend = Math.abs(dividend);
// divisor = Math.abs(divisor);
// }
// while (dividend >= divisor) {
// dividend -= divisor;
// answer++;
// }
// if (negative == 1) {
// answer *= -1;
// }
// ;
// return answer;
int answer = 0, negative = 0;
if ((dividend < 0) ^ (divisor < 0))
negative = 1;
if ((dividend == (1 << 31)) && (divisor == -1))
answer = ((1 << 31) - 1);
else {
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
while (dividend > divisor) {
int temp_divisor = divisor, counter = 0;
for (; dividend >= temp_divisor; counter++) {
temp_divisor = temp_divisor << 1;
}
counter--;
temp_divisor = temp_divisor >> 1;
dividend -= temp_divisor;
answer += (Math.pow((int) 2, (int) counter));
System.out.println(temp_divisor);
System.out.println(dividend);
System.out.println(answer);
System.out.println(counter + "\n");
}
}
if (negative == 1) {
answer *= -1;
}
return answer;
}
5 Replies
JavaBot
JavaBot4w ago
This post has been reserved for your question.
Hey @theash2473! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
theash2473
theash2473OP4w ago
The case is giving int_min when it multiplies int_max by 2 in the for loop which should be the condition at which it gets out of the loop but it is creating infinite loop instead.
JavaBot
JavaBot4w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
theash2473
theash2473OP4w ago
I am not able to get the code right. I tried optimizing the code but I can't get to the correct solution. This is the current one I am having. It seems that the most optimized code is the only one which is the solution. No other code will be able to work.
public static int divide(int dividend, int divisor) {
int answer = 0, negative = 0;
if ((dividend < 0) && (divisor > 0))
negative = 1;
if ((dividend == (1 << 31)) && (divisor == -1))
answer = ((1 << 31) - 1);
else {
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
while (dividend - divisor > 0) {// we cannot do dividend>divisor for the dividend = int_min and divisor = 1
// case
int temp_divisor = divisor, counter = 0;
for (; (counter <= 31) && (dividend - temp_divisor >= 0); counter++) {
if (counter == 30) {
System.out.println("Edge case executed");
counter++;
temp_divisor = dividend + 1;
break;
}
temp_divisor = temp_divisor << 1;
System.out.println("temp_divisor is: " + temp_divisor + " power of 2 is: " +
(counter + 1) + " counter is: " + counter);
}
if (temp_divisor != Integer.MAX_VALUE) {
counter--;
temp_divisor = temp_divisor >> 1;
}
dividend -= temp_divisor;
answer += (Math.pow((int) 2, (int) counter));
System.out.println(temp_divisor);
System.out.println(dividend);
System.out.println(answer);
System.out.println(counter + "\n");
}
}
if (negative == 1) {
System.out.println("negated");
answer *= -1;
}
return answer;
}
public static int divide(int dividend, int divisor) {
int answer = 0, negative = 0;
if ((dividend < 0) && (divisor > 0))
negative = 1;
if ((dividend == (1 << 31)) && (divisor == -1))
answer = ((1 << 31) - 1);
else {
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
while (dividend - divisor > 0) {// we cannot do dividend>divisor for the dividend = int_min and divisor = 1
// case
int temp_divisor = divisor, counter = 0;
for (; (counter <= 31) && (dividend - temp_divisor >= 0); counter++) {
if (counter == 30) {
System.out.println("Edge case executed");
counter++;
temp_divisor = dividend + 1;
break;
}
temp_divisor = temp_divisor << 1;
System.out.println("temp_divisor is: " + temp_divisor + " power of 2 is: " +
(counter + 1) + " counter is: " + counter);
}
if (temp_divisor != Integer.MAX_VALUE) {
counter--;
temp_divisor = temp_divisor >> 1;
}
dividend -= temp_divisor;
answer += (Math.pow((int) 2, (int) counter));
System.out.println(temp_divisor);
System.out.println(dividend);
System.out.println(answer);
System.out.println(counter + "\n");
}
}
if (negative == 1) {
System.out.println("negated");
answer *= -1;
}
return answer;
}
In this code, the edge cases are not able to be incorporated in the code. which is having (dividend = int_max , divisor=-1 ) or vice versa. Something is missing always. Would someone like to take a look in this code and help me?
JavaBot
JavaBot4w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server