life is too short for a diary




Dear Vishi, daily log on Feb 05, 2023.

Tags: diary letters

Dear Vishi, this is my daily log for Feb 05, 2023.

Leetcode problem

Today I tried solving problem to Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

My first solution was a brute force method.

class Solution {
    public double myPow(double x, int n) {
        if (n == 0){
            return 1;
        }

        double power = 1.0;
        boolean flip = false;

        if (n < 0){
            n *= -1;
            flip = true;
        }

        for (int i = 1; i <= n ; i++){
            power *= x;
        }

        if (flip){
            power = 1.0 / power;
        }

        return power;
    }
}

This didn't work and gave time limit exceeded. We can modify this and use a divide-and-conquer approach to calculate x raised to the power n in logarithmic time:

class Solution {
    public double myPow(double x, int n) {
        
        if (n == 0) return 1;

        double half = myPow(x, n / 2);
        
        if (n % 2 == 0) return half * half;

        else if (n > 0) return half * half * x;

        else  return half * half / x;
    }
}

Here the trick is that nstead of trying to solve the problem all at once, the code splits the problem into smaller parts by dividing n by 2 each time and calling itself with the new value of n.

  1. When "n" is equal to 0, the result is always 1, as any number raised to 0 is 1.

  2. Otherwise, the method calculates the result of the function for "n/2" (stored in "half"), which is x^(n/2).

  3. If "n" is even, then "n/2" is also even and x^n can be written as (x^(n/2))^2. The result is then (x^(n/2))^2 = half * half.

  4. If "n" is positive and odd, then x^n can be written as x^(n-1) * x. Since (n-1) is even, the result can be calculated as (x^((n-1)/2))^2 * x = half * half * x.

  5. If "n" is negative, then x^n can be written as 1/(x^-n). Since (-n) is positive, the result can be calculated as 1/(x^(-n)) = 1/(x^n) = half * half / x.

Tennis

I went to play tennis today. I guess consistence is the key. Everytime I miss a week or so, I see dip in my performance. Tennis meetup was at 3 pm but I went at 4.30 pm. I just had lunch at noon. Few folks play till night.

B. R. Ambedkar views on Gandhi

In a 1955 BBC interview, B. R. Ambedkar said, "Gandhi was never a Mahatma; I refuse to call him a Mahatma." There were strong disaggrements between Gandhi and Ambedkar.

Interestingly, Albert Einstein had a different opinion about Gandhi, "Generations to come will scarce believe that such a one as this ever in flesh and blood walked upon this earth"

Pakistan's former President, Pervez Musharraf Dies

Just read news today that Pervez Musharraf has died. There was a tweet by Mehbooba Mufti that I find problematic.

He was architect of Kargil war which was a fruitless war. Sending condolescences is formally accepted, but she wrote that he genuiely tried to address Kahsmir issues.

I guess there lot a disaggrements I have with BJP politics of Hindutava, but this tweet is in bad taste.

Below is the link of his interview.


comments powered by Disqus