REPLESX - Editorial

Yeah I got it now :innocent:

Thanks a lot for giving your time

It will be β€œX” in place of β€œk”.

I have done this without sorting just by finding the rank of x in array

It is passing all the test cases but when submitting it is failing

can anyone help?

var t = Convert.ToInt32(Console.ReadLine());

    while (t-- > 0)
    {
        var nxpk = Console.ReadLine().Split(' ').Select(xk => Convert.ToInt32(xk)).ToArray();

        var n = nxpk[0];

        var x = nxpk[1];

        var p = nxpk[2];

        var k = nxpk[3];

        var ar = Console.ReadLine().Split(' ').Select(xP => Convert.ToInt32(xP)).ToArray();

        var xRankL = 1;
        var xRankR = 1;
        var exists = false;
        for (int i = 0; i < n; i++)
        {
            if (ar[i] < x)
            {
                xRankL++;
                xRankR++;
            }
            
            if (ar[i] == x && exists)
            {
                xRankR++;
            }
            
            if (ar[i] == x)
            {
                exists = true;
            }
        }

        var a = exists ? 0 : 1;
        
        if(!exists && k < xRankL && p == xRankL)
        {
            Console.WriteLine(-1);
            continue;
        }
        
        if (p >= xRankL && p <= xRankR)
        {
            Console.WriteLine(0 + a);
            continue;
        }

        if (p < xRankL)
        {
            if (k <= p)
            {
                Console.WriteLine(xRankL - p + a);
            }
            else
            {
                Console.WriteLine(-1);
            }
        }
        else
        {
            if (k >= p)
            {
                Console.WriteLine(p - xRankR + a);
            }
            else
            {
                Console.WriteLine(-1);
            }
        }
    }
#include <bits/stdc++.h>

using namespace std;
int coun = 0;
int main()
{
int t;
cin >> t;
while (t–)
{
long long int n, x, p, k;
cin >> n >> x >> p >> k;
long long int arr[n + 1];
for (long long int i = 1; i <= n; i++)
{
cin >> arr[i];
}
sort(arr + 1, arr + n + 1);
if (arr[p] == x)
{
cout << 0 << endl;
}
else if (((p < k) && ((x > arr[p]))) || ((p > k) && ((x < arr[p]))))
{
cout << -1 << endl;
}
// else if(((k>=p)&&arr[p]>=x)||((p>=k)&&arr[p]<=x)){
// cout<<-1<<endl;
else
{
while ((arr[p] != x) && (coun < n))
{
arr[k] = x;
sort(arr + 1, arr + n + 1);
coun++;
}
if (arr[p] == x)
{
cout << coun << endl;
}
else
{
cout << -1 << endl;
}
}
}
return 0;
}

This question does not require sorting and no loops it just requires if else conditions

Done it just missed a condition that if x does not exist in array and k < xRank than actual rank is xrank -1;

https://www.codechef.com/viewsolution/38965981

i have implemented it using binary search and sorting.
it is almost same as the editorial but still it is failing the test cases.
pls help

Hi Every one,

Could any one please give me some test cases where n is equal to or less then five. I guess my code is failing for this particular test cases.
Thanks a Lot.
#include <bits/stdc++.h>
using namespace std;

int binarySearch(int arr[],int l,int r,int x){

if (r >= l) { 
  
    int mid = l + (r - l) / 2; 
    if (arr[mid] == x) 
        return mid; 

    if (arr[mid] > x) 
        return binarySearch(arr, l, mid - 1, x); 
    return binarySearch(arr, mid + 1, r, x); 
} 

return -1; 

}

int main() {
// your code goes here
int t;scanf("%d",&t);
while(t–){
int n,x,p,k;
scanf("%d%d%d%d",&n,&x,&p,&k);int arr[n+1];arr[0]=0;
for(int i=1;i<=n;i++)scanf("%d",&arr[i]);

    sort(arr,arr+(n+1));
    if(arr[p]==x){
        printf("0\n");
    }
    else if((x>arr[p]&&p<k)||(x<arr[p]&&p>k)) printf("-1\n");
    else {int position;
        
            if(x<arr[p]){
            //printf("FisrtIF\n");
            position = binarySearch(arr,1,p-1,x);
            for(int z=position+1;z<p;z++){
                if(arr[z]!=x) break;
                else position=z;
            }
            int flag=0;
              if(position==-1) {
                 
               int flg=0;
	           for(int j=p;j>1;j--){
	               if(arr[j-1]<x){
	                   position=j;flg=1;break;
	               }
	           }
	           
	           if(flg==0)position=1;
	           
	           printf("%d\n",p-position+1);
	           flag=1;
              }
              if(flag==0)
             printf("%d\n",p-position);
        }
        
        else {
            position= binarySearch(arr,p+1,n,x);
            
              int flag=0; 
              for(int z=position-1;z>p;z--){
                if(arr[z]!=x) break;
                else position=z;
            }
            //printf("%d--\n",position);
              if(position==-1) {
                  
                  int flg=0;
	           for(int j=p;j<n-1;j++){
	               
	               if(arr[j+1]>x){
	                   position=j;flg=1;break;
	               }
	               
	           }
	           if(flg==0)position=n;
	           
	           printf("%d\n",position-p+1);
	           flag=1;
              }
              if(flag==0)
              printf("%d\n",position-p);
              
        //for(int i=0;i<=n;i++)printf("%d ",arr[i]);printf("\n");
        
    }
}}
return 0;

}

i was getting Partially Correct answer but still not able to figure out why it’s falling. Please help me to fix this
ThankYou
code:
`#include <bits/stdc++.h>
using namespace std;

int main() {

ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin >> t;
while(t--){
    int n,x,p,k,temp,count;
    cin >> n >> x >> p>> k;
    vector<int> num;
    for(int i=0;i<n;i++){
        cin >> temp;
        num.push_back(temp);
    }
    sort(num.begin(),num.end());
    // for(int i=0;i<n;i++){
    //     cout << num[i] << " ";
    // }cout << endl;
    p--;
    k--;
    n--;
    // cout << "numP : " << num[p] <<" ,numK : "<< num[k] << ",X : " <<x<<endl;
    if(p==k && x<num[p]){
        count =0;
        for(int i=p;i>=0;i--){
            if(num[i]>x) count++;
        }
        cout << count;
    }
    else if(p==k && x>num[p]){
        count = 0;
        for(int i=p;i>=n;i++){
            if(num[i]<x) count++;
        }
        cout << count;
    }
    else if(p<k && x<num[p]){
        count =0;
        for(int i=p;i>=0;i--){
            if(num[i]>x) count++;
        }
        cout << count;
    }
    else if(p>k && x>num[p]){
        count = 0;
        for(int i=p;i<=n;i++){
            if(num[i]<x) count++;
        }
        cout << count;
    }
    else if(p<k && x>num[p] || p>k && x<num[p]){
        cout << -1;
    }
    else{
        cout << 0;
    }
    cout << endl;
}
return 0;

}

`