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 Find Minimum in Rotated Sorted Array II
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 注意数组中可能存在重复的元素。
90 0
LeetCode Find Minimum in Rotated Sorted Array II
LeetCode 153. Find Minimum in Rotated Sorted Array
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 你可以假设数组中不存在重复元素。
112 0
LeetCode 153. Find Minimum in Rotated Sorted Array
|
索引
LeetCode 373. Find K Pairs with Smallest Sums
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。 找到和最小的 k 对数字 (u1,v1), (u2,v2) ... (uk,vk)。
166 0
LeetCode 373. Find K Pairs with Smallest Sums
|
算法
[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;
1051 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
985 0
|
算法
[LeetCode]--26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in p
977 0
|
Java
Merge Two Sorted Lists & Remove Nth Node From End of List
1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
872 0
|
C++ Python
[LeetCode] Find Minimum in Rotated Sorted Array
As explained in the Solution tag, the key to solving this problem is to use invariants. We set two pointers: l for the left and r for the right.
735 0