Leetcode 29 - Bit Manipulation | Divide Two Integers

preview_player
Показать описание
Topic: Bit Manipulation

Code:

Leetcode:

*Note* I claim no rights to this question. All rights belong to Leetcode. If I'm reviewing a solution that was from another Leetcode user or Leetcode itself I will give credit below.

Hey there! Just wanted to let you know that some of the below links in this video description are affiliate links, which means that if you make a purchase through them, I may earn a small commission. Don't worry though, it doesn't cost you anything extra and it helps support this channel so I can continue to make more videos for you. Thank you so much for your support, and as always, all opinions are my own!

Рекомендации по теме
Комментарии
Автор

I like how you explain each line with an example.
Thanks for sharing this!

whogashaga
Автор

Didn't expect that accent when seeing that thumbnail

saideepesh
Автор

Well, I tried many sites to understand this concept, but I did not get them. But after seeing this video I completely understood the solution.
Thank you so much!

priyanshuagarwal
Автор

Thanks for the awesome explanation. Very clear and easy to understand.
Small feedback. Please try to keep the full code (at least the main portion) on the board and not erase it.
There was plenty of space on the left :)

kayysean
Автор

My initial approach to solve this LC question, derived from repeated substraction(but in my mind, i had a feeling as this would take long amount time, if say dividend is closer to Integer.MAX_VALUE), but anyways I continued with my solution, started submitting on leetcode console. At this point i started handling the negative divisior or divisor possibilities and even the finest edge case of Integer.MIN_VALUE as dividend. But as always, for one such input my solution timed out.
With low lying head, i headed towards the discussions tab and found soln by lee215. I attempted to understand his solution but failed. Later bumped into your video in one of the comments.
Hats off man, you explained really smooth.
I like your calmness in most of your vids and your approach to gather intuition is very rare among all interview-algo-youtubers.
Keep grinding

srinish
Автор

dividend = 2147483647
divisor = 1
there may be an overflow for "while (a - (b << 1 << x) >= 0) "

shen
Автор

Amazingly explained! The first two minutes are itself enough to get the solution clear.. Thank you so much.

bellydancingwithsrishticho
Автор

thanks bro, just got the solution after 1:30 minute watching this video

naveenminhas
Автор

Very clear and concise explanation . Thank you .

techgamer
Автор

Hey that was a really nice explanation.

I was wondering about the edge case where dividend is INT_MIN but divisor is not -1.
In that case, the code would simply take Math.abs over dividend and divisor and proceed, but abs(INT_MIN) would again overflow, leading to a potential issue.

Am I missing something here ?

PS: I saw a previous comment of yours where you explained how its handled in Java. I am using Cpp though. Is it handled the same way there too ? I tried with a cout<<abs(INT_MIN) but it seems thats giving some other value.

sandeeppattanayak
Автор

well I would say I was able to solve this question in a competitive programming arena using this video so its good.

PrafulPrasad
Автор

hi i am a first INFT student and i wasn't sure how to do this sum but after you explained it got easier thank u

anuj-utfo
Автор

Could you please explain a bit about how the solution works? I am confused about:

int a = Math.abs(dividend);
int b = Math.abs(divisor);

The result of a is possible overflow, for example, dividend = Integer.MIN_VALUE.

xiaojunli
Автор

in inner while loop
why there is b<<1<<x not directly b<<x
can somebody plz explain

vishalmishra
Автор

Great solution! What is the time complexity of this solution?

abcd
Автор

You were looking nervous but good work, best explanation video for the topic imo.

vyomyadav
Автор

When the input are 2147483647
and 1, this solution wont work as b<<1<<x will have a signed integer overflow. will have a value of -2^31. Then 2147483647
- (-2^31) will overflow the signed integer.

timotheel
Автор

Thank you so much for this, I watched a number of videos but they confused me even more. First 2 min of your video I got the approach, also the way you explained your code is also very simple & understandable.

Veronica-vqiz
Автор

Hi Thank you so much for that amazing explanation. There is something that I'd like to ask... Why don't we write " while( a-(b<<x) >=0)" instead of "while(a-b<<1<<x) >=0" and start x from 1 instead of 0 ?

youmnaelghandoor
Автор

I don't understand why the edge case

See I convert -2^32 to abs and store it in a long int
Like long int x = labs(dividend)

Continue the whole division
The place where I'm storing the ans is also long

So, I am not giving it a scope to overflow

Yet, I don't understand why after the while loops, the ans turns out -2^32
While it has to be +2^32-- because of the usage of labs and long

Why is it not working!?!
In my mind it should!!

harshithalingapalem
join shbcf.ru