Skip to content

Commit

Permalink
Added codes for 21 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 21, 2024
1 parent 9ca5ee7 commit e7a2c2d
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
108 changes: 108 additions & 0 deletions GeeksForGeeks/April/21-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//{ 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());
int array[] = new int[N];
Map<Integer, Integer> mp = new HashMap<>();

String input_line[] = read.readLine().trim().split(" ");
for(int i=0;i<N;i++)
{
array[i] = Integer.parseInt(input_line[i]);
if (mp.containsKey(array[i]))
{
mp.put(array[i], mp.get(array[i]) + 1);
}
else
{
mp.put(array[i], 1);
}
}

input_line = read.readLine().trim().split(" ");
int a = Integer.parseInt(input_line[0]);
int b = Integer.parseInt(input_line[1]);

int original[] = new int[N];

for(int i=0; i<N;i++)
{
original[i] = array[i];
}

int k1=0,k2=0,k3=0;
int kk1=0;int kk2=0;int kk3=0;

for(int i=0;i<N;i++)
{
if(original[i]>b)
k3++;
else if(original[i]<=b && original[i]>=a)
k2++;
else if(original[i]<b)
k1++;
}
Solution ob = new Solution();
ob.threeWayPartition(array,a,b);

for(int i=0;i<k1;i++)
{
if(array[i]<b)
kk1++;
}

for(int i=k1;i<k1+k2;i++)
{
if(array[i]<=b && array[i]>=a)
kk2++;

}

for(int i=k1+k2;i<k1+k2+k3;i++)
{
if(array[i]>b)
kk3++;
}
Boolean ok = false;
if(k1==kk1 && k2 ==kk2 && k3 == kk3)
ok = true;

for(int i=0;i<array.length;i++)
mp.put(array[i], mp.get(array[i]) - 1);

for(int i=0;i<array.length;i++)
if(mp.get(array[i])!=0)
ok=false;

if(ok)
System.out.println(1);
else
System.out.println(0);
}
}
}

// } Driver Code Ends


//User function Template for Java

class Solution{
//Function to partition the array around the range such
//that array is divided into three parts.
public void threeWayPartition(int array[], int a, int b)
{
// code here
Arrays.sort(array);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/21-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(|sort|)
Space complexity - O(|sort|)
2 changes: 2 additions & 0 deletions LeetCode/April/21-4-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)
52 changes: 52 additions & 0 deletions LeetCode/April/21-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class UnionFind
{
public UnionFind(int n)
{
id = new int[n];
rank = new int[n];
for (int i = 0; i < n; ++i)
id[i] = i;
}

public void unionByRank(int u, int v)
{
int i = find(u);
int j = find(v);
if (i == j)
return;
if (rank[i] < rank[j])
id[i] = j;
else if (rank[i] > rank[j])
id[j] = i;
else
{
id[i] = j;
++rank[j];
}
}

public int find(int u)
{
return id[u] == u ? u : (id[u] = find(id[u]));
}

private int[] id;
private int[] rank;
}

class Solution
{
public boolean validPath(int n, int[][] edges, int source, int destination)
{
UnionFind uf = new UnionFind(n);

for (int[] edge : edges)
{
int u = edge[0];
int v = edge[1];
uf.unionByRank(u, v);
}

return uf.find(source) == uf.find(destination);
}
}

0 comments on commit e7a2c2d

Please sign in to comment.