Replace For X CodeChef Solution

Replace For X CodeChef Solution

In this post, the Replace For X CodeChef Solution is given. Question details : October Challenge 2020 Division 2 > Replace For X.

Replace For X CodeChef Solution

#include<bits/stdc++.h>
using namespace std;
const int MOD=1000000007;
#define ll long long
#define Fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
int main()
{
Fastio;
ll t; 
cin>>t;
while(t--)
{
ll n,x,p,k;
cin>>n>>x>>p>>k;
ll a[n];
for(ll j=0;j<n;j++)
cin>>a[j];
sort(a,a+n);
if(a[p-1]==x)
cout<<0<<endl;
if(p>=k)
{
if(a[p-1]<x)
{
ll c=0;
for(ll i=p;i<=n;i++)
{
if(a[i]<x)
c++;
}
cout<<c+1<<endl;
}
}
if(p<=k)
{
if(a[p-1]>x)
{
ll c=0;
for(ll i=0;i<p-1;i++)
{
if(a[i]>x)
c++;
}
cout<<c+1<<endl;
}
}
if(p<k && a[p-1]<x)
cout<<-1<<endl;
if(p>k && a[p-1]>x)
cout<<-1<<endl;
}
}


The solution of the above program is contributed by Vishal Vaibhab (IIT BHU). If you like TheCode11, then do follow us on Facebook, Twitter and Instagram.

Previous Post Next Post

Contact Form