Even Odd Swap -Editorial || SWAPPY

PROBLEM LINK: Even Odd Swap | CodeChef

Problem Code: Even Odd Swap | 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 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:

Medium

PROBLEM:

Chef and his sister are playing a game in which a number is given by his friend . Chef loves even numbers while his sister chooses odd ones. Zero is considered neither even nor odd by these siblings. Chef’s sister chose all the best unit places and now he’s going to lose , but not when his friend is alive. Help his friend to change the unit places. Given a number , swap even digits with odd ones.

Chef and his sister are bored staying home due to lockdown, so they decided to play a game. They both love numbers and asked their mother who is a mathematics professor at ADGITM for a number. Chef likes even numbers while his sister is fond of odd ones. Zero is considered neither even nor odd by them.

Chef has been assigned the task to convert a number into another encrypted number. The encryption should be done in such a way that when you encounter the first digit as even then you need to swap it with the first clockwise odd element and repeat the same process from the next even number with occurs after the swapped number but if the first encounter digit is odd then you need to go anti-clockwise and swap it with the first even number and start the same process with number next to swapped number.

Note1 - 0 is considered neither odd nor even.
Note2 - Even should be the first digit.

Note3 - If the first digit is odd , it is to be swapped with an even digit found from the end of the number.

EXPLANATION:

Use Swapping.

SOLUTION:

C++:

int main()
{
test{
lli n;
cin>>n;
string s;
cin>>s;
lli cnt=0,x=-1,f=0;
if((s[0]-48)%2==0)
{
loop0(i,n)
{
if((s[i]-48)%2!=0)
{
if(cnt>0)
{
swap(s[x],s[i]);
}
cnt=0;
f=0;
}
else if(s[i]!=‘0’)
{
if(f==0)
{
x=i;
f=1;
}
cnt++;
}
}
cout<<s<<endl;
}
else
{
lli z=0;
rev0(i,n)
{
if((s[i]-48)%2==0&&s[i]!=‘0’)
{
swap(s[0],s[i]);
z=i;
break;
}
}
loop1(i,z-1)
{
if((s[i]-48)%2!=0)
{
if(cnt>0)
{
swap(s[x],s[i]);
}
cnt=0;
f=0;
}
else if(s[i]!=‘0’)
{
if(f==0)
{
x=i;
f=1;
}
cnt++;
}
}
cout<<s<<endl;
}
}
}