Help me in solving BUDGET25 problem

My issue

#include<bits/stdc++.h>
using namespace std;
int main(){
int t,n,x,i,sum;
bool f=true;
cin>>t;
while(t–){
cin>>n>>x;
int a[n];
for(i=0;i<n;i++){
sum=0;
cin>>a[i];
sum=sum+a[i];
}

 for(i=n;i>=1;i--){ 
     
     float m=sum*1.0/i;
     if(m>=x&&m<(x+1)){
         cout<<i<<endl;
         break;
     }
     
     }
     
         cout<<0<<endl;
     }

}

}

My code

 #include<bits/stdc++.h>
 using namespace std;
 int main(){
 int t,n,x,i,sum;
 bool f=true;
   cin>>t;
 while(t--){
     cin>>n>>x;
     int a[n];
     for(i=0;i<n;i++){
          sum=0;
         cin>>a[i];
         sum=sum+a[i];
     }
      
     for(i=n;i>=1;i--){ 
         
         float m=sum*1.0/i;
         if(m>=x&&m<(x+1)){
             cout<<i<<endl;
             break;
         }
         
         }
         
             cout<<0<<endl;
         }
     
     
 }
     
 }

Problem Link: Budget Allotment Practice Coding Problem

@vedaakash
its a simple greedy problem .
plzz refer my c++ code for better understanding

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    long long int n,x;
	    cin>>n>>x;
	    long long int a[n],sm=0;
	    for(int i=0;i<n;i++)
	    {
	        cin>>a[i];
	        if(a[i]>x)
	        {
	        sm+=(a[i]-x);
	        a[i]=0;
	        }
	        else
	        a[i]=x-a[i];
	    }
	    sort(a,a+n);
	    int ch=n;
	    for(int i=0;i<n;i++)
	    {
	       if(a[i]<=sm)
	       {
	           sm-=a[i];
	       }
	       else
	       {
	           ch=i;
	           break;
	       }
	    }
	    cout<<ch<<endl;
	}
	return 0;
}