greedy math strings *1300

Please click on ads to support us..

Python Code:

class C():
    def __init__(self, value, index):
        self.value = value
        self.index = index


n = input()

a = []
num = int(n[len(n)-1])

for i in range(len(n)):
    if int(n[i])%2 == 0:
        c = C(int(n[i]), i)
        a.append(c)

c = 1

if a == []:
    print(-1)
    c = -1
else:
    for i in a:
        if num>i.value:
            print(n[0:i.index]+str(num)+n[i.index+1:len(n)-1]+str(i.value))
            c = -1
            break


if c != -1:
    i = a[len(a)-1]
    print(n[0:i.index] + str(num) + n[i.index + 1:len(n) - 1] + str(i.value))

C++ Code:

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h> 
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
 

void solve(){
    string s;
    cin>>s;
    int n = sz(s);
    bool key=true;
    forn(i,n)
    {
        if((s[i]-'0')%2!=1)
        {
            key=false;
            break;
        }
    }
    if(key)
    {
        cout<<-1<<endl;
        return;
    }
    int r=s[n-1]-'0';
    forn(i,n)
    {
        if((s[i]-'0')%2==0)
        {
            int l=(s[i]-'0');
            if(l<r)
            {
                swap(s[i],s[n-1]);
                cout<<s<<endl;
                return;
            }
        }
    }
    rforn(i,n-1)
    {
        if((s[i]-'0')%2==0)
        {
            swap(s[i],s[n-1]);
            cout<<s<<endl;
            return;
        }
    }
}
int main()
{
    fast_cin();
    ll t = 1;
    // cin >> t;
    for(int it=1;it<=t;it++) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses