Ugly numbers efficient program solution explained | 2 Methods

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

КОМЕНТАРІ • 72

  • @anoopm3605
    @anoopm3605 4 роки тому +40

    Your multiplication solution is not exactly correct. For example, 2*7 is not an ugly number. Your DP solution is correct though.The basic concept is any ugly number multiplied with 2,3 or 5 will give an ugly number.As we only keep ugly numbers in the dp array, we will get the answer

    • @kedarnadkarni8781
      @kedarnadkarni8781 4 роки тому +1

      hE means to say:
      do the multiplication in the following way :
      if (ugly == nums[i2] * 2) ++i2;
      if (ugly == nums[i3] * 3) ++i3;
      if (ugly == nums[i5] * 5) ++i5;

    • @be_like__prateek
      @be_like__prateek 4 роки тому

      @@kedarnadkarni8781 see around 10:12

  • @kishalaybhattacharya8312
    @kishalaybhattacharya8312 4 роки тому +29

    your approach with the multiplication table is wrong, try to upload videos with right concept, otherwise it'll be confusing for others

    • @dabbiruhaneesh2714
      @dabbiruhaneesh2714 3 роки тому

      yes bro.he made me confuse for a while

    • @claio0128
      @claio0128 3 роки тому

      The DP algorithm does not solution this problem. For example, 14 is not an ugly number in your example, but you counted as a multiple of 2. I made the same mistake before.

    • @skinkyButt
      @skinkyButt 3 роки тому

      Ditto... the answer is wrong!!!! Don't follow this algo...

    • @pradeep1198
      @pradeep1198 2 роки тому

      yes its wrong...wasted almost an hour

  • @praveenchouhan6388
    @praveenchouhan6388 4 роки тому +8

    i think this approach deviates at a point wherein the 11th ugly number comes 2*7=14 which is incorrect, the dp solution is a thing after that but i think something is wrong in the main approach!!!

  • @charankumar8350
    @charankumar8350 4 роки тому +5

    Actually 1 is also a power of those,2 power 0 is 1,3 power 0 is 1,5 power 0 is 1 at(1:03)

  • @nocredits8066
    @nocredits8066 3 роки тому +1

    I don't understand the way we uptade the next number ugly number (nm2= ugly[i2] * 2). I mean, why does it work?

  • @K_EC_AushoRoup
    @K_EC_AushoRoup 3 роки тому +5

    DP approach is mind-blowing.

  • @be_like__prateek
    @be_like__prateek 4 роки тому +3

    Your multiplication solution is not exactly correct. For example, 2*7 is not an ugly number.

  • @sudheer-suri
    @sudheer-suri 4 роки тому +9

    out of thin air you thought you should use 2,3,5 tables ? you should've explained what made you use that method instead of just explaining the code in English.

    • @575saisri4
      @575saisri4 4 роки тому

      after a lot of practice, yes one will get ideas out of thin air.

    • @AshwinKumar-ds5ip
      @AshwinKumar-ds5ip 4 роки тому

      Because ugly numbers are numbers whose prime factors are 2,3,5. Pretty straightforward.

  • @prateekaryan1020
    @prateekaryan1020 4 роки тому +10

    Thank you for the explanation Bhaiya :)
    But your intuitive solution is misleading. In your intuitive solution, you are basically taking a minimum number from tables of 2,3 and 5 at each iteration, by this approach, we will even have 14,21.... and so on as an ugly number which is incorrect.
    Though, final dp approach is correct.

    • @techdose4u
      @techdose4u  4 роки тому +4

      Yea I too realized it afterwards but can't change it now. I will try to put a new video for this if I get time and remove this one.

    • @sajalkaushik5929
      @sajalkaushik5929 4 роки тому

      @@techdose4u hey could you explain what is the right intuition for DP?

    • @gladyouseen8160
      @gladyouseen8160 3 роки тому

      @@techdose4u bro can you even discuss the top down approach for this

    • @jackwindensky5606
      @jackwindensky5606 2 роки тому

      Why are 14 and 21 not ugly numbers though? 14 = 7 * 2. 2 is in the provided set. Same with 21 = 7 * 3.
      Edit: I believe it's because it needs to have both factors be in the set. Both 14 and 21 each only have one in the set

    • @surajsri248
      @surajsri248 2 роки тому

      The approach using something like bfs only to store it in a TreeSet so that they remain in an order and continuing until the interation reaches to can be something that works. But i dont think it involves dp.

  • @animeblastford6313
    @animeblastford6313 3 роки тому +1

    Guys, this approach is wrong. Using a multiplication table means that at some point you will insert 14(2*7) or 21(3*7) and so on, which are not ugly numbers.

  • @jubba9263
    @jubba9263 4 роки тому

    Hello, i need to write a programto find the 1500th ugly number, without any input. (C++) Could you help me out?

  • @micromir4
    @micromir4 4 роки тому +1

    everyone is jumping straight to 2,3,5 tables to explain the solution. How did you arrive at the table?

    • @assassinator3747
      @assassinator3747 4 роки тому +3

      because every number can only be divided by 2, 3, 5, one way to look at the sequence is to split the sequence to three groups as below:
      (1) 1×2, 2×2, 3×2, 4×2, 5×2, …
      (2) 1×3, 2×3, 3×3, 4×3, 5×3, …
      (3) 1×5, 2×5, 3×5, 4×5, 5×5, …
      We can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, …) multiply 2, 3, 5. Then we use similar merge method as merge sort, to get every ugly number from the three subsequence. Every step we choose the smallest one, and move one step after.

    • @kumarakash5219
      @kumarakash5219 3 роки тому

      @@assassinator3747 nice explanation ty👌👌👌👌

  • @RahulKumar-wf9xx
    @RahulKumar-wf9xx 3 роки тому +1

    Great videos, Really Helpful .....Please keep up the great work !!

  • @therawbean69
    @therawbean69 4 роки тому +2

    Your method will give 14 as the ugly number, which is not. Please correct the error

    • @techdose4u
      @techdose4u  4 роки тому

      In the code?

    • @therawbean69
      @therawbean69 4 роки тому

      TECH DOSE your logic itself is wrong.

    • @kalyanm6493
      @kalyanm6493 4 роки тому

      @@techdose4u The 2, 3, 5 tables method is wrong. If you continue further with that method, 14 will be an ugly number according to that logic.

  • @saravanan925
    @saravanan925 4 роки тому

    how to write a recursive formula for this problem? because generally for all the dp problems I have been seeing some recursive formula, why not in this problem?

    • @swarnikagupta16
      @swarnikagupta16 3 роки тому

      Have you found out the recursive approach?

    • @VY-zt3ph
      @VY-zt3ph 3 роки тому

      Let me tell you this guy just copy the solution from one source and then make a video of them. I have the algorithm to generate ugly numbers which I wrote. I will paste it here.

    • @VY-zt3ph
      @VY-zt3ph 3 роки тому

      @@swarnikagupta16
      see the apprach is that for ugly number you have a choice to multiply it with 2,3 and 5. Each multiplication will give you an ugly number. so recursion node will be splitting into 3 more nodes.
      Create an ugly number HashSet. Suppose you want to generate 100 ugly numbers. We will check if that ugly number is in the HashSet then we will not add it and will end the recursion process there. see this is a pseudo code solution so focus on the approach, not on syntax.
      int N = 100; // total number of ugly numbers to generate.
      Hashset hashset = new Hashset();
      totalNumbersGenerated;
      public void uglyGenerator(int num,int totalNumbersGenrated, int N, Hashset hashset){
      if(hashSet.contains(num) || (totalNumbersGenerated==N))
      { return ; // this will help to terminate the multiple subproblems}
      else if (hashSet.contains(num)!=true)
      { totalNumbersGenerated += 1;
      hashset.add(num);
      uglyGenerator(num*2);
      uglyGenerator(num*3);
      uglyGenerator(num*5);
      }
      }
      But here is a problem in the solution. It will not generate the ugly numbers in sorted order.

  • @ajayjangid1164
    @ajayjangid1164 3 роки тому

    solution should be explained as per exact "dp"-approach's intitution.

  • @ashwithchandra2622
    @ashwithchandra2622 Рік тому

    Excellent dp approach!!!!

  • @Vivek_kumar-gupta
    @Vivek_kumar-gupta 4 роки тому +2

    Awesome bhaiya!!

  • @roopeshkummara3519
    @roopeshkummara3519 4 роки тому

    if the numbers are already present in the array then what should we do?
    ex:-[1,2,3,4,5,6,8,9,10]
    i1,i2,i3=10,9,10
    then what should we do ?
    from now it is getting wrong for me.

    • @sugandh800
      @sugandh800 4 роки тому

      make a function for that let it be:- min(nm2,nm3,nm5) sor out of this 9 is min and it will return 9

  • @himanshu2149
    @himanshu2149 3 роки тому +1

    It's wrong this time because it will give 14 also as ugly number but it is not!! Delete the video bro!!

  • @karamsahoo6852
    @karamsahoo6852 4 роки тому +2

    bacha liye bhaiya

  • @midhileshmomidi2434
    @midhileshmomidi2434 4 роки тому

    How is brute force method tc is nlogn anyone pls explain

  • @shyammahato7178
    @shyammahato7178 5 років тому +2

    good explanation...thank you sir..

  • @lakshaysharma8364
    @lakshaysharma8364 3 роки тому

    thanks for such good explanation

  • @pradeep1198
    @pradeep1198 2 роки тому +1

    Don't post wrong logic and waste our time

  • @sonuranjansingh7583
    @sonuranjansingh7583 4 роки тому

    sare tutorial padh ke video banane aa jate hai bhai concept kaha hai

  • @anupamkumar3197
    @anupamkumar3197 4 роки тому +1

    Best explanation

  • @chiragsahu4584
    @chiragsahu4584 4 роки тому

    Great video thank you so much man!!!!!

  • @AmeyBikram
    @AmeyBikram 5 років тому +1

    Great Explanation

  • @miteshkumar4393
    @miteshkumar4393 4 роки тому

    can any one help why we are taking min only of those ?

    • @saravanan925
      @saravanan925 4 роки тому

      we are going from bottom, so first we need to find i th ugly number before (i+1)th ugly number . Hence we are taking minimum of those

  • @anonymoussloth6687
    @anonymoussloth6687 3 роки тому

    This approach is wrong. 7x2 = 14 This is not an ugly number

  • @ambrish8144
    @ambrish8144 4 роки тому +1

    cool

  • @shreyavardan439
    @shreyavardan439 3 роки тому

    bhai code chala kr bhi dikha diya kro

  • @abhishekgupta9705
    @abhishekgupta9705 3 роки тому

    Wrong Algo!!!!!!!

  • @abhisheksingh-hh7uk
    @abhisheksingh-hh7uk 3 роки тому

    bhai galat approach hai ye

  • @manavneekhra4901
    @manavneekhra4901 3 роки тому

    the algo is wrong.unsuscribing the channel