L9. Divide Two Integers without using Multiplication and Division Operators | Bit Manipulation

Поділитися
Вставка
  • Опубліковано 24 лис 2024

КОМЕНТАРІ • 146

  • @thoughtsofkrishna8963
    @thoughtsofkrishna8963 8 місяців тому +150

    Waiting for strings playlist

  • @IamGroot455
    @IamGroot455 4 місяці тому +16

    Best playlist over bit manipulation on entire youtube , I have completed it in just 1 day and its still 5 pm and i started on 9 am. The content was very enjoyable and gave me new ways to think using xor and many others . Thank you a lot for providing such a amazing content for FREE.❤❤❤

  • @bhagyashreeaher7429
    @bhagyashreeaher7429 8 місяців тому +31

    Hello Striver
    Thank you for Bit manipulation Series
    Please make String Problems Solving series soon :)

    • @md_seraj786_
      @md_seraj786_ 5 місяців тому

      yes, please make String problem series 🙏

  • @sujalgupta6100
    @sujalgupta6100 8 місяців тому +20

    string matching algorithms would also help. Please make a playlist on that too.

  • @udayshankar-e6v
    @udayshankar-e6v 7 місяців тому +16

    little modification requires for java : then it will pass all use case;
    long n = Math.abs((long)dividend); // Convert to long to handle Integer.MIN_VALUE
    long d = Math.abs((long)divisor); // Convert to long to handle Integer.MIN_VALUE

  • @ayushmamgain9040
    @ayushmamgain9040 2 місяці тому +6

    The question requires not to use multiplication operator, but it is used in the last line:
    n = n - (d * (1

  • @shwetanshu13
    @shwetanshu13 8 місяців тому +4

    Thank you so so much.... I was eagerly waiting for the bit manipulation series.

  • @areeb6703
    @areeb6703 6 місяців тому +1

    We can start our cnt from 32 (declared outside) and that way we won't have to increment it uptill 32, 31, 30... in order to find the point where ((d n) each time as the outer while loop iterates.
    This way we can simply keep decrementing cnt it until 0. Time complexity: O(32) which is as good as constant.

  • @utube4026
    @utube4026 8 місяців тому +9

    LGTA HAI RAJ BHAI AAJ HI BIT MANUPULATION KHATAM KR DENGE 😂😂😂😂😂😂😂😂. BUT SIR I HAVE MAD RESPECT FOR YOU 😊😊🫡🫡

  • @shrutikumbhare2009
    @shrutikumbhare2009 8 місяців тому +2

    amazing playlist..
    its a humble request to bring a new stack & queue playlist as well

  • @viratlover6206
    @viratlover6206 8 місяців тому +4

    Thank you so much Striver you are motivating us to learn new topics by your videos. You are really doing great work!!

  • @balajisadhu8510
    @balajisadhu8510 7 місяців тому +4

    i didnt expect i wound complete this bit manipulation tipic in 4 hts in one sitting .. thank you sooo muchhh

  • @Codebond7
    @Codebond7 8 місяців тому +5

    Thanks I will complete till here by tomorrow

  • @akashpurbia4390
    @akashpurbia4390 8 місяців тому +1

    Thanks for this amazing playlist!

  • @HitheshB-m8w
    @HitheshB-m8w 8 місяців тому +11

    bro CAN U START STRING SERIES AS WELL PLZZZZZZZZZZZZZZZZZZZZZZZZZ

  • @shreyxnsh.14
    @shreyxnsh.14 7 місяців тому +8

    Complete CPP Code:
    class Solution {
    public:
    int divide(int dividend, int divisor) {
    if(dividend==divisor)
    return 1;
    bool sign = true;
    if((dividend0) || (divisor=0))
    sign = false;
    unsigned int n = abs(dividend);
    unsigned int d = abs(divisor);
    unsigned int ans = 0;
    while(n>=d){
    short count=0;
    while(n > (d

  • @madhavmurari3875
    @madhavmurari3875 2 дні тому

    Best ever explanation ,Thank you Bhaiya .

  • @RiddhiMS-l5g
    @RiddhiMS-l5g 8 місяців тому

    Heyy striver,
    I have been learning from your videos and they are very nice.
    I would request you to start string series please.

  • @krityranjanpanda6259
    @krityranjanpanda6259 8 місяців тому +1

    Great content 😁💯 waiting for string matching algorithms

  • @knowthrvdo
    @knowthrvdo 8 місяців тому

    add this link to a to z dsa sheet so that students can easily get it and thanks for uploading lectures

  • @shreyxnsh.14
    @shreyxnsh.14 7 місяців тому +11

    this question is so bad, what does this even test? your mugging up skills?

  • @NiveditaKar-xj6ew
    @NiveditaKar-xj6ew 8 місяців тому

    Amazing explaination Striver

  • @sashibhushanarajput1194
    @sashibhushanarajput1194 2 місяці тому +1

    Bro though this is good but the main gist of the problem is implemenation as well within the rules after knowing the logic, and by using long that rule has been broken

  • @shashankgsharma0901
    @shashankgsharma0901 4 місяці тому

    Understood, btw, innner while loop was O(1) as max is 32 only and it doesn't increase with size.

  • @AdityaKumar-be7hx
    @AdityaKumar-be7hx 5 місяців тому

    This one works with 32 bit integers and is similar to Strivers solution:
    class Solution {
    public:
    int divide(int dividend, int divisor) {
    if(dividend == divisor)
    return 1;
    if (dividend == INT_MIN && divisor == -1) return INT_MAX;
    int sign = 1;
    if(dividend>=0 and divisor=d){
    int cnt=0;
    while(n>d

  • @sarder681
    @sarder681 4 місяці тому

    Thank you striver from bangladesh.

  • @stith_pragya
    @stith_pragya 7 місяців тому +1

    Thank You So Much for this wonderful video.....🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @limitless6189
    @limitless6189 8 місяців тому +6

    bro y did u delete the old string , heap, and other videos untill u upload new videos pls let the old one be there a kind ly request from ur student community pls understand us

  • @karthik-varma-1579
    @karthik-varma-1579 Місяць тому

    class Solution {
    public int divide(int dividend, int divisor) {
    if(dividend == divisor) return 1;
    boolean sign = true;
    if(dividend0) sign = false;
    if(dividend>0 && divisor=d){
    int count = 0;
    while(n>=(d

  • @017_indrasen9
    @017_indrasen9 8 місяців тому

    Thank you @Striver respect++

  • @riteshbisht94
    @riteshbisht94 7 місяців тому +1

    Please continue the bit manipulation series 🙏

  • @kushagrasahay
    @kushagrasahay 8 місяців тому +1

    thanks for such amazing playlist

  • @mounikap4492
    @mounikap4492 6 місяців тому

    Please include 1915. Number of Wonderful Substrings, and it would be nice to cover the concept of masking in bit_manipulation series. Thanks.

  • @shreyxnsh.14
    @shreyxnsh.14 7 місяців тому +4

    we were not supposed to use multiplication, then how is this solution valid?

  • @AfzalAsar
    @AfzalAsar 8 місяців тому +2

    Bhaiyaa please upload stack queue asap as well please 🥺

  • @masteredultrainstinct8840
    @masteredultrainstinct8840 8 днів тому

    There is another Math solution you can use. TC : LogN
    Use binary serach in range [1,N/2], pick mid and keep checking -> if mid*division

  • @karthik-varma-1579
    @karthik-varma-1579 Місяць тому

    Java Solution:
    class Solution {
    public int divide(int dividend, int divisor) {
    if(dividend == divisor) return 1;
    boolean sign = true;
    if(dividend0) sign = false;
    if(dividend>0 && divisor=d){
    int count = 0;
    while(n>=(d

  • @Yogamindmastery
    @Yogamindmastery 4 місяці тому

    Very good explaination thanks a lot Sir

  • @harshith1424
    @harshith1424 8 місяців тому

    Most wanted topic❤

  • @sumitsharma-ss2xr
    @sumitsharma-ss2xr 8 місяців тому

    Bhaiya sliding window v jldi lana i know apki schedule busy h but fir v wait kr rha hu 😅 Koi or playlist se pdh rha tha but waiting for yours sliding window playlist

  • @SuvradipDasPhotographyOfficial
    @SuvradipDasPhotographyOfficial 4 місяці тому

    Striver bhai bit manipulation over, please do strings. It's very important, many questions are getting stuck in string. Please bhai🙏🙏

  • @riteshbisht94
    @riteshbisht94 8 місяців тому +1

    Great video 🔥

  • @UECAshutoshKumar
    @UECAshutoshKumar 8 місяців тому +1

    Thank you 😊

  • @himanshukaushik9223
    @himanshukaushik9223 8 місяців тому +2

    Bhaiya a2z sheet pa code and article ka link upload kar do binary search tree pa bhi kuch questions pa nahi hai ??

  • @Algorithmswithsubham
    @Algorithmswithsubham 8 місяців тому +2

    sir how to write good quality code uspe ak video banaya

  • @foziezzz1250
    @foziezzz1250 6 місяців тому +4

    small doubt
    why did we checked that quotient == 1

  • @kunal5077
    @kunal5077 3 місяці тому

    Please start with string playlist sir
    HUMBLE REQUEST TO YOU

  • @rushidesai2836
    @rushidesai2836 7 місяців тому

    Below one slightly modified.
    public int divide(int dividend, int divisor) {
    // Check for edge cases
    if (dividend == 0) return 0;
    if (divisor == 0) throw new ArithmeticException("Division by zero");
    if (divisor == -1 && dividend == Integer.MIN_VALUE) return Integer.MAX_VALUE;
    // Determine the sign of the result
    boolean isNegative = (dividend < 0) ^ (divisor < 0);
    // Convert dividend and divisor to positive values
    long absDividend = Math.abs((long) dividend);
    long absDivisor = Math.abs((long) divisor);
    long quotient = 0;
    while (absDividend >= absDivisor) {
    long temp = absDivisor;
    long shift = 1;
    // Shift the divisor left until it becomes greater than the dividend
    while ((temp

  • @U2011-n7w
    @U2011-n7w 4 місяці тому +1

    best video

  • @mohitsingh7793
    @mohitsingh7793 7 місяців тому

    More efficient than Striver's code :
    T.C: O(1)
    S.C : O(1)
    int divideTwoInteger(int dividend, int divisor) {
    long q=0,sign=-1;
    if((dividend0))
    sign=1;
    long d1=abs(dividend), d2=abs(divisor),temp=0;
    for(int i=31;i>=0;i--)
    {
    if((temp+(d2

    • @TheHariPutraOfficially
      @TheHariPutraOfficially 3 місяці тому

      Bhai mujhe English samajne me dikkat ho rahi ..and striver fails to say what he wants to do..like..in this..start ne bolte .. multiplication means continuous subtraction
      I give up with DSA..kitna rough hai...
      Best development haiii

  • @ss_shruti10
    @ss_shruti10 2 місяці тому

    Please upload String Playlist also :)

  • @Rahul-rp5hk
    @Rahul-rp5hk 8 місяців тому

    Can be easily done in O(logn) too

  • @nisanduathsara5242
    @nisanduathsara5242 8 місяців тому

    hey striver need a full combinatorics playlist...

  • @vaibhavagarwal1479
    @vaibhavagarwal1479 8 місяців тому

    is this the end of the playlist??
    as there are some advance maths questions there in bit manipulation

  • @udaychaudhary3636
    @udaychaudhary3636 8 місяців тому +1

    waiting for the string playlist

  • @rishijen7363
    @rishijen7363 8 місяців тому +1

    Pls string playlist ❤❤❤

  • @shaikkhizar8133
    @shaikkhizar8133 8 місяців тому +1

    Thankyou sir

  • @AkashKumar-qb6sc
    @AkashKumar-qb6sc 3 місяці тому +1

    Awsm

  • @khalasianiket816
    @khalasianiket816 4 місяці тому

    understood ❤

  • @jaydabhi5539
    @jaydabhi5539 6 місяців тому

    Thanks striver.
    I understand the solution but i just want to ask will it help in any other problems if i know this solution?Anyone? Your reply is appreciated.

  • @akashkumaryadav1827
    @akashkumaryadav1827 3 місяці тому +1

    🔥

  • @ketonesgaming1121
    @ketonesgaming1121 8 місяців тому

    Java Solution : public class Solution {
    public static int divideTwoInteger(int dividend, int divisor) {
    if (dividend == divisor)
    return 1;

    boolean sign = true;
    if (dividend > 0 && divisor < 0)
    sign = false;
    if (dividend < 0 && divisor > 0)
    sign = false;

    int n = Math.abs(dividend);
    int d = Math.abs(divisor);
    int ans = 0;

    while (n >= d) {
    int count = 0;
    while (n >= (d

  • @thanuja.07
    @thanuja.07 8 місяців тому

    Can anyone tell me, where is the link to the problem and notes, as i couldn't figure out where is day8 in the link given in description

  • @the_ved11
    @the_ved11 8 місяців тому

    On the 2nd while condition, why does it not as n>= (d*(1

    • @ckrakle
      @ckrakle 8 місяців тому

      same as n >= (d

    • @akworld2739
      @akworld2739 7 місяців тому +1

      check his first video of bit manipulation in left shift

  • @shaiksoofi3741
    @shaiksoofi3741 5 місяців тому +1

    understood

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 7 місяців тому

    Thank you Bhaiya

  • @abhimanyuthapliyal5823
    @abhimanyuthapliyal5823 4 місяці тому

    isnt TC better for appr 1 , then why is appr 2 reqd ?

  • @Chhooso
    @Chhooso 6 місяців тому

    striver please drop the strings playlist

  • @deepakkumar13204
    @deepakkumar13204 8 місяців тому

    First view from my sids 😊

  • @govindlohar6816
    @govindlohar6816 6 місяців тому

    very nice

  • @Sumanth_Siddareddy
    @Sumanth_Siddareddy 4 місяці тому

    Understood

  • @sanjaykatta6499
    @sanjaykatta6499 5 місяців тому

    we didn't completely get away from using multiplication even with this code.

  • @mimansatripathi
    @mimansatripathi 7 місяців тому

    please start string series 🥺🥺🥺🥺🥺🥺

  • @paarthkumar3787
    @paarthkumar3787 4 місяці тому

    C++ Solution -
    #include
    using namespace std;
    class Solution {
    public:
    int divide(int dividend, int divisor) {
    if (dividend == divisor) return 1;
    if (dividend == INT_MIN && divisor == -1) return INT_MAX;
    int sign = 1;
    if ((dividend >= 0 && divisor < 0) || (dividend 0)) {
    sign = -1;
    }
    long n = labs(dividend); // Use labs for long absolute value
    long d = labs(divisor); // Use labs for long absolute value
    long ans = 0;
    while (n >= d) {
    int cnt = 0;
    while (n >= (d

  • @ankitpradhan2499
    @ankitpradhan2499 5 місяців тому +1

    can anyone plzz tell why this code is not working -->
    int division(int given, int divisor){
    bool sign = true; // sign is positive
    if(given < 0 && divisor > 0) sign = false; // sign is negative
    if(given > 0 && divisor < 0) sign = false; // sign is negaitve
    long given1 = abs(given);
    long divisor1 = abs(divisor);
    long ans = 0;
    int cnt = 0;
    while( given1 - ( divisor1

  • @sword013
    @sword013 Місяць тому

    the overflow part is still not that clear

  • @ayushraj5672
    @ayushraj5672 8 місяців тому

    Bhaiya, actually i want to start this DSA playlist but don't know from which video should i start as you have made playlists topicwise. It would really be helpful if you would create a new playlist and add all videos in order, else you can upload a seperate video regarding roadmap for this particular playlist, like from where one should start then what, then what, then next what....
    --Thank you
    Please reply asap:⁠-⁠)

  • @ritesh_kr
    @ritesh_kr 8 місяців тому

    what if our d is INT_MIN ?
    ps : i didnt watch the whole lecture 😅

  • @akshatgoel149
    @akshatgoel149 4 місяці тому +1

    There is no need for nested loops
    TC - O(log2N)
    class Solution {
    public:
    int divide(int dividend, int divisor) {
    long c = 0;
    int sign = 0;
    if(dividend0) sign = 1;
    if(dividend>0 && divisor

  • @RahulRai-vw3dw
    @RahulRai-vw3dw 27 днів тому

    when i run this code it gives tle

  • @tanishjain1643
    @tanishjain1643 6 місяців тому

    These code is working in cpp but not in java
    Greeting tle

  • @257divyanshu
    @257divyanshu 7 місяців тому +1

    I have a question, why are the we removing the largest possible first I mean why are we removing 12 first and then 6 and then 3...... we could have removed 3 and then 6 and then 12 (doing so will reduce the need to find what's the maximum that can be returned)............ after removing 3 (3 * 2^0) our n will 19, then we see that 6 (3 * 2^1) can be removed so we will remove 6, after removing 6 n becomes 13.... then we see that 12 (3 * 2^2) can be removed so we remove 12, then our n becomes 1, now the next thing that should be removed is 24 (3 * 2^3) .... but this cannot so we will return the sum of 1, 2 and 4 which is 7..... I tried this approach on the leetcode question 29, but it's giving TLE...... can someone please explain why..... I just reversed the order, right? this shouldn't cause TLE

    • @HaamroNotes
      @HaamroNotes 6 місяців тому

      // Online C compiler to run C program online
      #include
      int main() {
      int n;
      int d;
      scanf("%d %d",&n,&d);
      int sum=0;
      int count=0;
      int ans=0;
      while(sum

    • @pBERA0_0
      @pBERA0_0 2 місяці тому

      same doubt :/
      @257divyanshu

  • @SudhirKumar-ge5nv
    @SudhirKumar-ge5nv 8 місяців тому

    I just want to believe if the shift operators are used anywhere... 😢😢😢

    • @akworld2739
      @akworld2739 7 місяців тому

      yes it is too confuesing😅

  • @ayushojha4607
    @ayushojha4607 8 місяців тому

    start heap playlist

  • @prajwalrai2596
    @prajwalrai2596 8 місяців тому

    Waiting for string

  • @MohitKumar-uw1pz
    @MohitKumar-uw1pz 2 місяці тому

    MAIN HOON MOHIT

  • @VisibleNasir
    @VisibleNasir 8 місяців тому

  • @AbhishekKumar-lh2pt
    @AbhishekKumar-lh2pt 7 місяців тому

    🙇🙇🙌

  • @abdealimuneem4968
    @abdealimuneem4968 5 місяців тому

    could have also used binary search, amirite//

  • @slayer-x2f
    @slayer-x2f 3 місяці тому +2

    could have been better

  • @zaffarzeshan1308
    @zaffarzeshan1308 6 місяців тому

    2nd test case failed

  • @ckrakle
    @ckrakle 8 місяців тому

    taking long long 1LL menas ans += (1LL passed
    if(ans > INT_MAX && sign == 0) return INT_MAX;
    if(ans > INT_MAX && sign == 1) return INT_MIN;
    code 2 => not passed
    if(ans >= INT_MAX && sign == 0) return INT_MAX;
    if(ans >= INT_MAX && sign == 1) return INT_MIN;
    code 3 => not passed
    if(ans >= (1 (1

  • @Lucifer0872
    @Lucifer0872 8 місяців тому

    hail mary

  • @sanjanareddy2696
    @sanjanareddy2696 8 місяців тому

    Please stop wearing red and try other colors. Its boring now

    • @tousifjaved3485
      @tousifjaved3485 8 місяців тому

      Red increases attention and engagement of audience
      Maybe that's why he always wears red for video making

  • @amitdahiya7425
    @amitdahiya7425 8 місяців тому +2

    So I wrote the code same as yours , got runtime error due to corner cases , Checked the solution 99% of solution have used long long which is violation of the question rules
    and then checked the LEE215 solution but it was working well in past not now , so can anyone provide me the solution which works well and follows the question rules too
    Edit : Got the solution here it is 👇👇
    class Solution {
    public:
    int divide(int dividend, int divisor) {
    if (dividend == INT_MIN && divisor==-1) return INT_MAX;
    if(dividend==divisor)return 1;
    unsigned int n=abs(dividend),d=abs(divisor);
    bool neg=false;
    if(dividend =0)neg=true;
    else if(dividend >=0 && divisor=d){
    short count=0;
    while(n > (d

    • @the_ved11
      @the_ved11 8 місяців тому

      On the 2nd while condition, why does it not as n>= (d*(1

    • @takeUforward
      @takeUforward  8 місяців тому

      it is not possible, did not see any solution working with using int, you can use signed int, but that is violation as well

  • @aryansinha1818
    @aryansinha1818 11 днів тому

    The question requires not to use multiplication operator, but it is used in the last line:
    n = n - (d * (1

  • @SibiRanganathL
    @SibiRanganathL 3 місяці тому +1

    Understood

  • @rohanagarwal4290
    @rohanagarwal4290 8 місяців тому +1

    waiting for strings playlist

  • @Rahulyadav-sd3oj
    @Rahulyadav-sd3oj 8 місяців тому

    Thank u sir

  • @hardikpatel352
    @hardikpatel352 4 місяці тому

    understood