Divide Two Integers | Leetcode - 29

  Рет қаралды 67,511

Algorithms Made Easy

Algorithms Made Easy

Күн бұрын

Detailed Explanation for Divide Two Integers Leetcode 29 problem.
Get Discount on GeeksforGeeks courses (practice.geeks...) by using coupon code: ALGOMAEASY
To support us you can donate
Patreon: / algorithmsmadeeasy
UPI: algorithmsmadeeasy@icici
Check out our other popular playlists:
✅✅✅[ Tree Data Structure ] : • Tree Data Structure
✅✅✅[ Graphs Data Structure ] : • Graph Data Structure
✅✅✅[ December Leetcoding Challenge ] : • December Leetcoding Ch...
✅✅✅[ November Leetcoding Challenge ] : • November Leetcoding Ch...
✅✅✅[ August Leetcoding Challenges ] : • August LeetCoding Chal...
✅✅✅[ July Leetcoding Challenges ] : • July LeetCoding Challe...
✅✅✅[ Cracking the Coding Interview - Unique String ] : • Cracking the Coding In...
✅✅✅[ June Leetcoding Challenges ] : • June LeetCoding Challe...
✅✅✅[ May Leetcoding challenges ]: • May LeetCoding Challen...
Problem Link: leetcode.com/p...
Code: github.com/Alg...
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

Пікірлер: 83
@AlgorithmsMadeEasy
@AlgorithmsMadeEasy 3 жыл бұрын
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: ✅✅✅[ Tree Data Structure ] : kzbin.info/aero/PLJtzaiEpVo2zx-rCqLMmcFEpZw1UpGWls ✅✅✅[ Graphs Data Structure ] : kzbin.info/aero/PLJtzaiEpVo2xg89cZzZCHqX03a1Vb6w7C ✅✅✅[ January Leetcoding Challenge ] : kzbin.info/aero/PLJtzaiEpVo2wCalBcRcNjXQ0C6ku3dRkn ✅✅✅[ December Leetcoding Challenge ] : kzbin.info/aero/PLJtzaiEpVo2xo8OdPZxrpybGR8FmzZpCA ✅✅✅[ November Leetcoding Challenge ] : kzbin.info/aero/PLJtzaiEpVo2yMYz5RPH6pfB0wNnwWsK7e ✅✅✅[ August Leetcoding Challenge ] : kzbin.info/aero/PLJtzaiEpVo2xu4h0gYQzvOMboclK_pZMe ✅✅✅July Leetcoding challenges: kzbin.info/aero/PLJtzaiEpVo2wrUwkvexbC-vbUqVIy7qC- ✅✅✅June Leetcoding challenges: kzbin.info/aero/PLJtzaiEpVo2xIfpptnCvUtKrUcod2zAKG ✅✅✅May Leetcoding challenges: kzbin.info/aero/PLJtzaiEpVo2wRmUCq96zsUwOVD6p66K9e ✅✅✅Cracking the Coding Interview - Unique String: kzbin.info/aero/PLJtzaiEpVo2xXf4LZb3y_BopOnLC1L4mE Struggling in a question?? Leave in a comment and we will make a video!!!🙂🙂🙂
@whys2016
@whys2016 Жыл бұрын
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
@codeman0017
@codeman0017 8 ай бұрын
there is an edge case to handle it my friend
@gcziprusz
@gcziprusz 3 жыл бұрын
I struggled to understand even after watching this the intuition behind doubling of the divisor.
@deepaksaroha522
@deepaksaroha522 2 жыл бұрын
yes, explanation is not there, just algorithm..
@zr60
@zr60 2 жыл бұрын
This guy literally just copy pasted the solution and read the code out loud LOL. He probably doesn't understand the intuition behind it either.
@bmuralikrishna8054
@bmuralikrishna8054 Жыл бұрын
As per my understanding, the intuition behind this problem is, If we can exactly remove the dividable value from the given value then, finally we can reach the remaining value which cannot be dividable. We can do this by subtracting the divisor from the numerator but it is not efficient. Instead, we can do doubling the denominator and can reach the edge quickly. Once we reach the edge or cross the edge, just subtract the value from the numerator and the properly dividable value will be left out and the number of times that value can be dividable is 1
@rohanjuneja1778
@rohanjuneja1778 10 ай бұрын
yes thats correct . @@bmuralikrishna8054
@MrLeyt1125
@MrLeyt1125 7 ай бұрын
(divisor
@aaditya3937
@aaditya3937 3 жыл бұрын
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)
@DivyanshSaraswat-fl9iz
@DivyanshSaraswat-fl9iz 3 ай бұрын
04:14 What's the result variable showing here?
@himanshuchhikara4918
@himanshuchhikara4918 3 жыл бұрын
how time complexity is logN^2 can you please explain little bit
@gouravkrroy3625
@gouravkrroy3625 2 жыл бұрын
i dont understand some lines!..1.if (dividend==1
@anubhavnegi4230
@anubhavnegi4230 Жыл бұрын
1. yeah first statement is wrong 2. if both are -ve then it becomes false == false so condition becomes true hence it is correct
@hangto0401
@hangto0401 3 жыл бұрын
Sorry but I don't understand time complexity why time complexity is O(logN^2). Could you please explain that? :(
@rm0ndo
@rm0ndo Жыл бұрын
Outer loop will run for log n times (dividend is continuously reduced by 2^count) and inner loop also runs in log n time (divisor keeps doubling). So total time complexity is O(log n * log n) = O(log n^2)
@shubhamgarge7001
@shubhamgarge7001 2 жыл бұрын
i doesn't understand overflow condition
@bcinerd
@bcinerd 3 жыл бұрын
it shows error interger overflow and runtime error
@radoonhadoon
@radoonhadoon 3 жыл бұрын
im getting overflow at dividend - (divisor
@AshishKumar-tb6go
@AshishKumar-tb6go 3 жыл бұрын
getting the same have u sort that out?
@radoonhadoon
@radoonhadoon 3 жыл бұрын
@@AshishKumar-tb6go it's working in java but not in c++. I cannot understand why it isn't working in c++, if you find out why , please reply
@marvellouschandan
@marvellouschandan 3 жыл бұрын
​@@radoonhadoon @Ashish Kumar We need to handle the case when our variables going to overflow, for that you have convert all variables to long data type. Below solution might help class Solution { public: int divide(int dividend, int divisor) { if (dividend == 1 result is negative. bool sign = (dividend >=0) == (divisor >=0) ? true : false; long divd = abs(dividend); long divs = abs(divisor); long result=0; while (divd >= divs) { long count=0; // We are doubling each time while (divd >= divs
@mohitmundotia8950
@mohitmundotia8950 3 жыл бұрын
@@marvellouschandan Thanks got it
@divya-ul9vi
@divya-ul9vi 3 жыл бұрын
use long long instead of int for c++.
@manupathria1073
@manupathria1073 3 жыл бұрын
Wrote same code in c++. Getting runtime error on Dividend= INT_MAX DIVISOR=1 please help to understand the base case of overflow.
@abhishek-singh31
@abhishek-singh31 3 жыл бұрын
Divisor = -1
@anubhavnegi4230
@anubhavnegi4230 Жыл бұрын
Put it dividend = INT_MIN divisor = -1
@satyamgupta6030
@satyamgupta6030 Жыл бұрын
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 .
@ashutoshdas3010
@ashutoshdas3010 10 ай бұрын
isn't abs a mod operator? @AlgorithmsMadeWrong
@21sivansrawat
@21sivansrawat Жыл бұрын
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= 0) || (dividend
@your_daddy_mf
@your_daddy_mf Жыл бұрын
NAIVE THIS IS ALSO HANDLED BY SAME LINE
@anubhavnegi4230
@anubhavnegi4230 Жыл бұрын
hey your first line of code seems wrong , isnt dividend an int so max value supplied to it would 1
@pironobcoding
@pironobcoding 2 жыл бұрын
you have explained really well, but I can't understand your code its really complex
@pranaynagpure2804
@pranaynagpure2804 Жыл бұрын
Time complexity is wrong it should be O(log n *log n ) that is log^2 n because both loops exist log n times
@GHOST-mg7uu
@GHOST-mg7uu 3 жыл бұрын
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
@MrLeyt1125
@MrLeyt1125 7 ай бұрын
You cannot use long long, read descrpition: Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range
@anirudhrathore6601
@anirudhrathore6601 2 жыл бұрын
ok but can you explain how you can get absolute of -2147483648 and then solving it for divisor 2 will be hard because doubling 1073741824 will return negative number making it an infinite loop.
@himanshukandwal8710
@himanshukandwal8710 2 жыл бұрын
why (1
@osamaayman3765
@osamaayman3765 2 жыл бұрын
it is actually Integer.MIN_VALUE because it is written in the 2's complement from: the first bit from the left is taken with a negative sign and all other bits are positive. So, 1
@adrikagupta5573
@adrikagupta5573 3 жыл бұрын
What is the intuition behind this?
@manojboganadham9071
@manojboganadham9071 3 жыл бұрын
yes i too can not understand that
@kondapanenitinusripriyanka7719
@kondapanenitinusripriyanka7719 3 жыл бұрын
Understood??
@eeshanchanpura3356
@eeshanchanpura3356 2 жыл бұрын
its giving wrong result for input (1,-1)
@AliveGamerNo1
@AliveGamerNo1 Жыл бұрын
class Solution { public int divide(int dividend, int divisor) { // lets go if(dividend==Integer.MIN_VALUE && divisor==-1) return Integer.MAX_VALUE; // if(dividend==Integer.MIN_VALUE && divisor==1) return Integer.MIN_VALUE; int temp=0; int ans=0; int sign = (dividend
@Soh965
@Soh965 2 жыл бұрын
That was awesome. Such an amazing solution... How did people come up with these, dang
@AlgorithmsMadeEasy
@AlgorithmsMadeEasy Жыл бұрын
Thanks!!
@reethikavanaparthy6984
@reethikavanaparthy6984 2 жыл бұрын
Does Math.abs work for INT_MIN??
@MDFAIZANSARI
@MDFAIZANSARI Жыл бұрын
The algo didn't work for 10 / 2
@rajkaran9720
@rajkaran9720 2 жыл бұрын
Hi As per your explanation the below code is working for 10 ,3 but not for 16,3, can you help me what i'm missing here public int divide2(int dividend, int divisor) { int result = 0; int temp = divisor; while(dividend >= divisor) { int count = 0; while(dividend-temp >=0 ) { count++; temp1; result = result + count; } return result; }
@khatariinsaan5284
@khatariinsaan5284 Жыл бұрын
Code is wrong..Here is the Right Code if(dividend == 1=0) ? true : false; dividend = Math.abs(dividend); divisor = Math.abs(divisor); int result=0; while(dividend - divisor >= 0 ){ int count=0; while(dividend - (divisor
@gaganbedi4332
@gaganbedi4332 2 жыл бұрын
I watched it 3 times, but did't understood what he did in inner while loop.
@shivanshupandey3637
@shivanshupandey3637 Жыл бұрын
try to take few more testcases....not satisfying....
@ashishkumarjayswal
@ashishkumarjayswal 2 жыл бұрын
bhai tujhe hame samjhana hai ,ye nahi btana ki tujhe aata hai
@somnathmaityik
@somnathmaityik 2 жыл бұрын
op🤣🤣🤣🤣
@vamshi3307
@vamshi3307 2 жыл бұрын
🤣🤣🤣
@Man_of_Culture.
@Man_of_Culture. 2 жыл бұрын
🤣
@vishichaurasiya4427
@vishichaurasiya4427 2 жыл бұрын
🤣🤣🤣
@suzy1107
@suzy1107 2 жыл бұрын
The solution couldn't pass certain tests unfortunately..
@suzy1107
@suzy1107 2 жыл бұрын
nvm if I write a >= b it won't work. If I write a - b = 0, it works..
@iitjee258
@iitjee258 11 ай бұрын
nice explanation
@TheShrivivek
@TheShrivivek 2 жыл бұрын
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
@aashishshah1187
@aashishshah1187 2 жыл бұрын
Great explanation 🔥🔥 , i suffer entire day to solve this question finally i will have sound sleep 🙂
@AlgorithmsMadeEasy
@AlgorithmsMadeEasy 2 жыл бұрын
Glad you liked it !! Keep growing !!
@zr60
@zr60 2 жыл бұрын
you solve it but you don't understand it what's the point?
@nitianvlog
@nitianvlog Жыл бұрын
time waste kra diye optimal appraoch bataye hi nahi
@amolmishra8621
@amolmishra8621 3 жыл бұрын
Awesome explanation!
@AlgorithmsMadeEasy
@AlgorithmsMadeEasy 3 жыл бұрын
Thanks @Amol.
@yt-spikegaming7394
@yt-spikegaming7394 Жыл бұрын
I was guys who liked this video at 666th count XD
@hymnish_you
@hymnish_you 3 жыл бұрын
Nice Explanation
@AlgorithmsMadeEasy
@AlgorithmsMadeEasy 3 жыл бұрын
Thanks
@rajeshbammidy180
@rajeshbammidy180 2 жыл бұрын
If you do Math.abs(Integer.MIN_VALUE) it will give you Integer.MIN_VALUE only so this would fail for the some testcases
@gcziprusz
@gcziprusz 3 жыл бұрын
There is a bug on line `boolean sign = (dividend>=0) == (divisor>=0) ? true: false;` This example should yield a positive result -2/-1
@mohdanas458
@mohdanas458 2 жыл бұрын
what's wrong in it ? it is showing the positive result as LHS is equal to RHS condition true answer would be true
@suyashrahatekar4964
@suyashrahatekar4964 10 ай бұрын
The thing I hate about all solns on youtube are they dont explain the mathematical logic behind this. They present it like an algorithm that we have studied in 1st grade itself. Gave a dislike to all of em but fuck youtube
@ShreyaSingh-gm1pk
@ShreyaSingh-gm1pk Жыл бұрын
It's too complex
@shivaarukonda2640
@shivaarukonda2640 Жыл бұрын
poorly explained
@ItsYourCandyBro
@ItsYourCandyBro 3 жыл бұрын
Bro 4
@AlgorithmsMadeEasy
@AlgorithmsMadeEasy 3 жыл бұрын
4
@hardikp5192
@hardikp5192 9 ай бұрын
error in this case -2147483648 -1
Sum of Two Integers - Leetcode 371 - Java
11:48
NeetCode
Рет қаралды 121 М.
Validate Stack Sequences | Live Coding with Explanation | Leetcode - 946
6:34
Это было очень близко...
00:10
Аришнев
Рет қаралды 1,3 МЛН
Зу-зу Күлпаш 2. Бригадир.
43:03
ASTANATV Movie
Рет қаралды 720 М.
How I Turned a Lolipop Into A New One 🤯🍭
00:19
Wian
Рет қаралды 10 МЛН
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Maximum Number of Events That Can Be Attended | Leetcode - 1353
15:10
Algorithms Made Easy
Рет қаралды 7 М.
Viral Video of a Man's Crazy Job Interview
16:02
Darryl Vega TV
Рет қаралды 1,3 МЛН
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 536 М.
Maximum Value at a Given Index in a Bounded Array | Leetcode - 1802
18:19
Algorithms Made Easy
Рет қаралды 2 М.
Add Two Numbers Without The "+" Sign (Bit Shifting Basics)
18:25
Back To Back SWE
Рет қаралды 124 М.
Hash Tables and Hash Functions
13:56
Computer Science Lessons
Рет қаралды 1,6 МЛН
Leetcode 29 - Bit Manipulation | Divide Two Integers
9:32
Nideesh Terapalli
Рет қаралды 61 М.
Algorithms Explained for Beginners - How I Wish I Was Taught
17:38
Internet Made Coder
Рет қаралды 355 М.
Это было очень близко...
00:10
Аришнев
Рет қаралды 1,3 МЛН