Reduce a Number to 1 in Minimum Steps | Reduce N to 1 | Bit Manipulation Interview Problems

Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we explain the reduce N to 1 problem using bit manipulation in data structure in Java. In this problem,
1. You are given a positive number N.
2. You have to find the minimum number of operations required to convert N into 1.
3. Operations allowed :
(i) If n is even, you have to replace n with n/2.
(ii) If n is odd, you can replace n with either n-1 or n+1.
To submit this question, click here: www.pepcoding.com/resources/d...
For a better experience and more exercises, VISIT: www.pepcoding.com/resources/o...
#bits #bitmanipulation #bitmasking
Have a look at our result: www.pepcoding.com/placements
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education
Join us on Telegram: t.me/joinchat/UVTjJE83a-zFnPB

Пікірлер: 37

  • @utpalpodder-pk6vq
    @utpalpodder-pk6vq3 жыл бұрын

    so its good to have TLE in somecases....at least we can learn some optimization.

  • @ashwaniponia3817
    @ashwaniponia38173 жыл бұрын

    Great Explanation sir! one point we can also perform division by 2 and rem by 2 using bit manipulation to further reduce the time complexity.

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    yes

  • @rajgupta5158
    @rajgupta51583 жыл бұрын

    Question is much similar to recursive but little bit observation make it efficient. thank you sir

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    Keep learning and keep growing beta!

  • @ghanibhai1630
    @ghanibhai16302 жыл бұрын

    how to solve if we consider ODD in the form of 2x+1

  • @raushanraj_99
    @raushanraj_99 Жыл бұрын

    Sir your code will give wrong output in case of multiple of 6. There should be res += 2 Rather than res = 2 In this code this is always going to return 2 for all multiple of 6.

  • @rohitshinde8478
    @rohitshinde84783 жыл бұрын

    Thank you sir

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    Keep learning and keep growing😊 If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @princerajput9302
    @princerajput93023 жыл бұрын

    After changing data type in function from int to long your program is run. But you doesn't change the data type int to long in main function where you call this function. And in your main function you pass int variable. So how your programs run????. Because after changing there is no function which accept int data type parameter

  • @pranshulkharniwal8146
    @pranshulkharniwal81462 жыл бұрын

    majha aa gayaa

  • @mickyman753
    @mickyman7532 жыл бұрын

    sir ye 4x+1,4x+3 wali insight aur kisi question mai kaam aye toh description box main question link add kr dijiyega pls , ye khud se soch toh pana toh boht difficult hai

  • @mukultaneja7243
    @mukultaneja72433 жыл бұрын

    Sir how can we determine such pattern in such questions, when we can use BFS or DP in these ?? I mean how do we know that we should take 4x+1 and 4x+3 numbers for example ? Please tell !

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    bits questions are almost dependent on observation

  • @tushargupta3236
    @tushargupta32363 жыл бұрын

    To make a choice between n - 1 and n + 1, we can count the set bits in each of them and prefer the one with lesser set bits. Our main aim is to have a single set bit in the number so we can have repetitive n/2 operations on the number.

  • @SatyaPrakash-dj8ix

    @SatyaPrakash-dj8ix

    2 ай бұрын

    exactlyy

  • @akatsuki1363
    @akatsuki13633 жыл бұрын

    Best👌🔥

  • @akatsuki1363

    @akatsuki1363

    3 жыл бұрын

    Sir for detecting even and odd why did you choose 4x 4x+1 and 4x+3 instead of 2x and 2x+1

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem. If you like our efforts, we request a review g.page/Pepcoding/review?rc You can subscribe to our channel here kzread.infoabout?view_as=subscriber

  • @yashugarg9048
    @yashugarg90483 жыл бұрын

    When we try this problem for the first time then how it is possible to get 4x+1 etc in place of 2x+1.... Thanks

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    beta, kaafi derr dekhta rha sawaal ko firr dikha ye. it took mei 5 hours for this.

  • @mickyman753
    @mickyman7532 жыл бұрын

    after doing this question ,do try the dp question with the same name

  • @boredpotato6366

    @boredpotato6366

    2 жыл бұрын

    I tried to memoize the recursive approach for this problem but it gives MLE bcz n is very large.

  • @rahulbhatia3075
    @rahulbhatia30753 жыл бұрын

    Premium content 🔥

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    Thank you very much!! share among your peers

  • @rahulbhatia3075

    @rahulbhatia3075

    3 жыл бұрын

    @@Pepcoding sir pubg ban ho gaya, ab sir views pakka badh jainge sir 🔥🔥

  • @vimarshkoul930
    @vimarshkoul9303 жыл бұрын

    sir I guess for n==3 res should be res+=2 but you wrote res=2 ?? I tried res+=2 on pepcoding. It showed the status as correct answer.please correct me if i'm wrong. Great video by the way.

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    ek he baat hai beta iss case mei. n == 3 se 2 he raste hain

  • @vimarshkoul930

    @vimarshkoul930

    3 жыл бұрын

    @@Pepcoding par sir n==6 ke liye apka code shyd 2 return karega??

  • @anshumandas5392
    @anshumandas53923 жыл бұрын

    interview me ye qs. aa gaya to sir ko bulana padega explain karne ke liye🤣. humse to na ho payega pressure me itna kuch explain

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    Are nahi Beta sab ho jaega padhte rahe mehnat se .

  • @NishantKumar-hu7dq
    @NishantKumar-hu7dq3 жыл бұрын

    Sir your code will give wrong output in case of multiple of 6. There should be res += 2 Rather than res = 2 In this code this is always going to runtrn 2 for all multiple of 6.

  • @princerajput9302

    @princerajput9302

    3 жыл бұрын

    Yes you are correct

  • @AyushRaj-gf2ce
    @AyushRaj-gf2ce3 жыл бұрын

    isko apan dp se bhi kr sakte hai...

  • @Pepcoding

    @Pepcoding

    3 жыл бұрын

    yes

  • @pratyushgoel99

    @pratyushgoel99

    3 жыл бұрын

    ​@@Pepcoding This approach is much more efficient than dp right?

  • @multiplewaya-qk7uq

    @multiplewaya-qk7uq

    3 жыл бұрын

    bro Runtime aajayga mera agya too high dp

Келесі