CHALLENGE: LARGEST NUMBER WITH ONE SWAP 💻⌨️🖱️ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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! 🤓

26th Nov 2017, 10:20 PM
LukArToDo
LukArToDo - avatar
19 Answers
+ 9
Interesting challenge! Here is my try with Java programming language: https://code.sololearn.com/cQC26R1JqQpu
27th Nov 2017, 7:39 AM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
26th Nov 2017, 10:22 PM
LukArToDo
LukArToDo - avatar
+ 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.
27th Nov 2017, 5:27 AM
LukArToDo
LukArToDo - avatar
27th Nov 2017, 1:25 PM
Käzî Mrîdùl Høssäîn
Käzî Mrîdùl Høssäîn - avatar
+ 9
@sayan The output number must be largest as possible and differ from the initial for one swap
27th Nov 2017, 2:59 PM
LukArToDo
LukArToDo - avatar
27th Nov 2017, 8:51 AM
David Akhihiero
David Akhihiero - avatar
28th Nov 2017, 11:32 AM
Alex V. S.
+ 4
Thank you very interesting challange. Here is my attempt! https://code.sololearn.com/c02FozHqpHRp/?ref=app
30th Nov 2017, 8:37 AM
Hamid
Hamid - avatar
28th Nov 2017, 9:03 AM
Hiroki Masuda
Hiroki Masuda - avatar
+ 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
29th Nov 2017, 1:19 PM
Lucas Pardo
Lucas Pardo - avatar
+ 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 ??????
27th Nov 2017, 2:50 PM
sayan chandra
sayan chandra - avatar
30th Nov 2017, 12:42 AM
Norberttony
Norberttony - avatar
1st Dec 2017, 5:49 PM
Cuttek
Cuttek - avatar
+ 2
https://code.sololearn.com/c5R3FmCrGXYo/?ref=app https://code.sololearn.com/ce3nW1LTDwpJ/?ref=app
4th Dec 2017, 9:39 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
+ 1
I try...
28th Nov 2017, 9:08 AM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar
+ 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
1st Dec 2017, 2:36 AM
Murillo Pyaia
Murillo Pyaia - avatar
+ 1
Made corrections on the code so it now accepts negative numbers. Still the shortest one in python!
1st Dec 2017, 6:34 PM
Murillo Pyaia
Murillo Pyaia - avatar
+ 1
After correcting the bugs, it isn't the shorter anymore :( hahaha
1st Dec 2017, 7:46 PM
Murillo Pyaia
Murillo Pyaia - avatar
0
find two largest numbers locations and swap them to 0th nd 1st index locations.. primary logic
29th Nov 2017, 2:56 AM
Nani Nani