• + 1 comment

    Best possible solution can be found from (min,min-1,min-2). I think we don't need to check for (min-3) and (min-4). I have submitted by calculating optimal from (min,min-1,min-2). Can you provide an example where min-3 or min-4 is more optimal ?

    My code :

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int t,n;
        cin>>t;
        while(t--)
        {
            cin>>n;
            int arr[n],i,op1=0,op2=0,op3=0,mn=10000,left;
            for(i=0;i<n;i++)
            {
                cin>>arr[i];
                if(mn>arr[i])
                    mn=arr[i];
            }
            for(i=0;i<n;i++)
            {
                left=arr[i]-mn; // for (min)
                op1+=left/5;
                left%=5;
                op1+=left/2;
                left%=2;
                op1+=left/1;
    
                left=arr[i]-(mn-1); // for (min-1)
                op2+=left/5;
                left%=5;
                op2+=left/2;
                left%=2;
                op2+=left/1;
    
                left=arr[i]-(mn-2); // for (min-2)
                op3+=left/5;
                left%=5;
                op3+=left/2;
                left%=2;
                op3+=left/1;
            }
            op1=min(op1,op2); // optimal from min-1 and min-2
    
            cout<<min(op1,op3)<<endl; // Most Optimal
        }
    }