Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organizing Recursive Function Implementations in a Dedicated Folder #181

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Programs/Recursion/binarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package Recursion;


// Binary search using recursion
public class binarySearch {

public static boolean search(int[] arr, int tar, int l, int r){
if(l <= r)
{
int mid = (l+r)/2;

if(arr[mid] == tar){
return true;
} else if(arr[mid] > tar){
return search(arr, tar, l, mid - 1);
} else {
return search(arr, tar, mid + 1, r);
}
}
return false;
}

public static void main(String[] args){
int arr[] = {10, 25, 40, 55, 70, 95};
int tar = 25;

int l = 0;
int r = arr.length-1;

System.out.println(search(arr, tar, l, r));
}
}


// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
41 changes: 41 additions & 0 deletions Programs/Recursion/combinationSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.*;

public class combinationSum {

public static void fun(int ind, int[] arr, int tar, List<List<Integer>> res, List<Integer> ds) {
if(ind >= arr.length){
if(tar == 0){
res.add(new ArrayList(ds));
}
return;
}
// not pick
fun(ind+1, arr, tar, res, ds);

// pick

if(arr[ind]<= tar){
ds.add(arr[ind]);
fun(ind, arr, tar-arr[ind], res, ds);
ds.remove(ds.size()-1);
}

}
public static void main(String[] args) {
int[] arr = {2,3,6,7};
int tar = 7;

List<List<Integer>> res = new ArrayList<>();
fun(0, arr, tar, res, new ArrayList<Integer>());
System.out.println(res);
}
}


<<<<<<< HEAD
// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
=======
This article is Contributed by Himanshu Jha.
Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
>>>>>>> ac2d3cc604ad9612702aeac09180b1fa93dd5fec
33 changes: 33 additions & 0 deletions Programs/Recursion/palindromeString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Recursion;

public class palindromeString {

public static boolean fun(String s, int l, int r){
if(l>=r){
return true;
}
if(s.charAt(l) != s.charAt(r)){
return false;
}
return fun(s, l+1, r-1);
}

public static void main(String[] args){
String str = "MADAM";

int l = 0;
int r = str.length() - 1;
System.out.println(fun(str, l, r));


}
}


<<<<<<< HEAD
// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
=======
This article is Contributed by Himanshu Jha.
Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
>>>>>>> ac2d3cc604ad9612702aeac09180b1fa93dd5fec
36 changes: 36 additions & 0 deletions Programs/Recursion/permutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.*;
public class permutation {

public static void fun(List<List<Integer>> res, List<Integer> ds, boolean[] vis, int[] arr){
if(ds.size() == arr.length){
res.add(new ArrayList<>(ds));
return;
}

for(int i=0;i<arr.length;i++){
if(!vis[i]){
vis[i] = true;
ds.add(arr[i]);
fun(res, ds, vis, arr);
ds.remove(ds.size()-1);
vis[i] = false;
}
}
}

public static void main(String[] args){
int[] arr = {1,2,3};

List<List<Integer>> res = new ArrayList<>();

boolean[] vis = new boolean[arr.length];

fun(res, new ArrayList<>(), vis, arr);
System.out.println(res);

}
}


// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
41 changes: 41 additions & 0 deletions Programs/Recursion/permutationSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.*;

public class permutationSet {

public static void fun(int[] arr, boolean[] vis, List<Integer> ds, List<List<Integer>> res){
if(ds.size() == arr.length){
res.add(new ArrayList<>(ds));
return;
}

for(int i=0;i<arr.length;i++){
if(!vis[i]){
vis[i]=true;
ds.add(arr[i]);
fun(arr, vis, ds, res);
ds.remove(ds.size()-1);
vis[i]=false;
}
}
}
public static void main(String[] args){
int n = 3;
int k = 3; // return kth permutation of 1st 3 non negative numbers

// contruct a array of size n such each element is i+1
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = i+1;
}

boolean[] vis = new boolean[n];
List<List<Integer>> res = new ArrayList<>();

fun(arr, vis, new ArrayList<>(), res);

System.out.println(res.get(k-1));
}
}

// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
45 changes: 45 additions & 0 deletions Programs/Recursion/reverseArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package Programs.Recursion;

public class reverseArray{

public static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void reverse(int[] arr, int l, int r){
if(l>=r){
return;
}
swap(arr, l, r);
reverse(arr, l+1, r-1);
}

public static void print(int[] arr){
for(int i=0; i<arr.length; i++){
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args){
int[] arr = {1,2,3,4,5,6,7,8,9,10};
int n = 10;

int l = 0;
int r = n-1;

reverse(arr, l, r);

print(arr);
}

}

<<<<<<< HEAD

// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
=======
This article is Contributed by Himanshu Jha.
Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
>>>>>>> ac2d3cc604ad9612702aeac09180b1fa93dd5fec
66 changes: 66 additions & 0 deletions Programs/Recursion/sumK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// find subsequences whose sum is k, there is no infinte supplies

//same as combination sum 1 but with some modification
import java.util.*;
public class sumK {

// 1st method :-

public static void fun(int ind, int[] arr, int s, int sum, List<Integer> ds){
if(ind == arr.length){
if(s == sum){
System.out.println(ds);
}
return;
}

// not pick
fun(ind+1, arr, s, sum, ds);

// pick
ds.add(arr[ind]);
s+=arr[ind];
fun(ind+1, arr, s, sum, ds);
s-=arr[ind];
ds.remove(ds.size()-1);
}


// 2nd method :-

public static void f(int ind, int[] arr, int sum, List<Integer> ds){
if(ind == arr.length){
if(sum == 0){
System.out.println(ds);
}
return;
}

// not pick
f(ind+1, arr, sum, ds);

// pick
ds.add(arr[ind]);
f(ind+1, arr, sum-arr[ind], ds);
ds.remove(ds.size()-1);
}

public static void main(String[] args){
int[] arr = {1,2,1};
int sum = 2;

int n = arr.length;

System.out.println("1st Method ");
fun(0, arr, 0, sum, new ArrayList<>());

System.out.println("-------------------");

System.out.println("2nd Method ");
f(0, arr, sum, new ArrayList<>());
}
}


// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
27 changes: 27 additions & 0 deletions Programs/Recursion/theory.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Recusrion is an assumption, in which we know the answer of the subproblem & using that we try to find the solution of the main problem.
A function call itself is called Recusrion.

it is not a data structure instead it is a way to write a program.

Time and Space Complexity :-

Time Complexity :-

the time Complexity totally depends on the number of recursive calls your program made.

example :- a simple factorial program using Recusrion take O(n) time while
The time complexity of a recursive Fibonacci sequence (fib(n) = fib(n-1) + fib(n-2)) is O(2^n)


Space Complexity :-

space complexity refers to the amount of memory space used by the algorithm relative to the size of the input.
In recursion, space complexity is influenced by the stack space used for function calls and the recursive depth.


when the time complexity increases to O(2^n), then It can be optimized using dynamic programming.



This article is Contributed by Himanshu Jha.
Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ It is very easy to contribute, you may follow these steps -
99.[RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list
100. [ReverseString](https://github.com/PrajaktaSathe/Java/blob/main/ReverseString.java) -Program to reverse a String using the java method substring.
101.[Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java
102. [Combination Sum](https://github.com/HIMANSHU-1504/Java/blob/main/Programs/Recursion/combinationSum.java) - Program to demonstrate combination sum.
103. [Palindromic String](https://github.com/HIMANSHU-1504/Java/blob/main/Programs/Recursion/palindromeString.java) -Program to demonstrate Palindromic String using recursion.
104. [Reverse Array](https://github.com/HIMANSHU-1504/Java/blob/main/Programs/Recursion/reverseArray.java) -Program to demonstrate Reverse an array using recursion.
105. [Binary Search](https://github.com/HIMANSHU-1504/Java/blob/main/Programs/Recursion/binarySearch.java) -Program to demonstrate Binary Search using recursion.
106. [Permutations](https://github.com/HIMANSHU-1504/Java/blob/main/Programs/Recursion/permutation.java) -Program to demonstrate Permutations of given array using recursion.
107. [Permutations Set](https://github.com/HIMANSHU-1504/Java/blob/main/Programs/Recursion/permutationSet.java) -Program to find kth Permutations set of given n nums using recursion.
108. [Subsequences with sum k](https://github.com/HIMANSHU-1504/Java/blob/main/Programs/Recursion/sumK.java) - Program to find subsequences with given sum k using recursion.

# Contributors -
## A big thank you to all our contributors!!!
Expand Down