-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ca5ee7
commit e7a2c2d
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(|sort|) | ||
Space complexity - O(|sort|) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |