-
Notifications
You must be signed in to change notification settings - Fork 0
/
21_three-way-partitioning.java
137 lines (115 loc) · 3.46 KB
/
21_three-way-partitioning.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 04-2024(April)/21_three-way-partitioning.java
/**
* Date : 21-Apr-24
* Repo : https://github.com/ankitsamaddar/daily-geeksforgeeks
*
* Problem : Three way partitioning
* Difficulty: 🟢Easy
*
* GeeksforGeeks : https://www.geeksforgeeks.org/problems/three-way-partitioning/1
*/
import java.io.*;
import java.util.*;
// User function Template for Java
class Solution {
// Function to partition the array around the range such
// that array is divided into three parts.
// 3-WAY PARTITIONING APPROACH
// Similar to dutch national-flag problem
public void threeWayPartition(int array[], int a, int b) {
int low = 0;
int mid = 0;
int high = array.length - 1;
while (mid <= high) {
// Condition 1
// arr[mid] < a, swap elements at index mid with low,and increment
if (array[mid] < a) {
int temp = array[low];
array[low] = array[mid];
array[mid] = temp;
low++;
mid++;
}
// Condition 2
// a <= array[mid] <= b, mid at right position, just increment it
else if (array[mid] >= a && array[mid] <= b)
mid++;
// Condition 3
// array[mid] > b, swap elements at index mid with high, decrement high
else if (array[mid] > b) {
int temp = array[mid];
array[mid] = array[high];
array[high] = temp;
high--;
}
}
}
}
//{ Driver Code Starts
// Initial Template for Java
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