Divide Two Integers | Leetcode - 29

preview_player
Показать описание
Detailed Explanation for Divide Two Integers Leetcode 29 problem.

To support us you can donate

Check out our other popular playlists:

If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful.

#coding #leetcode #programminglife #programmingisfun #programmer #tech #software #codinglife #leetcode
Рекомендации по теме
Комментарии
Автор

We hope you all are enjoying our videos!!! Don't forget to leave a comment!!! Please like the video to support us!!!

Questions you might like:

Struggling in a question??

Leave in a comment and we will make a video!!!🙂🙂🙂

AlgorithmsMadeEasy
Автор

There is a bug in the code. Taking the absolute value is dangerous because abs(Int.MIN_VALUE) causes overflow, and you will end up with Int.MIN_VALUE

whys
Автор

I struggled to understand even after watching this the intuition behind doubling of the divisor.

gcziprusz
Автор

(divisor<<1<<count) sometimes overflows INT_MAX, so it wont work anymore,
for example: runtime error: left shift of 1090366779 by 1 places cannot be represented in type 'int' [solution.c]

MrLeyt
Автор

If we divide 15/2 in your way i.e, doubling the divisor then I'am getting the soution as: 3+2+1(6) which actually is wrong(ans must be 7)

aaditya
Автор

class Solution {
public int divide(int dividend, int divisor) {

// lets go

&& divisor==-1) return Integer.MAX_VALUE;
// && divisor==1) return Integer.MIN_VALUE;


int temp=0;

int ans=0;



int sign = (dividend<0) ^ (divisor <0 ) ? -1 : 1 ;

dividend=Math.abs(dividend);
divisor=Math.abs(divisor);
int subq=0;
int quotient=0;
while(dividend-divisor>=0){

for(subq=0;dividend -

quotient += 1<<subq;
dividend -= divisor<<subq;
}

return sign * quotient;
}
}

AI_for_funn
Автор

int divid(long long dividend, long long divisor) {

// Calculate sign of divisor i.e.,
// sign will be negative only iff
// either one of them is negative
// otherwise it will be positive
int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;

// remove sign of operands
dividend = abs(dividend);
divisor = abs(divisor);

// Initialize the quotient
long long quotient = 0, temp = 0;

// test down from the highest bit and
// accumulate the tentative value for
// valid bit
for (int i = 31; i >= 0; --i) {

if (temp + (divisor << i) <= dividend) {
temp += divisor << i;
quotient |= 1LL << i;
}
}

return sign * quotient;
}

int Solution::divide(int A, int B) {
if(A==INT_MIN&&B==-1)
return INT_MAX;
return divid((long long)A, (long long)B);
}

GHOST-mguu
Автор

04:14 What's the result variable showing here?

DivyanshSaraswat-fliz
Автор

how time complexity is logN^2 can you please explain little bit

himanshuchhikara
Автор

That was awesome. Such an amazing solution... How did people come up with these, dang

Soh
Автор

Sorry but I don't understand time complexity why time complexity is O(logN^2). Could you please explain that? :(

hangto
Автор

1 condition missing in sign assignment that if both dividend and divisor are negative then also sign will be positive


public int divide(int dividend, int divisor) {

if(dividend == 1<<31 && divisor==-1) return Integer.MAX_VALUE;

if(dividend == -1 && divisor == -1) return 1;

boolean sign = ((dividend >= 0 && divisor >= 0) || (dividend <= 0 && divisor <= 0)) ? true :false;

dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
int result =0 ;
while(dividend - divisor>=0){
int count =0;

while(dividend - (divisor << 1 << count) >= 0 ){

count++;

}

result += 1<<count;
dividend -= divisor<<count;

}

return sign? result:-result;


}

sivansrawat
Автор

bhai tujhe hame samjhana hai, ye nahi btana ki tujhe aata hai

ashishkumarjayswal
Автор

i dont understand some lines!..1.if (dividend==1<<31) not possible as should be divident ==INT_MIN!
2.bool sign=dividend>0== dividor>0?if both are negative?it should be XOR in place of ==!

gouravkrroy
Автор

it shows error interger overflow
and runtime error

bcinerd
Автор

you have explained really well, but I can't understand your code its really complex

pironobcoding
Автор

i doesn't understand overflow condition

shubhamgarge
Автор

what a great solution just one correction . tc will be O(log N) because inner loop will run for O(logN) time for the first time and when inner control come to inner loop 2nd time with the new dividend value it will always execute for 1 time therefore the tc - will be O(LogN) only? Please correct me if I'm wrong.

btw thanks for the great solution .

satyamgupta
Автор

hey your first line of code seems wrong, isnt dividend an int so max value supplied to it would 1<<31 -1 and even if you do division with -1 u get quotient = -1<<31 which is in the range of int .... dividend should be Integer.MIN_VALUE and divisor -1 THEN overflow happens and we return Integer.MAX_VALUE

anubhavnegi
Автор

class Solution {
public int divide(int dividend, int divisor) {
int sign = 1;
boolean add = false;
if (dividend == divisor) return 1;
if (divisor == Integer.MIN_VALUE) return 0;
if (dividend == Integer.MIN_VALUE && divisor == -1)
return Integer.MAX_VALUE;
if (divisor == 1 ) return dividend;
else if (divisor == -1) return -dividend;
sign = (dividend >=0) == (divisor>=0) ? 1 : -1;
divisor = Math.abs(divisor);
if (dividend == Integer.MIN_VALUE) {
dividend = Integer.MIN_VALUE + divisor;
add = true;
}
dividend = Math.abs(dividend);
int ans = 0;
while (dividend >= divisor){
dividend -= divisor;
ans++;
}
if (add) return sign*(ans+1);
return sign*ans;
}
}
simple approach java solution

TheShrivivek