+ 21
CHALLENGE: LARGEST NUMBER WITH ONE SWAP 💻⌨️🖱️
Task: Find the largest number (without leading zeros) that could be generated by swapping only two digits of given integer at most once (so, only one swap is allowed). If is given number already and the largest number, you do not make any swap. Examples: Input: 2736 Output : 7236 Input: 841882 Output : 881842 Input: - 65480223 Output : - 25480263 Input : 852 Output : 852 All languages are welcome! 🤓
19 Answers
+ 9
Interesting challenge!
Here is my try with Java programming language:
https://code.sololearn.com/cQC26R1JqQpu
+ 11
Here's for example :
https://code.sololearn.com/cATE8XxDx77C/?ref=app
+ 11
Only one swap. So, for input 2736 you have next possibilities :
2736 (initial number) , 7236, 3726, 6732, 2376, 2637, 2763. The largest number is 7236.
If you already have the largest number, in case 852 as initial number, you do not make any swap.
+ 9
@sayan
The output number must be largest as possible and differ from the initial for one swap
+ 8
+ 6
My trying with Python:
https://code.sololearn.com/cJ111cQRPKXy/
+ 4
Thank you very interesting challange.
Here is my attempt!
https://code.sololearn.com/c02FozHqpHRp/?ref=app
+ 3
My try here, Tell me if there are any mistakes or if takes too much time to find an answer and I will try to fix It. =D
https://code.sololearn.com/c6AGBRlqzyRC/?ref=app
+ 2
is it like...
U CAN DO ONLY ONE TIME SWAP IN CODE
OR DO WHATEVER BUT OUTPUT NO. SHOULD DIFFERS ONY ONE-SWAP WITH INPUT
??????
+ 2
I used JS: https://code.sololearn.com/Wop0UaaJM8Kk/#js
+ 2
here's mine
https://code.sololearn.com/cHwm2mwvm1T9/?ref=app
+ 2
https://code.sololearn.com/c5R3FmCrGXYo/?ref=app
https://code.sololearn.com/ce3nW1LTDwpJ/?ref=app
+ 1
I try...
+ 1
A little bit late, but i believe my code is the shortest one in python so far:
https://code.sololearn.com/cqqt6ZWjQrJD/?ref=app
+ 1
Made corrections on the code so it now accepts negative numbers. Still the shortest one in python!
+ 1
After correcting the bugs, it isn't the shorter anymore :( hahaha
0
find two largest numbers locations and swap them to 0th nd 1st index locations.. primary logic