Regarding Contest Problem STRATERS 58 ADD TO SUBSEQUENCE EASY EXPLANATION WITHOUT USING log2!

int main() {
// your code goes here
int T;
cin>>T;
while(T–)
{
int N;
cin>>N;
int A[N];
for(int i=0;i<N;i++)
{
cin>>A[i];
}
map<int,int>m;
for(int i=0;i<N;i++)
{
m[A[i]]++;
}
int Operations=0;
int foo=1;int flag=0;int d;
while(flag==0)
{
int Upper_Bound=m.rbegin()->first + 1;
for(auto p:m)
{
if(p.second>1)
{
flag=1;
if(foo==1)
{
d=Upper_Bound-p.first;int D;
if(p.second>=4)
{
D=p.second/2;
m[Upper_Bound]=D;
m[p.first]=p.second-D;
}
else
{
m[Upper_Bound]=1;
m[p.first]=p.second-1;
}
foo=0;
/int D=(p.second)/2;
m[Upper_Bound]=D;
m[p.first]-=p.second-D;foo=0;
/
}
else
{
int D=(p.second)/2;
m[d+p.first]=D;
m[p.first]-=D;
}
}
}
if(flag==1)
{
Operations++;
foo=1;flag=0;
}
else
{
cout<<Operations<<endl;break;
}
}
}
return 0;
}