In this problem, there was only test case which I missed, and that was 1 1 2 2 5. In this case without sorting you will get answer 2, and with sorting in descending order you will get answer 3. Since, we want maximum, hence 3 will be the answer.
My code:- CodeChef: Practical coding for everyone
1 Like
Thanks, I was so confused why my code was not working
include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
for(int i=0 ; i<t ; i++){
int n,x;
cin>>n>>x;
int arr[n];
for(int j=0 ; j<n ; j++){
cin>>arr[j];
}
int l = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + l);
int coun =0,v=0;
while(arr[v]<x){
coun++;
v++;
}
coun=0;
int sum=0;
while(arr[n-1]>x){
while(arr[n-1]>x){
sum++;
(arr[n-1])=(arr[n-1])-1;
}
n--;
coun++;
}
n= n+ coun;
while (v >= 0) {
while ((arr[v] < x) && (sum >= 0) ) {
arr[v]=arr[v]+1;
if(sum==0){
break;
}
sum--;
}
if(sum==0){
break;
}
v--;
}
int p=0 ;
for(int r =0; r<n;r++){
if(arr[r]==x){
p++;
}
else{
p=p;
}
}
cout<<p<<endl;
}
}
please tell the error