➤ Problem Link : 1157E. Minimum Array
✅ C++ Solution :
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
int n;
cin>>n;
int A[n+1];
int B[n+1];
int C[n+1];
map<int,pair<int,vector<int> > > st;
st.clear();
for(int i=1;i<=n;i++)
cin>>A[i];
for(int i=1;i<=n;i++)
{
cin>>B[i];
st[B[i]%n].second.push_back(B[i]);
st[B[i]%n].first += 1;
}
int v,t;
for(int i=1;i<=n;i++)
{
v=A[i];
t=(n-(v%n))%n;
auto it=st.lower_bound(t);
auto it2=st.lower_bound(0);
if(it==st.end())
{
B[i]=((*it2).second.second)[--((*it2).second.first)];
if((*it2).second.first == 0)
st.erase(it2);
}
else
{
if((((*it).second.second)[0] + v )%n < (((*it2).second.second)[0] + v) %n)
{
B[i]=((*it).second.second)[--((*it).second.first)];
if((*it).second.first == 0)
st.erase(it);
}
else
{
B[i]=((*it2).second.second)[--((*it2).second.first)];
if((*it2).second.first == 0)
st.erase(it2);
}
}
C[i]=(A[i]+B[i])%n;
}
for(int i=1;i<=n;i++)
cout<<C[i]<<" ";
}
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!
