Skip to content

Commit

Permalink
Added for 17 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 17, 2024
1 parent 9f090f1 commit 05cc131
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
82 changes: 82 additions & 0 deletions GeeksForGeeks/17-1-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<Integer> arr = new ArrayList<>();

for(int i=0; i<n; i++)
arr.add(Integer.parseInt(S[i]));

Solution ob = new Solution();
ArrayList<ArrayList<Integer>> res = ob.uniquePerms(arr,n);
for(int i=0; i<res.size(); i++)
{
for(int j=0; j<n; j++)
{
System.out.print(res.get(i).get(j) + " ");
}
System.out.println();
}
}
}
}
// } Driver Code Ends


//User function Template for Java

class Solution
{
static Set<ArrayList<Integer>> ans;

static void helper(int idx, ArrayList<Integer> arr)
{
int n = arr.size();
if (idx >= n)
{
ArrayList<Integer> 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<ArrayList<Integer>> uniquePerms(ArrayList<Integer> arr, int n)
{
ans = new HashSet<>();
Collections.sort(arr);
helper(0, arr);

ArrayList<ArrayList<Integer>> 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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/17-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * n!)
Space complexity - O(n * n!)
2 changes: 2 additions & 0 deletions LeetCode/17-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
17 changes: 17 additions & 0 deletions LeetCode/17-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution
{
public boolean uniqueOccurrences(int[] arr)
{
Map<Integer, Integer> count = new HashMap<>();
Set<Integer> 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;
}
}

0 comments on commit 05cc131

Please sign in to comment.