0% found this document useful (0 votes)
9 views3 pages

Recursion (Basics) Solutions

The document presents five solutions to various recursion problems in Java, including finding all occurrences of a key in an array, printing digits of a number, calculating the length of a string, counting substrings, and solving the Tower of Hanoi problem. Each solution includes code snippets and a brief explanation of the recursive approach used. Additionally, a link to a video discussing the solutions is provided.

Uploaded by

Azy Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Recursion (Basics) Solutions

The document presents five solutions to various recursion problems in Java, including finding all occurrences of a key in an array, printing digits of a number, calculating the length of a string, counting substrings, and solving the Tower of Hanoi problem. Each solution includes code snippets and a brief explanation of the recursive approach used. Additionally, a link to a video discussing the solutions is provided.

Uploaded by

Azy Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

RECURSION (Basics) SOLUTIONS

nazim45200@gmail.com
Solution 1:
public class Solution {
public static void allOccurences(int arr[], int key, int i) {
if(i == arr.length) {
return;
}

if(arr[i] == key) {
System.out.print(i+" ");
}
allOccurences(arr, key, i+1);
}
public static void main(String[] args) {
int arr[] = {3, 2, 4, 5, 6, 2, 7, 2, 2};
int key = 2;
allOccurences(arr, key, 0);
System.out.println();
}
}

Solution 2:
public class Solution {
static String digits[] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};

public static void printDigits(int number) {


if(number == 0) {
return;
}

int lastDigit = number%10;


printDigits(number/10);
System.out.print(digits[lastDigit]+" ");
}
public static void main(String[] args) {
printDigits(1234);
System.out.println();
}
}

nazim45200@gmail.com
Solution 3 :
public class Solution {
public static int length(String str) {
if(str.length() == 0) {
return 0;
}

return length(str.substring(1)) + 1;
}
public static void main(String[] args) {
String str = "abcde";
System.out.println(length(str));
}
}

Solution 4 :
public class Solution {
public static int countSubstrs(String str, int i, int j, int n) {
if (n == 1) {
return 1;
}
if (n <= 0) {
return 0;
}

int res = countSubstrs(str, i + 1, j, n - 1) +


countSubstrs(str, i, j - 1, n - 1) -
countSubstrs(str, i + 1, j - 1, n - 2);

if (str.charAt(i) == str.charAt(j)) {
res++;
}
return res;
}
public static void main(String[] args) {

nazim45200@gmail.com
String str = "abcab";
int n = str.length();
System.out.print(countSubstrs(str, 0, n-1, n));
}
}

Solution 5 :
public class Solution {
public static void towerOfHanoi(int n, String src, String helper, String dest) {
if(n == 1) {
System.out.println("transfer disk " + n + " from " + src + " to " + dest);
return;
}

//transfer top n-1 from src to helper using dest as 'helper'


towerOfHanoi(n-1, src, dest, helper);
//transfer nth from src to dest
System.out.println("transfer disk " + n + " from " + src + " to " + helper);
//transfer n-1 from helper to dest using src as 'helper'
towerOfHanoi(n-1, helper, src, dest);
}

public static void main(String args[]) {


int n = 4;
towerOfHanoi(n, "A", "B", "C");
}
}

The Solution for this particular question has also been discussed here :
https://www.youtube.com/watch?v=u-HgzgYe8KA
At timestamp : 00:05

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy