-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathequal distinct
More file actions
163 lines (142 loc) · 3.97 KB
/
equal distinct
File metadata and controls
163 lines (142 loc) · 3.97 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Problem
Let F(S)F(S) denote the number of distinct elements in the array SS. For example, F([1, 2, 3, 2]) = 3, F([1, 2]) = 2.F([1,2,3,2])=3,F([1,2])=2.
You are given an array AA containing NN integers. Find if it is possible to divide the elements of the array AA into two arrays BB and CC such that :
Every element of the array AA belongs either to array BB or to array CC.
F(B) = F(C)F(B)=F(C).
Input Format
The first line of input will contain a single integer TT, denoting the number of test cases.
Each test case consists of two lines of input.
The first line of each test case contains an integer NN — the length of the array AA.
The next line contains NN space-separated integer A_1, A_2, \dots, A_NA
1
,A
2
,…,A
N
, denoting the elements of the array AA.
Output Format
For each test case, print YES if it is possible to divide the elements of the array AA into two arrays B, CB,C satisfying all the conditions and NO otherwise.
You may print each character of the string in either uppercase or lowercase (for example, the strings yEs, yes, Yes, and YES will all be treated as identical).
Constraints
1 \leq T \leq 10^41≤T≤10
4
2 \leq N \leq 10^52≤N≤10
5
1 \leq A_i \leq N1≤A
i
≤N
The sum of NN over all test cases won't exceed 2\cdot 10^52⋅10
5
.
Sample 1:
Input
Output
3
2
1 2
3
1 2 3
4
3 1 1 2
YES
NO
YES
Explanation:
Test case 11: One possible division is B = [1], C = [2]B=[1],C=[2]. Here F(B) = F(C) = 1.F(B)=F(C)=1.
Test case 22: There is no way to distribute the elements of the array A = [1, 2, 3]A=[1,2,3] satisfying all the conditions.
Test case 33: One possible division is B = [3, 1], C = [1, 2]B=[3,1],C=[1,2]. Here F(B) = F(C) = 2.F(B)=F(C)=2.
*/
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
void merge(int array[], int const left, int const mid,
int const right)
{
auto const subArrayOne = mid - left + 1;
auto const subArrayTwo = right - mid;
auto *leftArray = new int[subArrayOne],
*rightArray = new int[subArrayTwo];
for (auto i = 0; i < subArrayOne; i++)
leftArray[i] = array[left + i];
for (auto j = 0; j < subArrayTwo; j++)
rightArray[j] = array[mid + 1 + j];
auto indexOfSubArrayOne = 0,
indexOfSubArrayTwo = 0;
int indexOfMergedArray = left;
while (indexOfSubArrayOne < subArrayOne && indexOfSubArrayTwo < subArrayTwo)
{
if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo])
{
array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
}
else
{
array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
}
indexOfMergedArray++;
}
while (indexOfSubArrayOne < subArrayOne)
{
array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
indexOfMergedArray++;
}
while (indexOfSubArrayTwo < subArrayTwo)
{
array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
indexOfMergedArray++;
}
delete[] leftArray;
delete[] rightArray;
}
void mergeSort(int array[], int const begin, int const end)
{
if (begin >= end)
return;
auto mid = begin + (end - begin) / 2;
mergeSort(array, begin, mid);
mergeSort(array, mid + 1, end);
merge(array, begin, mid, end);
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int count = 0;
mergeSort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
{
if (arr[i] == arr[i + 1])
{
count++;
}
}
if (count == 0 && n%2 !=0)
{
cout << "NO" <<endl;
}
else
{
cout << "YES" << endl;
}
}
return 0;
}