Watson gives to Sherlock a bag of numbers [1, 2, 3 ... N] and then he removes K numbers…
- User
Watson gives to Sherlock a bag of numbers [1, 2, 3 ... N] and then he removes K numbers A1, A2 ... AK from the bag. He now asks Sherlock to find the P'th smallest number in the bag. Input First line contains T, the number of test cases. Each test case consists of N, K and P followed by K integers in next line denoting the array A.
Output For each test case, print P'th smallest number in the bag. If no such number exists output -1.
Constraints 1 ≤ T ≤ 10 20% testdata: 1 ≤ N ≤ 10^3 20% testdata: 1 ≤ N ≤ 10^5 60% testdata: 1 ≤ N ≤ 10^9 0 ≤ K ≤ min(N, 10^5) 1 ≤ P ≤ N
Note: Large input files. Use scanf instead of cin.
SAMPLE INPUT 2 4 1 2 1 5 2 4 1 3
SAMPLE OUTPUT 3 -1
Explanation
Test case 1: Remaining numbers are [2, 3, 4]. 3 is the 2nd smallest remaining numbers.
Test case 2: Remaining numbers are [2, 4, 5]. 4th smallest remaining number doesn't exist.
Assistant
pythonimport sys def main(): input = sys.stdin.read().split() ptr = 0 T = int(input[ptr]) ptr += 1 for _ in range(T): N = int(input[ptr]) K = int(input[ptr+1]) P = int(input[ptr+2]) ptr += 3 A = [] if K > 0: A = list(map(int, input[ptr:ptr+K])) ptr += K if P > N - K: print(-1) continue if K == 0: print(P) continue A.sort() B = [0] + A + [N + 1] accumulated = 0 found = False for i in range(1, len(B)): prev = B[i-1] curr = B[i] start = prev + 1 end = curr - 1 if start > end: continue cnt = end - start + 1 if accumulated + cnt >= P: offset = P - accumulated - 1 print(start + offset) found = True break accumulated += cnt if not found: print(-1) if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →
DeepSeek-R1Compare side by side →