Untitled

mail@pastecode.io avatar
unknown
java
a month ago
1.3 kB
54
Indexable
Never
import java.util.Scanner;
class Main {
    public static void printIncreasing(int n){
        if(n == 0){
            return;
        }

        printIncreasing(n-1);
        System.out.println(n);
    }

    public static void printDecreasing(int n){
        if(n==0){
            return;
        }

        System.out.println(n);
        printDecreasing(n-1); 
    }

    public static int firstOcc(int[] arr, int tar, int idx){
        if(idx == arr.length){ // find first occurence from nth index to last
            return -1;
        }

        int aageKaFirstOcc = firstOcc(arr, tar, idx+1);

        if(arr[idx] == tar){
            return idx;
        } else {
            return aageKaFirstOcc;
        }
    }    

    // print steps to move n number of discs from A to C with the help of B
    public static void towerOfHanoi(int n, int A, int C, int B){
        if(n == 0){
            return;
        }

        towerOfHanoi(n-1, A, B, C); // moving n-1 discs from A to B using C
 
        // moving nth disc from A to C
        System.out.println("Moving disc " + n + " from " + A + " to " + C);

        towerOfHanoi(n-1, B, C, A); // moving n-1 discs B to C using A
    }

    public static void main(String[] args) {
        towerOfHanoi(3,10,30,20);
    }
}
Leave a Comment