View Code of Problem 133

    #include<stdio.h>
     
    int main()
    {
      int t,n,x,i,j,temp;
      int a[100000];
      scanf("%d",&t);
      while(t--)
      {
        int flag=0;
        scanf("%d %d",&n,&x);
        for(i=0;i<n;i++)
          scanf("%d",&a[i]);
        
        for(i=0;i<n-1;i++)
          for(j=0;j<n-i-1;j++)
          {
            if(a[j]>a[j+1])
            {
              temp=a[j];
              a[j]=a[j+1];
              a[j+1]=temp;
            }
          }
          
        i=0;
        j=n-1;
        while(i!=j)
        {
          if(a[i]+a[j]==x)
          {
            printf("YES\n");
            break;
          }
          else if(a[i]+a[j]<x)
            i++;
          else if(a[i]+a[j]>x)
            j--;
        }
        if(i==j)
          printf("NO\n");
      }
      return 0;
    }

Double click to view unformatted code.


Back to problem 133