Can Someone review my solution for the problem Art of Balance:
int offset(ll t[],ll goal,ll rows,ll excluded){
float off=0,set=0;
for(int i=0;i<rows;i++)
{
off=off+abs(goal - t[i]);
}
if(excluded==0){
return (off/2);}
else{
for(int i=rows;i<=(rows+excluded);i++)
{
set=set+t[i];
}
return ((off+set)/2);
}
}
bool nonzero(int i)
{return (i!=0);}
int count(string s, char c)
{
int res = 0;
for (int i=0;i<s.length();i++)
if (s[i] == c)
res++;
return res;
}
int main()
{
ll t,occ[26],stdc,numofcounts,ans=0,modifiedlength,slength,y;
float special;
cin>>t;
while(t–)
{
string str;
cin>>str;
slength=str.length();
for(int i=0;i<=25;i++)
{
char c = 'A'+i;
occ[i]=count(str, c);
}
sort(occ,occ+26,greater<ll>());
numofcounts=count_if(begin(occ),end(occ),nonzero);
if(slength%numofcounts==0)
{ ans=offset(occ,slength/numofcounts,numofcounts,0); }
else{
for(int i=0;;i++)
{
if(slength%(numofcounts-i)==0){
if((i)>(y=((slength/i)%numofcounts)))
{
modifiedlength=y+numofcounts;
}
else
{
modifiedlength=numofcounts-i;
}
break;}
}
ans=offset(occ,slength/modifiedlength,modifiedlength,numofcounts-modifiedlength);
}
cout<<ans<<endl;
}
return 0;
}
thanking you!