Trapping Rain Water

In Trapping Rain Water problem we have given N non-negative integers representing an elevation map and the width of each bar is 1. We have to find the amount of water that can be trapped in the above structure. Example Let’s understand that by an example For the above elevation …

Read moreTrapping Rain Water

Tower Of Hanoi

Tower of Hanoi is a mathematical problem with the following conditions:

  • There are three towers
  • There may be n number of rings present
  • The rings are of different sizes
  • Only one disk can be moved at a time
  • Any disk can only be moved on the top of a bigger disk
  • Only the top disk can be removed

Explanation

Let us look at how this problem can be handled when we have two disks

Tower Of Hanoi Explained

We move the top(small) disk onto the next tower after which we move the second disk to the third tower and then eventually shift the first disk as well on to the third tower. (3 moves)

Assumption: The disks are initially sorted

Similarly when we work with 3 disks. We would need 7 steps to shift all of them to the third ring.

  • Disk 1 moved from 1 to 3Tower Of Hanoi
  • Now, Disk 2 moved from 1 to 2Tower Of Hanoi
  • Disk 1 moved from 3 to 2Tower Of Hanoi
  • Disk 3 moved from 1 to 3Tower Of Hanoi
  • Now, Disk 1 moved from 2 to 1
  • Disk 2 moved from 2 to 3
  • Disk 1 moved from 1 to 3

We are thus moving n-1 disks on to the second tower, the last disk to the third tower and n-1 disks onto the first disk thus completing the shift.

Let us now look at a recursive implementation of the same.

JAVA Program for Tower Of Hanoi

public class hanoi
{
  public static void hanoi(int n,char from,char mid,char to)
  {
    if(n==1)
    {
      //Moving disk1 on to the first rod
      System.out.println("Moving disk "+n+"From rod:"+from+"To Rod"+to);
      return;
    }
    //Moving n-1 disks to the second rod
    hanoi(n-1,from,to,mid);
    System.out.println("Moving disk "+n+"From rod:"+from+"To Rod"+to);
    //Moving the disks on top of already moved first disk
    hanoi(n-1,mid,from,to);
  }
  public static void main(String args[])
  {
    int n=3;
    hanoi(n,'A','B','C');
  }
}

C++ Program for Tower Of Hanoi

#include<iostream>
using namespace std;
  void hanoi(int n,char from,char mid,char to)
  {
    if(n==1)
    {
      //Moving disk1 on to the first rod
      cout<<"Moving disk "<< n <<"From rod:"<< from <<" To Rod:"<< to <<endl;
      return;
    }
    //Moving n-1 disks to the second rod
    hanoi(n-1,from,to,mid);
    cout<<"Moving disk "<< n <<"From rod:"<< from <<" To Rod:"<< to <<endl;
    //Moving the disks on top of already moved first disk
    hanoi(n-1,mid,from,to);
  }
  int main()
  {
    int n=4;
    hanoi(n,'A','B','C');
  }
Moving disk 1From rod: A To Rod: B
Moving disk 2From rod:A To Rod:C
Moving disk 1From rod:B To Rod:C
Moving disk 3From rod: A To Rod: B
Moving disk 1From rod:C To Rod:A
Moving disk 2From rod:C To Rod:B
Moving disk 1From rod: A To Rod: B
Moving disk 4From rod:A To Rod:C
Moving disk 1From rod:B To Rod:C
Moving disk 2From rod: B To Rod: A
Moving disk 1From rod:C To Rod:A
Moving disk 3From rod:B To Rod:C
Moving disk 1From rod: A To Rod: B
Moving disk 2From rod:A To Rod:C
Moving disk 1From rod:B To Rod:C

Conclusion for Tower Of Hanoi

We thus come to the conclusion:

15 moves for 4 disks

31 moves for 5 disks

Thus, we come to the conclusion that for n disks we need to make (2^n)-1 moves.

2 disks=(2*2)-1=2 moves

3 disks=(2^3)-1=7 moves

4 disks=(2^4)-1=15 moves

and so on.

Complexity Analysis for Tower Of Hanoi

Time Complexity

Recursive Equation:  T(n) = 2T(n-1) + 1

T(n)= O( 2^{(n+1)} - 1), or you can say O(2^n) which is exponential

Space Complexity

T(n) = T(n-1) + k
T(0) = k
T(1) = 2k
T(2) = 3k
T(3) = 4k
So the space complexity is O(n).

References

Sliding Window Technique

Before getting on and along with what is the sliding window technique? What it does and how it does what it does let us get the hang of this concept by a small problem Given an array of integers, we have the task of finding the minimum sum from all …

Read moreSliding Window Technique

GCD Of Two Numbers

What is Greatest Common Factor? GCD of two numbers is the largest number that divides both of them. Approach-1 Brute Force Finding all the prime factors of both the numbers, then finding the product of the intersection. Finding the largest number that divides both the numbers. What is it that …

Read moreGCD Of Two Numbers

MiniMax Algorithm

Everyone might be wondering. Argh, another new MINIMAX ALGORITHM. Why do we need it? Let’s know to play a game of chess or tic-tac-toe we have often wondered if there was an algorithm to win the game. Explanation A lot of times we might have wondered if It was possible to …

Read moreMiniMax Algorithm

Target Sum

“Target Sum” is a special problem for all the DPHolics I have with me today. There ain’t no need to worry I am going to abandon the rest of my lovely readers. We all have gone through the classic KnapSack problem where we try to find the maximum number of …

Read moreTarget Sum

Counting Bits

All about Counting Bits! Humans have a problem communicating with the computers they made. Why? Humans speak and understand the language they have come to speak and listen to over the years but they taught the poor computer 0’s and 1’s. So today, Let’s teach our computer to count the …

Read moreCounting Bits

Wiggle Sort

Wiggle Sort!? All my readers must’ve found the name of today’s problem very funny. However, it is a very smart problem that tests our understanding of a varied range of concepts. Let us jump straight to the problem without any further confusion. Example You have an array Input: [1,5,1,6,4] Expected …

Read moreWiggle Sort

Rainbow Table

A rainbow table attack might bring in a myriad of pre-nominations and assumptions into the minds of my imaginative readers. Like everyone might know about me till now. I am not someone fond of jumping to things right away so I would want to help my readers with a few …

Read moreRainbow Table