We are given a problem: "Sam is dangerous boy. He like to fight with everyone who is more powerful than him. One day, Sam enters in the meeting of all gangsters of the city. He wants to fight with everyone who is more powerful than him.
But one man know his motive and tell to all gangsters about Sam. But gangsters don't know whether the Sam is present there or not and from how many gangsters Sam will fight. Help gangsters and tell them that Sam is present there or not. If Sam is present, then how many gangsters will fight with Sam.
INPUT: The first line denotes the number of test cases T. For each line, a number N which denotes the total number of gangsters in the meeting including Sam and P which denotes the power of Sam. Next line, there are N space separated numbers denoting the power of each person.
OUTPUT: You have to tell whether Sam is present in the meeting or not in terms of YES or NO. If Sam is there, then print the total number of gangsters who will fight with Sam.
Constraints: 1 ≤ T ≤ 100; 1 ≤ N ≤ 10000; 0 ≤ Power ≤ 1000000
SAMPLE INPUT:
1
6 5
1 3 7 2 6 8
SAMPLE OUTPUT:
NO 0
Interpretation: For each test case, we are given N (total number of people) and P (Sam's power). Then we have N numbers: the powers of each person present, which includes possibly Sam? The problem says: "Sam enters in the meeting of all gangsters of the city." So Sam enters the meeting, so he is present. But then the gangsters don't know whether Sam is present or not, but we have to determine. Wait: The input gives N which denotes total number of gangsters in the meeting including Sam, and P which is Sam's power. And the next line has N numbers: powers of each person. So presumably, among those N numbers, one of them is Sam's power (which is P). But the gangsters don't know if Sam is actually among them? Actually, they know that Sam's power is P, but they don't know if he is among them. They have a list of all the powers of people present. So we have to check if there is any person with power exactly P. If yes, then Sam is present (YES), and then we need to count the number of gangsters who will fight with Sam. According to the description: Sam fights with everyone who is more powerful than him. So if Sam is present, then the number of fights = number of people (excluding Sam?) with power > P. But careful: "Sam wants to fight with everyone who is more powerful than him." That includes other gangsters. But if there are multiple people with power equal to Sam, does he fight with them? The problem says "more powerful", so only those > P. Also, if there are multiple people with same power as Sam, they are not more powerful. So we count those > P.
If Sam is not present (i.e., no person with power exactly P), then output "NO 0"? The sample output shows "NO 0". For N=6, P=5, powers: 1 3 7 2 6 8. There is no 5, so Sam is not present, and output "NO 0". So the 0 is the number of fights? But Sam is not present, so maybe output 0 fights? Or maybe it's like they ask: "tell them that Sam is present there or not. If Sam is present, then print the total number of gangsters who will fight with Sam." So if Sam is not present, we only need to say NO; maybe the 0 is just for formatting? But sample output has "NO 0". So likely the output format is: if present, "YES X", else "NO 0". That is, even when not present, we still output NO followed by 0 (maybe to indicate no fights). But check problem statement: "You have to tell whether Sam is present in the meeting or not in terms of YES or NO. If Sam is there, then print the total number of gangsters who will fight with Sam." It doesn't specify what to print if NO. But sample prints "NO 0". So we should output NO and then number of gangsters who would fight if he were present? But 0 makes sense: no fights because he's not there. So just output NO 0.
But careful: Could there be a case where Sam is present and there are 0 fights? Then output "YES 0". That should be valid.
So algorithm: For each test case, read N and P. Then read N integers: powers list. Check if P exists in list. Could be multiple persons with same power? Possibly. But Sam is a person; if there are multiple with same power, then maybe Sam is one of them. We just need to know if at least one person has power P. Then count number of values > P. That count is the number of fights.
But note: The N includes Sam, so if Sam is present, there is exactly one Sam? Or could there be multiple people with power P, but only one is Sam? The problem doesn't specify, but we can assume Sam is a unique person. The input says N includes Sam; but if there are multiple entries with power P, would that mean Sam is one of them? Probably we should treat that Sam is one of the N persons and his power is P, but the list of powers includes his power. So there must be exactly one occurrence of P if Sam is present. But could there be duplicates? Possibly, because there could be other gangsters with same power as Sam? But they are not Sam. However, the problem states: "the power of each person". So it's possible that two gangsters have the same power. In that case, Sam's power is P, so if there is at least one person with power P, Sam might be among them. But if there are multiple with power P, we don't know which is Sam. But we just need to determine if Sam is present. If there is at least one power equal to P, then Sam is present? But maybe Sam's power is unique to him? Not necessarily. But the problem says: "P which denotes the power of Sam." So if there is a person with that power, we can infer Sam is present. But if there are multiple, still Sam is present. So we can just check existence.
But careful: The input description: "The first line denotes the number of test cases T. For each line, a number N which denotes the total number of gangsters in the meeting including Sam and P which denotes the power of Sam. Next line, there are N space separated numbers denoting the power of each person." This implies that the list includes Sam's power. So at least one of those numbers equals P. But they might be asking to verify if Sam is actually there? Possibly there is a twist: Maybe Sam sometimes doesn't attend? The story says: "Sam enters in the meeting of all gangsters". So he always enters. But then the gangsters don't know if he is present. Actually, the story is contradictory: Sam enters, but the gangsters don't know if he's there. The problem statement might be designed so that we must check if his power appears in the list. So essentially, for each test case, we need to check if P is in the list. If yes, output YES and count of > P. If no, output NO 0.
So algorithm is straightforward.
Constraints: T up to 100, N up to 10000, power values up to 1e6. So we can just read and count.
Edge Cases:
- If N=1 and power is P, then Sam is present, and number of > P is 0 => output YES 0.
- If N=1 and power not P, then output NO 0.
- If there are duplicates, count > P among all N values regardless of duplicates, including other gangsters with same power? But fights only with those more powerful, so duplicates of same power as Sam (if any) are not counted. If there are other gangsters with same power, they are not >, so fine.
But careful: If Sam is present, we should count the number of gangsters who will fight with Sam. That includes only those more powerful than him. But if Sam is present, then we have N gangsters total, including Sam. So fights = count of values > P. Note that this count may include possibly some individuals with power > P. It doesn't matter if there are multiple Sam-like power people; they are not >.
But what if there are multiple individuals with power exactly P? Then Sam fights with none of them, but they are not >. So count only > P.
So code: For each test case:
- read N and P.
- read N integers into list or just process as we read.
- initialize flags: found = False, count = 0.
- For each value a:
- if a == P: found = True
- if a > P: count += 1
- If found: print "YES", count
- else: print "NO 0" (or "NO 0"? According to sample output: "NO 0". It's a space separated output. So format: "NO 0". So we can print "NO",0 but with space: print("NO 0") if using Python f-string.
But note: The sample output: "NO 0". So it's "NO" space "0". Yes.
Double-check: For YES case: Should be "YES X". Probably same format: "YES 3" etc.
Now check if any tricky part: The problem says "total number of gangsters who will fight with Sam". That is the number of gangsters who are more powerful than Sam. But if Sam is not present, the problem says "tell them that Sam is present there or not." It doesn't explicitly say to output any number then. But sample includes 0. So we follow sample.
Alternatively, perhaps the output should be "NO" alone? But sample includes 0. So include 0.
Potential pitfalls: Input format: "For each line, a number N which denotes the total number of gangsters in the meeting including Sam and P". That means each test case has two lines: first line contains N and P, second line contains N numbers. T is first line. So we need to read T, then loop T times.
Edge: Could be extra spaces, newlines. Use typical input reading.
Time complexity: O(TN) up to 10010000 = 1e6, fine.
Now, let's produce final solution code in Python.
Implementation:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
p = int(next(it))
found = False
cnt = 0
for i in range(n):
a = int(next(it))
if a == p:
found = True
if a > p:
cnt += 1
if found:
out_lines.append(f"YES {cnt}")
else:
out_lines.append(f"NO 0")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()
That's it.
But we should also consider alternative interpretation: Maybe the count of fights includes only those gangsters that are more powerful than Sam, but only if Sam is present; if not present, maybe we should output the number of gangsters that are more powerful than Sam anyway? But sample says 0 when not present. So they set count to 0. It could be that they always output a number after YES/NO. So our approach is fine.
Now, just to be thorough: Could there be a scenario where Sam's power appears multiple times? Then found is True, we count >P. That includes all >P, which could be more than N-1 if there are duplicates of P? But Sam is only one person; but the list includes Sam plus others. If there are other persons with same power as Sam, they are not Sam, but they are in the meeting. Does Sam fight with them? No, because they are not more powerful. So count >P still correct.
One nuance: Should we exclude Sam himself from the count of >P? Since we check all numbers, and if Sam's power equals P, we don't count it because it's not >P. So it's fine.
Thus solution is correct.
But wait: The problem says "Sam is dangerous boy. He like to fight with everyone who is more powerful than him." So if there are gangsters with exactly same power, he doesn't fight them. So our count is correct.
Now, the final answer: Provide explanation and code.
Potential alternative: Could be that the input includes Sam's power separately, and the list of N powers includes Sam's power as well? Yes.
Thus produce final output.
But there is something: The sample input:
1
6 5
1 3 7 2 6 8
We output NO 0. So that matches.
If we test a case: T=1, N=4, P=3, powers: 3 5 2 7 -> found True, count >3 = 2 (5 and 7) -> output YES 2.
If multiple P: N=5, P=3, powers: 3 3 5 2 7 -> found True, count >3 = 2 (5,7) -> YES 2.
All good.
Now, is there any hidden trick? Possibly the problem expects that if Sam is present, we should output the number of gangsters he will fight, but maybe the number of gangsters includes those who are more powerful than him excluding himself, but if there are other people with same power, they are not counted. Already considered.
One more nuance: The problem says "total number of gangsters in the meeting including Sam". So the N numbers represent all gangsters. So Sam's power is among them. So if there is no value equal to P, it's a contradiction? But the gangsters don't know if Sam is present, so input could be such that Sam is not present. So okay.
Now, produce final answer.
Given the style, I'll provide solution in Python.
Also, I can implement faster input reading for large N.
Thus final solution.
But I think the answer expects simple code.
Now, write explanation:
- Read T.
- For each test case:
- Read N, P.
- Read N integers.
- Check if any integer equals P.
- Count integers > P.
- If found: print YES and count.
- Else: print NO and 0.
Complexities: O(T*N) time, O(1) extra.
That's it. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
p = int(next(it))
found = False
cnt = 0
for _ in range(n):
x = int(next(it))
if x == p:
found = True
if x > p:
cnt += 1
if found:
out_lines.append(f"YES {cnt}")
else:
out_lines.append(f"NO 0")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()