PROBLEM LINK: Best Enemies | CodeChef
Problem Code: Best Enemies | CodeChef
Practice: CodeChef | Competitive Programming | Participate & Learn | CodeChef
Contest : Carnival Finale Coding Competition | CodeChef
Author: Codechef Adgitm Chapter : dayash | CodeChef User Profile for Yash | CodeChef(https://www.codechef.com/users/tannatsri)
Tester: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
Editorialist: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
DIFFICULTY:
Easy
PROBLEM:
Chef and Aditya are enemies. Chef is obsessed with large positive integers but he won’t perform any operation with an integer or it’s multiples which is chosen by Aditya (2 or 3).Chef was in hurry as he had to attend a party, so he assigned the task of creating the largest number possible by the given digits in number. You can use a digit only once and the length of largest number should be equal to the length of the given number.
EXPLANATION:
Swapping while traversal.
SOLUTION:
C++:
#include<bits/stdc++.h>
using namespace std;
void solve()
{
ll n,number;
cin >> n;
string a;
cin >> a;
cin >> number;
vector given;
map<ll,ll> not_changable;
for(ll i=0;i<n;i++)
{
ll ch=a[i]-‘0’;
if(ch%number==0)
not_changable[i]=1;
else
given.push_back(a[i]);
}
sort_2(given);
ll j=0;
for(ll i=0;i<n;i++)
{
if(not_changable[i]==0)
{
cout << given[j++];
}
else
cout << a[i];
}
cout << endl;
}
signed main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll tt=1;
cin>>tt;
while(tt–)
{
solve();
}
return 0;
}