Source Code
#include<bits/stdc++.h>
#define int long long
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int rand(int l, int r){
    uniform_int_distribution<int> uid(l, r);
    return uid(rng);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
    cin.tie(NULL);

int tt=1;

//cout<<(1LL<<31LL)<<endl;
//cin>>tt;
//int T=

while(tt--)
{
   
   int n,m;
   cin>>n>>m;
   int mx=0;
   int arr[n];
   map<int,int>mp;
   for(int i=0;i<n;i++){
int x;
    cin>>arr[i];
    mp[arr[i]]++;
}
sort(arr,arr+n);
if(n==2&&(arr[1]-arr[0])%2==0)
{
    cout<<abs(m-n)<<"\n";
    return 0;
}
int ig=n-mp[arr[n-1]];
int ig2=n-mp[arr[0]];

int rr=0;
for(int i=0;i<ig;i++)
{
    if(i<ig/2)
    {
        rr+=(arr[i]+(m-arr[n-1]));
    }   
    else rr-=(arr[n-1]-arr[i]);
}
if(rr==0)
{
   
   cout<<abs(m-n)<<"\n";
    return 0;
}
//cout<<"FUCK"<<"\n";
int fi=mp[arr[0]];
rr=0;
ig=ig2;
for(int i=0;i<ig;i++)
{
    if(i>=ig/2)
    {
        rr+=(arr[0]+(m-arr[i+fi]));
    }   
    else rr-=(arr[i+fi]-arr[0]);
}
if(rr==0)
{
     cout<<abs(m-n)<<"\n";
    return 0;
}
cout<<0<<"\n";
}}

/*stolen stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?)
    * do smth instead of nothing and stay organized
    * WRITE STUFF DOWN
    * DON'T GET STUCK ON ONE APPROACH
*/
Copy
Spin the wheel Boody04
GNU G++17
2 ms
244 KB
Wrong Answer