Remove Smallest

简介: Remove Smallest

文章目录

一、Remove Smallest

总结


一、Remove Smallest

本题链接:Remove Smallest


题目:

A. Remove Smallest

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

You are given the array a consisting of n positive (greater than zero) integers.


In one move, you can choose two indices i and j (i≠j) such that the absolute difference between ai and aj is no more than one (|ai−aj|≤1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).


Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.


You have to answer t independent test cases.


Input

The first line of the input contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.


The first line of the test case contains one integer n (1≤n≤50) — the length of a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤100), where ai is the i-th element of a.


Output

For each test case, print the answer: “YES” if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or “NO” otherwise.


Example

input

5

3

1 2 2

4

5 5 5 5

3

1 2 4

4

1 3 4 4

1

100

output

YES

YES

NO

NO

YES

Note

In the first test case of the example, we can perform the following sequence of moves:


choose i=1 and j=3 and remove ai (so a becomes [2;2]);

choose i=1 and j=2 and remove aj (so a becomes [2]).

In the second test case of the example, we can choose any possible i and j any move and it doesn’t matter which element we remove.


In the third test case of the example, there is no way to get rid of 2 and 4.


本博客给出本题截图:

image.png

image.png

题意:给t组数字串,对于每组数字有两种操作:挑选两个绝对值相差1的数,并删除其中的小数;挑选两个值相同的数字并任意删除一个数

AC代码

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 60;
int a[N];
int main()
{
    int t;
    cin >> t;
    while (t -- )
    {
        int n;
        cin >> n;
        for (int i = 0; i < n; i ++ ) 
            cin >> a[i];
        if (n == 1)
        {
            puts("YES");
            continue;
        }
        sort(a, a + n);
        int cnt = 0;
        for (int i = 0; i < n - 1; i ++ ) 
            if (a[i + 1] - a[i] == 1 || a[i + 1] == a[i])
                cnt ++;
        if (cnt == n - 1) puts("YES");
        else puts("NO");
    }
    return 0;
}

总结

水题,不解释

目录
相关文章
LeetCode 203. Remove Linked List Elements
删除链表中等于给定值 val 的所有节点。
87 0
LeetCode 203. Remove Linked List Elements
|
索引
LeetCode 373. Find K Pairs with Smallest Sums
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。 找到和最小的 k 对数字 (u1,v1), (u2,v2) ... (uk,vk)。
172 0
LeetCode 373. Find K Pairs with Smallest Sums
|
存储 算法
LeetCode 328. Odd Even Linked List
给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。
108 0
LeetCode 328. Odd Even Linked List
|
算法
LeetCode Find Minimum in Rotated Sorted Array II
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 注意数组中可能存在重复的元素。
104 0
LeetCode Find Minimum in Rotated Sorted Array II
LeetCode 215. Kth Largest Element in an Array
在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
106 0
|
算法
[LeetCode]--203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 –&gt; 2 –&gt; 6 –&gt; 3 –&gt; 4 –&gt; 5 –&gt; 6, val = 6 Return: 1 –&gt; 2 –&gt; 3 –&gt; 4 –&gt;
1057 1
[LeetCode]--83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1-&gt;1-&gt;2, return 1-&gt;2. Given 1-&gt;1-&gt;2-&gt;3-&gt;3, return 1-&gt
994 0