From 05cc131cde0f8e266b955c986afc5cfd233fb392 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Wed, 17 Jan 2024 11:29:36 +0530 Subject: [PATCH] Added for 17 Jan --- GeeksForGeeks/17-1-24/GFG.java | 82 +++++++++++++++++++++++++++++++++ GeeksForGeeks/17-1-24/README.md | 2 + LeetCode/17-1-24/README.md | 2 + LeetCode/17-1-24/Solution.java | 17 +++++++ 4 files changed, 103 insertions(+) create mode 100644 GeeksForGeeks/17-1-24/GFG.java create mode 100644 GeeksForGeeks/17-1-24/README.md create mode 100644 LeetCode/17-1-24/README.md create mode 100644 LeetCode/17-1-24/Solution.java diff --git a/GeeksForGeeks/17-1-24/GFG.java b/GeeksForGeeks/17-1-24/GFG.java new file mode 100644 index 0000000..9d92761 --- /dev/null +++ b/GeeksForGeeks/17-1-24/GFG.java @@ -0,0 +1,82 @@ +//{ Driver Code Starts +//Initial Template for JAVA + +import java.io.*; +import java.util.*; + +class GFG { + public static void main(String args[]) throws IOException { + BufferedReader read = + new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(read.readLine()); + while (t-- > 0) { + int n = Integer.parseInt(read.readLine()); + + String S[] = read.readLine().split(" "); + + ArrayList arr = new ArrayList<>(); + + for(int i=0; i> res = ob.uniquePerms(arr,n); + for(int i=0; i> ans; + + static void helper(int idx, ArrayList arr) + { + int n = arr.size(); + if (idx >= n) + { + ArrayList temp = new ArrayList<>(arr); + ans.add(temp); + return; + } + + for (int i = idx; i < n; i++) + { + Collections.swap(arr, i, idx); + helper(idx + 1, arr); + Collections.swap(arr, i, idx); + } + } + + static ArrayList> uniquePerms(ArrayList arr, int n) + { + ans = new HashSet<>(); + Collections.sort(arr); + helper(0, arr); + + ArrayList> result = new ArrayList<>(ans); + + Collections.sort(result, (a, b) -> { + for (int i = 0; i < n; i++) + { + if (!a.get(i).equals(b.get(i))) + { + return a.get(i) - b.get(i); + } + } + return 0; }); + + return result; + } +} diff --git a/GeeksForGeeks/17-1-24/README.md b/GeeksForGeeks/17-1-24/README.md new file mode 100644 index 0000000..0b71f35 --- /dev/null +++ b/GeeksForGeeks/17-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n * n!) +Space complexity - O(n * n!) diff --git a/LeetCode/17-1-24/README.md b/LeetCode/17-1-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/LeetCode/17-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/17-1-24/Solution.java b/LeetCode/17-1-24/Solution.java new file mode 100644 index 0000000..3af78bf --- /dev/null +++ b/LeetCode/17-1-24/Solution.java @@ -0,0 +1,17 @@ +class Solution +{ + public boolean uniqueOccurrences(int[] arr) + { + Map count = new HashMap<>(); + Set occurrences = new HashSet<>(); + + for (final int a : arr) + count.merge(a, 1, Integer::sum); + + for (final int value : count.values()) + if (!occurrences.add(value)) + return false; + + return true; + } +}