Help needed for a problem from CODERS contest

This code is giving WA for some test cases. Can anyone point out those test cases?

Problem → Contest Page | CodeChef


    
    int t; cin>>t;
    while(t--){
        int n; cin>>n;
        
        int arr[n]; int ans=0;
        int zero=0, nonzero=0, ones=0;
        
        for(int i=0; i<n; i++){
            cin>>arr[i];
            if(arr[i]==0) zero++;
            else if(arr[i]==1) ones++;
            
            if(arr[i]!=0)nonzero++;
        }
        
        sort(arr, arr+n);
        int cnt=1;
        for(int i=1; i<n; i++){
            if(arr[i]==arr[0]) cnt++;
        }
        if(zero>=1) ans=nonzero;
        
        else if(arr[0]==1) ans=n+(nonzero-ones);
        else ans= (n-cnt)+(arr[0]*n);
        
        cout<<ans<<endl;
    }

Problem solved :slight_smile:
Instead of using int , use long long
because int overflow may occur larger arr[i] * n

1 Like