Binary Exponentiation | Pow(x,n) | Leetcode #50

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024
  • This video explains the most optimal technique to find pow(x,n) using the binary exponentiation technique. We have shown you the bruteforce followed by the optimal technique. We will learn binary exponentiation from scratch. This is one of the most important math algorithm and a very frequently asked interview problem.
    CODE LINK is present below as usual. If you find any difficulty or have any queries then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    ======================================PLEASE DONATE=============================
    🧡 SUPPORT OUR WORK: / techdose
    💚 UPI-ID: surya.kahar@ybl
    💞JOIN Membership: / @techdose4u
    ==============================================================================
    INSTAGRAM : / surya.pratap.k
    LinkedIn: / surya-pratap-kahar-47b...
    WEBSITE: techdose.co.in/
    TELEGRAM Channel LINK: t.me/codewithT...
    TELEGRAM Group LINK: t.me/joinchat/...
    =======================================================================
    USEFUL LINKS:
    🟠Must do TIPS to ACE Virtual Interview: • 🔴Must do Tips to ACE y...
    🟢Best strategy to excel your coding interview: • 🔴Best strategy to exce...
    🟡Get your dream job in 1 month: • 🔴Get your dream job in...
    🔵How to crack dream job in just 2 months: • How to crack dream job...
    🟣7 Days DSA plan: techdose.co.in...
    RELATED LINKS:
    CODE LINK: gist.github.co...

ความคิดเห็น • 21

  • @hiteshbhandari7009
    @hiteshbhandari7009 11 หลายเดือนก่อน +8

    'Squaring the base and taking half of the exponential' - You summed up the algorithm very well :)

  • @Sranju23
    @Sranju23 8 หลายเดือนก่อน +1

    Nice explanation. However there's one edge case added on this LC problem.
    If given number is negative then take the absolute value but store this as long to avoid overflow.
    At the end return accordingly.

  • @Martinnnnnn-e5s
    @Martinnnnnn-e5s หลายเดือนก่อน

    amazing! I finally understood the iterative approach

  • @Bkmap
    @Bkmap 4 หลายเดือนก่อน

    can you please tell me which software are you using in this video to teach .

  • @yogitajoshi7218
    @yogitajoshi7218 ปีที่แล้ว

    Thank you soo much sir. Amazing Explanation🙌

  • @ajaygonepuri2285
    @ajaygonepuri2285 ปีที่แล้ว

    Great Explanation!

  • @chakravarthybatna1589
    @chakravarthybatna1589 11 หลายเดือนก่อน

    great explanation;

  • @GameplayRoudie
    @GameplayRoudie ปีที่แล้ว

    how can double hold big value in "ans" variable? it will overflow

  • @rahulsharma15
    @rahulsharma15 ปีที่แล้ว

    Nice thanks for sharing

  • @anshumaan1024
    @anshumaan1024 ปีที่แล้ว +1

    nice explanation

    • @techdose4u
      @techdose4u  ปีที่แล้ว +1

      Thanks and welcome

  • @franly656
    @franly656 7 หลายเดือนก่อน

    Great!

  • @SK-qn5ry
    @SK-qn5ry ปีที่แล้ว

    Does anyone took techdose DSA course 0:31?
    How much it costs?
    What is timings of live classes?

    • @jawwadakhter5261
      @jawwadakhter5261 ปีที่แล้ว

      Bro techdose ke playlist me videos hai, usme dekho sab bataya hai

  • @ToanPham-wr7xe
    @ToanPham-wr7xe 2 หลายเดือนก่อน

    😮

  • @btengaming4899
    @btengaming4899 22 วันที่ผ่านมา

    where is the code of even exponent

    • @techdose4u
      @techdose4u  22 วันที่ผ่านมา

      Please do dry run on the given code for both odd & even.

  • @rahulchoudhary414
    @rahulchoudhary414 ปีที่แล้ว

    '''class Solution {
    public double myPow(double x, int n) {
    //exponentiation
    double result=1;
    int m=Math.abs(n);
    while(m>=1){

    if(m%2==1){
    result=result*x;
    }
    x=x*x;
    m=m/2;

    }
    return (n

    • @kidoo1567
      @kidoo1567 ปีที่แล้ว

      Use long data type..

    • @bibhabpanda
      @bibhabpanda ปีที่แล้ว

      after result=result*x; write m=m-1;

    • @developer_save
      @developer_save ปีที่แล้ว

      // here is the correct one
      class Solution {
      public double myPow(double x, int n) {
      double result = 1.0;
      long m = Math.abs((long) n); // Convert n to long to handle Integer.MIN_VALUE
      while (m > 0) {
      if (m % 2 == 1) {
      result *= x;
      }
      x *= x;
      m /= 2;
      }
      return (n < 0) ? 1 / result : result;
      }
      }
      //The issue is with the line m = m / 2;. In the exponentiation by squaring algorithm, you typically divide the exponent n by 2 in each iteration. However, in your code, you are modifying m instead of n. As a result, the algorithm won't work correctly for negative exponents.