diff --git a/algorithms/Java/README.md b/algorithms/Java/README.md index d4d5b7fd..0513ecaa 100644 --- a/algorithms/Java/README.md +++ b/algorithms/Java/README.md @@ -97,7 +97,23 @@ - [Check Tree Traversal](trees/check-tree-traversal.java) ## Backtracking + - [N Queen Problem](backtracking/nqueen.java) ## Bit Manipulation + - [Count Set Bits](bit-manipulation/count-set-bits.java) + +## Recursion + +- [Array sorted or not](recursion/array-sorted.java) +- [Sum of all the elements in the array](recursion/array-sum.java) +- [Binary search using recursion](recursion/binary-search.java) +- [Factorial of a number](recursion/Factorial.java) +- [First uppercase letter in a string](recursion/first-uppercase-letter.java) +- [Linear search using recursion](recursion/linear-search.java) +- [Minimum and maximum element in the array](recursion/min-max-in-array.java) +- [Printing 1 to N and N to 1](recursion/print-n.java) +- [Print PI value in the string](recursion/print-pi.java) +- [Reverse the string](recursion/reverse-string.java) +- [Find the length of the string](recursion/string-length.java) diff --git a/algorithms/Java/recursion/Factorial.java b/algorithms/Java/recursion/Factorial.java new file mode 100644 index 00000000..79f74878 --- /dev/null +++ b/algorithms/Java/recursion/Factorial.java @@ -0,0 +1,34 @@ +package com.dsa; +/*Description: To find factorial of a number using recursion + +Time Complexity: O(n) where n is the number given +*/ + +public class Factorial { + + //function starts + static int fact(int n) { + //base case + if (n == 1) { + return 1; + } + //recursive function call + int ans = fact(n - 1); + //multiply answer with n + return ans * n; + } + + //main starts + public static void main(String[] args) { + int ans = fact(5); + System.out.println("Factorial is: " + ans); + } + + /* + Sample Input: + n=5 + + Output: + Factorial is: 120 + */ +} diff --git a/algorithms/Java/recursion/array-sorted.java b/algorithms/Java/recursion/array-sorted.java new file mode 100644 index 00000000..a6c2d7ce --- /dev/null +++ b/algorithms/Java/recursion/array-sorted.java @@ -0,0 +1,40 @@ +package com.dsa; + +/* +Description: To find whether array is sorted or not using recursion + +Time Complexity: O(n) where n is the number of elements present in the array +*/ + +public class ArraySorted { + + // function body + static boolean isSorted(int[] arr, int n) { + //base case + //if the length of the array is 1, it means it is sorted + //so return true + if (n == 1) { + return true; + } + //since n means length of the array, check if the element at n-1 index(last) is greater than the element at n-2 index (second last) + //if it is greater, then make a recursive function call for checking the remaining elements + if (arr[n - 1] > arr[n - 2]) { + return isSorted(arr, n - 1); + } + //if not, return false + //this means array is not sorted + return false; + } + //main starts + public static void main(String[] args) { + int[] arr = {1, 2, 3, 4, 5}; + boolean ans = isSorted(arr, arr.length); + System.out.println("Array sorted : " + ans); + } + /* + Sample Input: + arr = [1, 2, 3, 4, 5] + + Output: Array sorted : true + */ +} diff --git a/algorithms/Java/recursion/array-sum.java b/algorithms/Java/recursion/array-sum.java new file mode 100644 index 00000000..dceb8659 --- /dev/null +++ b/algorithms/Java/recursion/array-sum.java @@ -0,0 +1,38 @@ +package com.dsa; + +/* +Description: To find the sum of all the elements in the array using recursion + +Time Complexity: O(n) +*/ + +public class ArraySum { + + //function starts + static int findSum(int[] arr, int n) { + //base case + //if there is only one element in the array + //that element will be the sum + if (n == 1) { + return arr[0]; + } + //recursive function call + int ans = findSum(arr, n - 1); + //add all the array elements in the answer + return ans + arr[n - 1]; + } + + //main starts + public static void main(String[] args) { + int arr[] = {3, 4, 6}; + int ans = findSum(arr, arr.length); + System.out.println("Sum is: " + ans); + } +/* +Sample Input: +arr=[3,4,6] + +Output: +Sum is: 13 +*/ +} diff --git a/algorithms/Java/recursion/binary-search.java b/algorithms/Java/recursion/binary-search.java new file mode 100644 index 00000000..a98734ff --- /dev/null +++ b/algorithms/Java/recursion/binary-search.java @@ -0,0 +1,37 @@ +package com.dsa; + +/* +Description: Binary Search Program in Java using Recursion + +Time Complexity: O(log N) +*/ + +public class BinarySearch { + //main starts + public static void main(String[] args) { + int arr[] = {12, 19, 23, 45, 50, 67, 89}; + int target = 89; + int ans = binarySearchR(arr, target, 0, arr.length - 1); + System.out.println("Element found at index: "+ ans); + } + + //function + static int binarySearchR(int[] arr, int target, int start, int end) { + //finding the mid + int mid = start + (end - start) / 2; + //base case, if element not found, return -1 + if (start > end) { + return -1; + } + //return the index if the mid is found + if (arr[mid] == target) { + return mid; + } + //recursisve function call + if (arr[mid] > target) { + return binarySearchR(arr, target, start, mid - 1); + } else { + return binarySearchR(arr, target, mid + 1, end); + } + } +} diff --git a/algorithms/Java/recursion/first-uppercase-letter.java b/algorithms/Java/recursion/first-uppercase-letter.java new file mode 100644 index 00000000..c5622a42 --- /dev/null +++ b/algorithms/Java/recursion/first-uppercase-letter.java @@ -0,0 +1,39 @@ +package com.dsa; + +/*Description: To find the first uppercase letter in the string using recursion + +TIme Complexity: O(n) where n is the number of characters present in the string +*/ +public class firstUppercaseLetter { + //main starts + public static void main(String[] args) { + String s = "hEllo eveRyoNe"; + char c = printfirstUP(s, 0); + System.out.println("First uppercase letter in the string: " + c); + } + + //function starts + static char printfirstUP(String s, int i) { + //base case + //if i is equal to the string length, return '\0' + //means no uppercase letter found + if (i == s.length()) { + return '\0'; + } + //if the character at the given index is a capital letter + //return it + if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') { + return s.charAt(i); + } + //recursive function call + return printfirstUP(s, i + 1); + } + + /* + Sample Input: + s= hEllo eveRyoNe + + Output: + E + */ +} diff --git a/algorithms/Java/recursion/linear-search.java b/algorithms/Java/recursion/linear-search.java new file mode 100644 index 00000000..01254813 --- /dev/null +++ b/algorithms/Java/recursion/linear-search.java @@ -0,0 +1,30 @@ +package com.dsa; +/* +Description: Linear search in java using recursion + +Time Complexity: O (N) + */ + +public class LinearSearch { + //main starts + public static void main(String[] args) { + int arr[]={7,2,1,98,43,12,55}; + int target = 2; + int ans = linearSearch(arr, target, 0); + System.out.println("Element found at index: "+ ans); + } + + //function + static int linearSearch(int[] arr, int target, int i) { + //base case + if(i==arr.length){ + return -1; + } + //if the target element is found, return its index + if(arr[i]==target){ + return i; + } + //make a recursive function call by incrementing variable i by 1 + return linearSearch(arr, target, i+1); + } +} diff --git a/algorithms/Java/recursion/min-max-in-array.java b/algorithms/Java/recursion/min-max-in-array.java new file mode 100644 index 00000000..0d0db3bf --- /dev/null +++ b/algorithms/Java/recursion/min-max-in-array.java @@ -0,0 +1,54 @@ +package com.dsa; +/* +Description: Java Program to find minimum and maximum value from the array using recursion + +Time Complexity: O(n) where n is the number of elements inside the array + +*/ + +public class Main { + + + //function to find maximum element in the array + static int findMax(int[] arr, int n) { + //base case + //if there is only 1 element in array, it will be the maximum element + if (n == 1) { + return arr[0]; + } + //recursive function call + int mx = findMax(arr, n - 1); + //return maximum element from every recursive call to the function + return Math.max(arr[n - 1], mx); + + } + + //function to find minimum element in the array + static int findMin(int[] arr, int n) { + //base case + //if there is only 1 element in array, it will be the minimum element + if (n == 1) { + return arr[0]; + } + //recursive function call + int mn = findMin(arr, n - 1); + //return minimum element from every recursive call to the function + return Math.min(arr[n - 1], mn); + } + + public static void main(String[] args) { + int[] arr = {-1, -2, 3, 4, 5, 6}; + int min = findMin(arr, arr.length); + System.out.println("Minimum element in array: " + min); + int max = findMax(arr, arr.length); + System.out.println("Maximum element in array: " + max); + } + + /* + Sample Input: + arr=[-1,-2,3,4,5,6] + Output: + Minimum element in array: -2 + Maximum element in array: 6 + */ +} diff --git a/algorithms/Java/recursion/print-n.java b/algorithms/Java/recursion/print-n.java new file mode 100644 index 00000000..11e062d3 --- /dev/null +++ b/algorithms/Java/recursion/print-n.java @@ -0,0 +1,52 @@ +package com.dsa; +/* +Description: To print from 1 to N and N to 1 using recursion + +Time Complexity: O(n) +*/ + +public class PrintN { + //main starts + public static void main(String[] args) { + System.out.println("Printing 1 to N:"); + print2(5); + System.out.println(); + System.out.println("Printing N to 1"); + print1(5); + } + + //for Printing 1 to N + static void print2(int n) { + //base case + if (n == 0) { + return; + } + //recursive function call + print2(n - 1); + + //print the value of n at each recursive function call + System.out.print(n + " "); + } + + //for Printing N to 1 + static void print1(int n) { + //base case + if (n == 0) { + return; + } + //print the value of n first + System.out.print(n + " "); + + //then make the recursive function call + print1(n - 1); + } + + /* + Output: + + Printing 1 to N: + 1 2 3 4 5 + Printing N to 1 + 5 4 3 2 1 + */ +} diff --git a/algorithms/Java/recursion/print-pi.java b/algorithms/Java/recursion/print-pi.java new file mode 100644 index 00000000..87616da8 --- /dev/null +++ b/algorithms/Java/recursion/print-pi.java @@ -0,0 +1,36 @@ +package com.dsa; + +public class printPi { + //main starts + public static void main(String[] args) { + String s = "piabcpidefpi"; + changetoPi(s, 0); + } + + //function starts + private static void changetoPi(String s, int i) { + //base case + if (i == s.length()) { + return; + } + //if we found "pi" in the string, print 3.14 instead of it + //increment i by 2 in recursive function call + if (s.charAt(i) == 'p' && s.charAt(i + 1) == 'i') { + System.out.print("3.14"); + changetoPi(s, i + 2); + } + //else print the character itself and increment i by 1 in recursive function call + else + { + System.out.print(s.charAt(i)); + changetoPi(s, i + 1); + } + } + /* + Sample Input: + s = piabcpidefpi + + Output: + 3.14abc3.14def3.14 + */ +} diff --git a/algorithms/Java/recursion/reverse-string.java b/algorithms/Java/recursion/reverse-string.java new file mode 100644 index 00000000..ebf7fb43 --- /dev/null +++ b/algorithms/Java/recursion/reverse-string.java @@ -0,0 +1,40 @@ +package com.dsa; + +import java.util.Arrays; +import java.util.Collections; + +public class ReverseString { + public static void main(String[] args) { + StringBuilder s = new StringBuilder("latte"); + String ans = reverse(s, 0, s.length() - 1); + System.out.println(ans); + //another way + String s1 = "hiii"; + char arr[] = s1.toCharArray(); + char ans2[] = reverse2(arr, 0, arr.length - 1); + String a = String.valueOf(ans2); + System.out.println(a); + } + + static char[] reverse2(char[] arr, int start, int end) { + if (start > end) { + return arr; + } + char temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + + return reverse2(arr, start + 1, end - 1); + + } + + static String reverse(StringBuilder s, int start, int end) { + if (start > end) { + return s.toString(); + } + char temp = s.charAt(end); + s.setCharAt(end, s.charAt(start)); + s.setCharAt(start, temp); + return reverse(s, start + 1, end - 1); + } +} diff --git a/algorithms/Java/recursion/string-length.java b/algorithms/Java/recursion/string-length.java new file mode 100644 index 00000000..a573d8b7 --- /dev/null +++ b/algorithms/Java/recursion/string-length.java @@ -0,0 +1,29 @@ +package com.dsa; + +/* +Description: To find the length of the string using recursion + +Time Complexity: O(n) where n is the number of characters present in the string +*/ + +public class StringLength { + + //function body + static int findLength(String s) { + //if we are at the end of the string return 0 + //this is the base case + if (s.equals("")) { + return 0; + } + //recursive function call + //at every recursive function call, we are removing the last character of the string using substring() method + //since we are removing one character at every recursive function call we are adding one to it, to count the character + return findLength(s.substring(1)) + 1; + } + public static void main(String[] args) { + String s = "Hello"; + //calling the function to return the string length + int len = findLength(s); + System.out.println(len); + } +}