From 62e1f9f0e17745360bef8dabcad4817e11dc399c Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:31:09 +0530 Subject: [PATCH 01/10] Create permutations.cpp --- Array_Functions/Permutations/permutations.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Array_Functions/Permutations/permutations.cpp diff --git a/Array_Functions/Permutations/permutations.cpp b/Array_Functions/Permutations/permutations.cpp new file mode 100644 index 0000000..1e5dc96 --- /dev/null +++ b/Array_Functions/Permutations/permutations.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +using namespace std; + +template +vector> generatePermutations(vector arr) { + vector> permutations; + sort(arr.begin(), arr.end()); + permutations.push_back(arr); + while (true) { + int i = arr.size()-2; + while (i >= 0 && arr[i] >= arr[i + 1]) { + i--; + } + if (i < 0) break; + int j = arr.size()-1; + while (arr[j] <= arr[i]) { + j--; + } + swap(arr[i], arr[j]); + reverse(arr.begin() + i + 1, arr.end()); + permutations.push_back(arr); + } + return permutations; +} + +int main() { + vector arr = {1, 3, 2}; + vector> permutations = generatePermutations(arr); + + for (const auto& perm : permutations) { + for (const auto& elem : perm) { + cout << elem << " "; + } + cout << endl; + } + + return 0; +} From 63358732be10e372b87811e643929c124a6dd9fe Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:37:00 +0530 Subject: [PATCH 02/10] Create permutations.py --- Array_Functions/Permutations/permutations.py | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Array_Functions/Permutations/permutations.py diff --git a/Array_Functions/Permutations/permutations.py b/Array_Functions/Permutations/permutations.py new file mode 100644 index 0000000..4118556 --- /dev/null +++ b/Array_Functions/Permutations/permutations.py @@ -0,0 +1,27 @@ +def generate_permutations(arr): + permutations = [] + arr.sort() + permutations.append(arr[:]) + + while True: + i = len(arr) - 2 + while i >= 0 and arr[i] >= arr[i + 1]: + i -= 1 + if i < 0: + break + j = len(arr) - 1 + while arr[j] <= arr[i]: + j -= 1 + + arr[i], arr[j] = arr[j], arr[i] + arr = arr[:i + 1] + arr[i + 1:][::-1] + permutations.append(arr[:]) + + return permutations + +if __name__ == "__main__": + arr = ['a', 'b', 'c', 'd'] + permutations = generate_permutations(arr) + + for perm in permutations: + print(" ".join(map(str, perm))) From 2b24a1d5265dff13c5a7d76ed9c4fbbf1e953e9e Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:39:19 +0530 Subject: [PATCH 03/10] Create permutations.java --- .../Permutations/permutations.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Array_Functions/Permutations/permutations.java diff --git a/Array_Functions/Permutations/permutations.java b/Array_Functions/Permutations/permutations.java new file mode 100644 index 0000000..a908c14 --- /dev/null +++ b/Array_Functions/Permutations/permutations.java @@ -0,0 +1,39 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class Main { + public static > List> generatePermutations(List arr) { + List> permutations = new ArrayList<>(); + Collections.sort(arr); + permutations.add(new ArrayList<>(arr)); + while (true) { + int i = arr.size() - 2; + while (i >= 0 && arr.get(i).compareTo(arr.get(i + 1)) >= 0) { + i--; + } + if (i < 0) break; + int j = arr.size() - 1; + while (arr.get(j).compareTo(arr.get(i)) <= 0) { + j--; + } + Collections.swap(arr, i, j); + Collections.reverse(arr.subList(i + 1, arr.size())); + permutations.add(new ArrayList<>(arr)); + } + return permutations; + } + + public static void main(String[] args) { + List arr = Arrays.asList(1, 3, 2); + List> permutations = generatePermutations(arr); + + for (List perm : permutations) { + for (Integer elem : perm) { + System.out.print(elem + " "); + } + System.out.println(); + } + } +} From e4aa4bfe6fbc25c997c297f6d03a182f35350939 Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:40:09 +0530 Subject: [PATCH 04/10] Update RoadMap.md --- RoadMap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RoadMap.md b/RoadMap.md index ebf78d2..53577fd 100644 --- a/RoadMap.md +++ b/RoadMap.md @@ -15,7 +15,7 @@ These are fundamental algorithms and functions often used in competitive program - [x] [Binary Search](Array_Functions/Binary_Search) - [x] [Upper bound](Array_Functions/Upper_Bound) - [x] [Lower bound (Binary Search)](Array_Functions/Lower_Bound_Binary_Search) -- [ ] Generate all Permutation (n!) +- [x] [Generate all Permutation (n!)](Array_Functions/Permutations) - [x] [Count occurrences of all elements](Array_Functions/Count_Occurence) ### 2. String Algorithms From 751e26875628eec909ae131bbb8bc7bcf43ab890 Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 22:09:43 +0530 Subject: [PATCH 05/10] Rename permutations.cpp to permutations_iterative.cpp --- .../Permutations/{permutations.cpp => permutations_iterative.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Array_Functions/Permutations/{permutations.cpp => permutations_iterative.cpp} (100%) diff --git a/Array_Functions/Permutations/permutations.cpp b/Array_Functions/Permutations/permutations_iterative.cpp similarity index 100% rename from Array_Functions/Permutations/permutations.cpp rename to Array_Functions/Permutations/permutations_iterative.cpp From 328556b8a983d0a6e99709e9dd9f2806644c26d3 Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 22:10:08 +0530 Subject: [PATCH 06/10] Rename permutations.java to permutations_iterative.java --- .../{permutations.java => permutations_iterative.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Array_Functions/Permutations/{permutations.java => permutations_iterative.java} (100%) diff --git a/Array_Functions/Permutations/permutations.java b/Array_Functions/Permutations/permutations_iterative.java similarity index 100% rename from Array_Functions/Permutations/permutations.java rename to Array_Functions/Permutations/permutations_iterative.java From 2b879e341ef6336f3c1038cc0c8a74adfe630366 Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 22:11:07 +0530 Subject: [PATCH 07/10] Rename permutations.py to permutations_iterative.py --- .../Permutations/{permutations.py => permutations_iterative.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Array_Functions/Permutations/{permutations.py => permutations_iterative.py} (100%) diff --git a/Array_Functions/Permutations/permutations.py b/Array_Functions/Permutations/permutations_iterative.py similarity index 100% rename from Array_Functions/Permutations/permutations.py rename to Array_Functions/Permutations/permutations_iterative.py From fb53f40ef60d84258a4d7ef3e7f00b4db80502d5 Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 22:17:27 +0530 Subject: [PATCH 08/10] Create permutations_recursive.cpp --- .../Permutations/permutations_recursive.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Array_Functions/Permutations/permutations_recursive.cpp diff --git a/Array_Functions/Permutations/permutations_recursive.cpp b/Array_Functions/Permutations/permutations_recursive.cpp new file mode 100644 index 0000000..a21d046 --- /dev/null +++ b/Array_Functions/Permutations/permutations_recursive.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +template +void generatePermutations(vector& arr, int l, int r, vector>& result) { + if (l == r) { + result.push_back(arr); + } else { + for (int i = l; i <= r; i++) { + swap(arr[l], arr[i]); + generatePermutations(arr, l + 1, r, result); + swap(arr[l], arr[i]); + } + } +} + +template +vector> getPermutations(vector arr) { + vector> result; + generatePermutations(arr, 0, arr.size() - 1, result); + return result; +} + +int main() { + vector arr = {1, 2, 4, 3}; + vector> permutations = getPermutations(arr); + + for (const auto& perm : permutations) { + for (const auto& elem : perm) { + cout << elem << " "; + } + cout << endl; + } + + return 0; +} From 875cb6594e267a00d444f2404ccef8a7d04b4b7b Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 22:18:24 +0530 Subject: [PATCH 09/10] Create permutations_recursive.java --- .../Permutations/permutations_recursive.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Array_Functions/Permutations/permutations_recursive.java diff --git a/Array_Functions/Permutations/permutations_recursive.java b/Array_Functions/Permutations/permutations_recursive.java new file mode 100644 index 0000000..868e328 --- /dev/null +++ b/Array_Functions/Permutations/permutations_recursive.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.List; + +public class Permutations { + + private static void generatePermutations(List arr, int l, int r, List> result) { + if (l == r) { + result.add(new ArrayList<>(arr)); + } else { + for (int i = l; i <= r; i++) { + swap(arr, l, i); + generatePermutations(arr, l + 1, r, result); + swap(arr, l, i); + } + } + } + + private static List> getPermutations(List arr) { + List> result = new ArrayList<>(); + generatePermutations(arr, 0, arr.size() - 1, result); + return result; + } + + private static void swap(List arr, int i, int j) { + T temp = arr.get(i); + arr.set(i, arr.get(j)); + arr.set(j, temp); + } + + public static void main(String[] args) { + List arr = new ArrayList<>(); + arr.add(1); + arr.add(2); + arr.add(4); + arr.add(3); + List> permutations = getPermutations(arr); + + for (List perm : permutations) { + for (Integer elem : perm) { + System.out.print(elem + " "); + } + System.out.println(); + } + } +} + From 8aeaaa84639c1fdbec669f01edde2a36da14340a Mon Sep 17 00:00:00 2001 From: Subhoshri Pal <138369802+Subhoshri@users.noreply.github.com> Date: Wed, 15 Jan 2025 22:20:21 +0530 Subject: [PATCH 10/10] Create permutations_recursive.py --- .../Permutations/permutations_recursive.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Array_Functions/Permutations/permutations_recursive.py diff --git a/Array_Functions/Permutations/permutations_recursive.py b/Array_Functions/Permutations/permutations_recursive.py new file mode 100644 index 0000000..d9c591c --- /dev/null +++ b/Array_Functions/Permutations/permutations_recursive.py @@ -0,0 +1,20 @@ +def generate_permutations(arr, l, r, result): + if l == r: + result.append(arr[:]) + else: + for i in range(l, r + 1): + arr[l], arr[i] = arr[i], arr[l] + generate_permutations(arr, l + 1, r, result) + arr[l], arr[i] = arr[i], arr[l] + +def get_permutations(arr): + result = [] + generate_permutations(arr, 0, len(arr) - 1, result) + return result + +if __name__ == "__main__": + arr = ['a', 'b', 'c'] + permutations = get_permutations(arr) + + for perm in permutations: + print(" ".join(map(str, perm)))