My issue
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin>>t;
while(t--){
long long n,m; cin>>n>>m;
long long dp[n];
for(int i=0; i<n; i++){
cin>>dp[i];
}
sort(dp,dp+n);
long long ans=0;
int x=1;
for(int i=0; i+1<n; i++){
ans+=dp[n-1]-dp[i];
}
cout<<max((dp[n-1]-dp[0]),(ans%m==0)? ans/m: (ans/m)+1)<<"\n";
}
return 0;
}
Why we take the diff between max and min?
5 2
1 2 3 4 5
Iteration 1(index 3,4): 1 2 4 5 5
Iteration 2(index 2,3): 1 3 5 5 5
Iteration 3(index 1,2): 2 4 5 5 5
Iteration 4(index 1,2): 3 5 5 5 5
Iteration 5(index 1,2): 4 5 5 5 5
Iteration 6(index 1,2): 5 5 5 5 5
Here it needs 6 iterations but the following code gives 5 which is accepted. Why?
Problem Link: Make All Equal Practice Coding Problem - CodeChef