Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package codechef; // don't place package name! */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- /* Name of the class has to be "Main" only if the class is public. */
- public final class Codechef
- {
- public static int sol(int s,int n,HashSet<Integer> hs){
- int ans=0;
- if(!hs.contains(s))
- return ans;
- int p1=s,p2=s;
- while(p1>0 && p2<=n && ans<1001){
- if(!hs.contains(p1)){
- return ans;
- }
- p1--;
- if(!hs.contains(p2)){
- return ans;
- }
- p2++;
- ans++;
- }
- while(p1>0 && ans<1001){
- if(!hs.contains(p1)){
- return ans;
- }
- p1--;
- ans++;
- }
- while(p2<=n && ans<1001){
- if(!hs.contains(p2)){
- return ans;
- }
- p2++;
- ans++;
- }
- return ans;
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc=new Scanner(System.in);
- int t=sc.nextInt();
- while(t>0){
- int n=sc.nextInt();
- int s=sc.nextInt();
- int k=sc.nextInt();
- HashSet<Integer> hs=new HashSet<>();
- while(k>0){
- hs.add(sc.nextInt());
- k--;
- }
- int ans=sol(s,n,hs);
- System.out.println(ans);
- t--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement