B. Subtract Operation

本文探讨了一种特殊的列表操作问题,即通过一系列操作使列表中的最后一个元素等于预设的目标值k。文章详细介绍了问题背景、输入输出格式及示例,并提供了一个算法实现思路。

B. Subtract Operation

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a list of nn integers. You can perform the following operation: you choose an element xx from the list, erase xx from the list, and subtract the value of xx from all the remaining elements. Thus, in one operation, the length of the list is decreased by exactly 11.

Given an integer kk (k>0k>0), find if there is some sequence of n−1n−1 operations such that, after applying the operations, the only remaining element of the list is equal to kk.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and kk (2≤n≤2⋅1052≤n≤2⋅105, 1≤k≤1091≤k≤109), the number of integers in the list, and the target value, respectively.

The second line of each test case contains the nn integers of the list a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109).

It is guaranteed that the sum of nn over all test cases is not greater that 2⋅1052⋅105.

Output

For each test case, print YES if you can achieve kk with a sequence of n−1n−1 operations. Otherwise, print NO.

You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer).

Example

input

Copy

4
4 5
4 2 2 7
5 4
1 9 1 3 4
2 17
17 0
2 17
18 18

output

Copy

YES
NO
YES
NO

Note

In the first example we have the list {4,2,2,7}{4,2,2,7}, and we have the target k=5k=5. One way to achieve it is the following: first we choose the third element, obtaining the list {2,0,5}{2,0,5}. Next we choose the first element, obtaining the list {−2,3}{−2,3}. Finally, we choose the first element, obtaining the list {5}{5}.

import sys
import math
#import numpy as np
from collections import defaultdict
#sys.stdin = open("in.txt", "r")
 
T = int(input())
for _ in range(T):
    n, l = [int(i) for i in input().split()]
    arr=[int(i) for i in input().split()]
    dict=defaultdict(int)
    for v in arr:
        dict[v]=1
    flag=True
    for v in arr:
        if (v-l) in dict or (v+l) in dict:
            print('YES')
            flag=False
            break
    
    if flag:
        print('NO')

只要存在差值为k的pair就可以,每次操作可以移除其他数

11-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值