Recursion Introduction

mail@pastecode.io avatar
unknown
java
a month ago
865 B
80
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;
        }
    }    

    public static void main(String[] args) {
        int[] arr = {81,12,3}
        
        System.out.println(firstOcc(arr,7,90));
    }
}
Leave a Comment