CodeForces - 1669B Triple

Given an array aa of nn elements, print any value that appears at least three times or print -1 if there is no such value.

Input
The first line contains an integer tt (1 \leq t \leq 10^41≤t≤10
4
) — the number of test cases.

The first line of each test case contains an integer nn (1 \leq n \leq 2\cdot10^51≤n≤2⋅10
5
) — the length of the array.

The second line of each test case contains nn integers a_1, a_2, \dots, a_na
1

,a
2

,…,a
n

(1 \leq a_i \leq n1≤a
i

≤n) — the elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 2\cdot10^52⋅10
5
.

Output
For each test case, print any value that appears at least three times or print -1 if there is no such value.

Sample 1
Inputcopy Outputcopy
7
1
1
3
2 2 2
7
2 2 3 3 4 2 2
8
1 4 3 4 3 2 4 1
9
1 1 1 2 2 2 3 3 3
5
1 5 2 4 3
4
4 4 4 4
-1
2
2
4
3
-1
4
Note
In the first test case there is just a single element, so it can’t occur at least three times and the answer is -1.

In the second test case, all three elements of the array are equal to 22, so 22 occurs three times, and so the answer is 22.

For the third test case, 22 occurs four times, so the answer is 22.

For the fourth test case, 44 occurs three times, so the answer is 44.

For the fifth test case, 11, 22 and 33 all occur at least three times, so they are all valid outputs.

For the sixth test case, all elements are distinct, so none of them occurs at least three times and the answer is -1.

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 2 * 1e5+20;
int main(void) {
	int a[N];
	int n;
	cin >> n;
	int m;
	for (int i = 0; i < n; i++) {
		int sum = 1;
		int flag = 0;
		cin >> m;
		for (int j = 0; j < m; j++) {
			cin >> a[j];
		}
		sort(a, a + m);
		for (int j = 1; j < m; j++) {
			if (a[j] == a[j - 1]) {
				sum++;
			}
			else {
				sum = 1;
			}
			if (sum >= 3) {
				cout << a[j] << endl;
				flag = 1;
				break;
			}
		}
		if (flag == 0) {
			cout << -1 << endl;
		}
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值