From 47bf3c0e510f1f02839c774d09c04f7f2039f734 Mon Sep 17 00:00:00 2001 From: HIMANSHU-1504 Date: Tue, 9 Jan 2024 22:03:16 +0530 Subject: [PATCH 1/7] first --- Programs/Recursion/combinationSum.java | 34 ++++++++++++++++++++++ Programs/Recursion/palindromeString.java | 24 ++++++++++++++++ Programs/Recursion/reverseArray.java | 36 ++++++++++++++++++++++++ Programs/Recursion/theory.txt | 27 ++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 Programs/Recursion/combinationSum.java create mode 100644 Programs/Recursion/palindromeString.java create mode 100644 Programs/Recursion/reverseArray.java create mode 100644 Programs/Recursion/theory.txt diff --git a/Programs/Recursion/combinationSum.java b/Programs/Recursion/combinationSum.java new file mode 100644 index 0000000..367ec34 --- /dev/null +++ b/Programs/Recursion/combinationSum.java @@ -0,0 +1,34 @@ +package Programs.Recursion; + +import java.util.*; + +public class combinationSum { + + public static void fun(int ind, int[] arr, int tar, List> res, List 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> res = new ArrayList<>(); + fun(0, arr, tar, res, new ArrayList()); + System.out.println(res); + } +} diff --git a/Programs/Recursion/palindromeString.java b/Programs/Recursion/palindromeString.java new file mode 100644 index 0000000..706434c --- /dev/null +++ b/Programs/Recursion/palindromeString.java @@ -0,0 +1,24 @@ +package Programs.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)); + + + } +} diff --git a/Programs/Recursion/reverseArray.java b/Programs/Recursion/reverseArray.java new file mode 100644 index 0000000..118b873 --- /dev/null +++ b/Programs/Recursion/reverseArray.java @@ -0,0 +1,36 @@ +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 Date: Tue, 9 Jan 2024 22:05:12 +0530 Subject: [PATCH 2/7] Update combinationSum.java --- Programs/Recursion/combinationSum.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Programs/Recursion/combinationSum.java b/Programs/Recursion/combinationSum.java index 367ec34..5c9e90c 100644 --- a/Programs/Recursion/combinationSum.java +++ b/Programs/Recursion/combinationSum.java @@ -32,3 +32,7 @@ public static void main(String[] args) { System.out.println(res); } } + + +This article is Contributed by Himanshu Jha. +Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/ From 87286e19c4df99ba5ba3ea1d33f26e5391335aa1 Mon Sep 17 00:00:00 2001 From: Himanshu Jha <90416078+HIMANSHU-1504@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:05:33 +0530 Subject: [PATCH 3/7] Update reverseArray.java --- Programs/Recursion/reverseArray.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Programs/Recursion/reverseArray.java b/Programs/Recursion/reverseArray.java index 118b873..3423e75 100644 --- a/Programs/Recursion/reverseArray.java +++ b/Programs/Recursion/reverseArray.java @@ -34,3 +34,6 @@ public static void main(String[] args){ } } + +This article is Contributed by Himanshu Jha. +Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/ From c873a1b6623a5df66af9f44e91cc954bdc55722c Mon Sep 17 00:00:00 2001 From: Himanshu Jha <90416078+HIMANSHU-1504@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:05:54 +0530 Subject: [PATCH 4/7] Update palindromeString.java --- Programs/Recursion/palindromeString.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Programs/Recursion/palindromeString.java b/Programs/Recursion/palindromeString.java index 706434c..568b6fa 100644 --- a/Programs/Recursion/palindromeString.java +++ b/Programs/Recursion/palindromeString.java @@ -22,3 +22,7 @@ public static void main(String[] args){ } } + + +This article is Contributed by Himanshu Jha. +Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/ From ac2d3cc604ad9612702aeac09180b1fa93dd5fec Mon Sep 17 00:00:00 2001 From: Himanshu Jha <90416078+HIMANSHU-1504@users.noreply.github.com> Date: Tue, 9 Jan 2024 22:15:25 +0530 Subject: [PATCH 5/7] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index cb3d676..f3f6aa5 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,9 @@ 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. +103.[Reverse Array](https://github.com/HIMANSHU-1504/Java/blob/main/Programs/Recursion/reverseArray.java) -Program to demonstrate Reverse an array using recursion. # Contributors - ## A big thank you to all our contributors!!! From 3b9e3dcaa805a355b28ab130079c3b8e4d7b709a Mon Sep 17 00:00:00 2001 From: HIMANSHU-1504 Date: Wed, 10 Jan 2024 16:40:22 +0530 Subject: [PATCH 6/7] added some programs --- Programs/Recursion/binarySearch.java | 36 +++++++++++++ Programs/Recursion/combinationSum.java | 6 ++- Programs/Recursion/palindromeString.java | 6 ++- Programs/Recursion/permutation.java | 36 +++++++++++++ Programs/Recursion/permutationSet.java | 41 +++++++++++++++ Programs/Recursion/reverseArray.java | 4 ++ Programs/Recursion/sumK.java | 66 ++++++++++++++++++++++++ 7 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 Programs/Recursion/binarySearch.java create mode 100644 Programs/Recursion/permutation.java create mode 100644 Programs/Recursion/permutationSet.java create mode 100644 Programs/Recursion/sumK.java diff --git a/Programs/Recursion/binarySearch.java b/Programs/Recursion/binarySearch.java new file mode 100644 index 0000000..c1a2932 --- /dev/null +++ b/Programs/Recursion/binarySearch.java @@ -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/ \ No newline at end of file diff --git a/Programs/Recursion/combinationSum.java b/Programs/Recursion/combinationSum.java index 367ec34..21e9abf 100644 --- a/Programs/Recursion/combinationSum.java +++ b/Programs/Recursion/combinationSum.java @@ -1,5 +1,3 @@ -package Programs.Recursion; - import java.util.*; public class combinationSum { @@ -32,3 +30,7 @@ public static void main(String[] args) { System.out.println(res); } } + + +// This article is Contributed by Himanshu Jha. +// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/ \ No newline at end of file diff --git a/Programs/Recursion/palindromeString.java b/Programs/Recursion/palindromeString.java index 706434c..926bc18 100644 --- a/Programs/Recursion/palindromeString.java +++ b/Programs/Recursion/palindromeString.java @@ -1,4 +1,4 @@ -package Programs.Recursion; +package Recursion; public class palindromeString { @@ -22,3 +22,7 @@ public static void main(String[] args){ } } + + +// This article is Contributed by Himanshu Jha. +// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/ \ No newline at end of file diff --git a/Programs/Recursion/permutation.java b/Programs/Recursion/permutation.java new file mode 100644 index 0000000..ec45b5c --- /dev/null +++ b/Programs/Recursion/permutation.java @@ -0,0 +1,36 @@ +import java.util.*; +public class permutation { + + public static void fun(List> res, List ds, boolean[] vis, int[] arr){ + if(ds.size() == arr.length){ + res.add(new ArrayList<>(ds)); + return; + } + + for(int i=0;i> 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/ \ No newline at end of file diff --git a/Programs/Recursion/permutationSet.java b/Programs/Recursion/permutationSet.java new file mode 100644 index 0000000..054e7b8 --- /dev/null +++ b/Programs/Recursion/permutationSet.java @@ -0,0 +1,41 @@ +import java.util.*; + +public class permutationSet { + + public static void fun(int[] arr, boolean[] vis, List ds, List> res){ + if(ds.size() == arr.length){ + res.add(new ArrayList<>(ds)); + return; + } + + for(int i=0;i> 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/ \ No newline at end of file diff --git a/Programs/Recursion/reverseArray.java b/Programs/Recursion/reverseArray.java index 118b873..2b3b6b9 100644 --- a/Programs/Recursion/reverseArray.java +++ b/Programs/Recursion/reverseArray.java @@ -34,3 +34,7 @@ public static void main(String[] args){ } } + + +// This article is Contributed by Himanshu Jha. +// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/ \ No newline at end of file diff --git a/Programs/Recursion/sumK.java b/Programs/Recursion/sumK.java new file mode 100644 index 0000000..c0df485 --- /dev/null +++ b/Programs/Recursion/sumK.java @@ -0,0 +1,66 @@ +// find subset 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 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 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/ \ No newline at end of file From 14038849c924eee548bc70c3f42b50e97104feef Mon Sep 17 00:00:00 2001 From: HIMANSHU-1504 Date: Wed, 10 Jan 2024 16:52:25 +0530 Subject: [PATCH 7/7] added some programs in recursion --- Programs/Recursion/sumK.java | 2 +- README.md | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Programs/Recursion/sumK.java b/Programs/Recursion/sumK.java index c0df485..720b8fa 100644 --- a/Programs/Recursion/sumK.java +++ b/Programs/Recursion/sumK.java @@ -1,4 +1,4 @@ -// find subset whose sum is k, there is no infinte supplies +// find subsequences whose sum is k, there is no infinte supplies //same as combination sum 1 but with some modification import java.util.*; diff --git a/README.md b/README.md index cb3d676..95ab531 100644 --- a/README.md +++ b/README.md @@ -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!!!