Skip to main content

Recursion One Shot - Advanced Level Questions | Placement (Tech)

Recursion in Java-DSA



Recursion is a programming technique where a function calls itself repeatedly until a certain condition is met. In Java, recursion is commonly used in Data Structures and Algorithms (DSA) to solve problems involving trees, graphs, and other recursive structures.

Here is an example of a recursive function to calculate the factorial of a number:

Here is an example of a recursive function to calculate the factorial of a number:

java
public static int factorial(int n) {
if (n == 0)
return 1
 } 
else
return n * factorial(n-1);
 } 
}




This function takes an integer n as input and returns the factorial of n. The factorial of a number is the product of all integers from 1 to that number. For example, the factorial of 4 is 4 * 3 * 2 * 1 = 24.

The function first checks if n is equal to 0. If it is, it returns 1 (the base case). If n is not equal to 0, it calls itself with n-1 as the input and multiplies the result with n.

It's important to note that recursive functions can be resource-intensive and can cause a stack overflow error if the recursion depth is too deep. Therefore, it's important to design recursive algorithms carefully and consider alternative approaches when necessary.







ADVANCED

Q1. Print all the permutations of a string.


public class Recursion3 {

 

   public static void printPermutation(String str, int idx, String perm) {

       if(str.length() == 0) {

           System.out.println(perm);

           return;

       }

      

       for(int i=0; i<str.length(); i++) {

           char currChar = str.charAt(i);

           String newStr = str.substring(0, i) + str.substring(i+1);

           printPermutation(newStr, idx+1, perm+currChar);

       }

   }

   public static void main(String args[]) {

       String str = "abc";

       printPermutation(str, 0, "");

   }

}

 


Time complexity - O(n*n!)


Q2. CountPathMaze 


public class Recursion3 {

 

   public static int countPaths(int i, int j, int m, int n) {

       if(i == m-1 || j == n-1) {

           return 1;

       }

 

       return countPaths(i+1, j, m, n) + countPaths(i, j+1, m, n);

   }

 

   public static void main(String args[]) {

       int m = 4, n = 5;

       System.out.println(countPaths(0, 0, m, n));

   }

}

 


Time complexity - O(2^(m+n))


Q3. Tiling problem


public class Recursion3 {

 

   public static int placeTiles(int n, int m) {

       if(n < m) {

           return 1;

       } else if(n == m) {

           return 2;

       }

 

       return placeTiles(n-1, m) + placeTiles(n-m, m);

   }

 

   public static void main(String args[]) {

       int n = 4, m = 4;

       System.out.println(placeTiles(n, m));

   }

}

 



Q4. Friends pairing problem


public class Recursion3 {

 

   public static int pairFriends(int n) {

      if(n <= 1) {

          return 1;

      }

 

       return pairFriends(n-1) + (n-1) * pairFriends(n-2);

   }

 

   public static void main(String args[]) {

       int n = 3;

       System.out.println(pairFriends(n));

   }

}

 



Q5. Subsets of a set


import java.util.ArrayList;

 

public class Recursion3 {

 

   public static void printSubsets(ArrayList<Integer> subset) {

       for(int i=0; i<subset.size(); i++) {

           System.out.print(subset.get(i)+" ");

       }

       System.out.println();

   }

 

   public static void findSubsets(int n, ArrayList<Integer> subset) {

       if(n == 0) {

           printSubsets(subset);

           return;

       }

 

       findSubsets(n-1, subset);

       subset.add(n);

       findSubsets(n-1, subset);

       subset.remove(subset.size() - 1);

   }

 

   public static void main(String args[]) {

       int n = 3;

       findSubsets(n, new ArrayList<Integer> ());

   }

}

 


 

Comments

Popular posts from this blog

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

2D Arrays | Java Complete Placement Course | Lecture 11

Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...