I was solving a problem and the problem description was:
Given a number N. You have to perform exactly two swap operations to make the number as large as possible.
You are given a number N (10 ≤ N ≤ 2×109). You have to perform exactly two swap operation. You may choose any two unequal positions of this number, and swap the digits at those two positions.
Like, for number 451 you can choose position 1 and 2 then swap this after swapping number will 541 again you can choose position 2 and 3 and swap after swapping number become 514.
What is the largest number you can get after these two operations?
So I made a code which kind of makes sense but I can't understand why in my code swap()
is swapping the same character. You may understand by seeing the code better.
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--){
string s;
cin>>s;
ll k=2;//Number of minimum swaps
for(int i=0;i<s.size();i++){
char ma=s[i];//storing maximum number
for(int j=i+1;j<s.size();j++){
if(ma < s[j]){
ma=s[j];
}
}
if(ma != s[i]){
swap(ma,s[i]);//swaping two chars if I found something bigger than s[i]. This line is also occuring error I mean unexpected behavior
k--;
}
cout<<"Iteration "<<i+1<<" "<<s<<endl;//This line is for inspecting what is going on
}
}
}
Input and output:
Input:
1
123
Output:
Iteration 1 323
Iteration 2 333
Iteration 3 333
This shouldn't happen if you see the code.
Please say what is going on with swap
.
As @molbdnilo mentioned, your ma
variable is only a copy of your s[j]
char. To actually switch between those two chars, you need to use a temp variable to store the location of j
int jtmp;
for(int j=i+1;j<s.size();j++)
{
if(ma < s[j])
{
ma=s[j]; jtmp = j;
}
}
Full modified code:
#include <iostream>
#include <string>
#define ll long long
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--){
string s;
cin>>s;
ll k=2;
for(int i=0;i<s.size();i++)
{
char ma=s[i]; int jtmp;
for(int j=i+1;j<s.size();j++)
{
if(ma < s[j])
{
ma=s[j]; jtmp = j;
}
}
if(ma != s[i])
{
swap(s[jtmp],s[i]);
k--;
}
cout<<"Iteration "<<i+1<<" "<<s<<endl;
}
}
}
Result:
1
45231
Iteration 1 54231
Iteration 2 54231
Iteration 3 54321
Iteration 4 54321
Iteration 5 54321
Addition : You can see that your loop still goes for a while after the string are sorted. This could be fixed with a function to check if the string has been sorted.
#include <iostream>
#include <string>
#include <algorithm>
#define ll long long
using namespace std;
bool LF(char a, char b)
{
return a > b;
}
bool stringCheck(string x)
{
string y = x;
sort(y.begin(), y.end(), LF);
if (y == x) {return true;} return false;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--){
string s;
cin>>s;
ll k=2;
for(int i=0;i<s.size();i++)
{
char ma=s[i]; int jtmp;
for(int j=i+1;j<s.size();j++)
{
if(ma < s[j])
{
ma=s[j]; jtmp = j;
}
}
if(ma != s[i])
{
swap(s[jtmp],s[i]);
k--;
}
cout<<"Iteration "<<i+1<<" : "<<s<<endl;
if (stringCheck(s)) {break;}
}
}
}
Result:
1
45231
Iteration 1 : 54231
Iteration 2 : 54231
Iteration 3 : 54321