Submit | All submissions | Best solutions | Back to list |
JNEXT - Just Next !!! |
DevG likes too much fun to do with numbers. Once his friend Arya came and gave him a challenge, he gave DevG an array of digits which is forming a number currently (will be called as given number). DevG was challenged to find the just next greater number which can be formed using digits of given number. Now DevG needs your help to find that just next greater number and win the challenge.
Input
The first line have t number of test cases (1 ≤ t ≤ 100). In next 2×t lines for each test case first there is number n (1 ≤ n ≤ 1000000) which denotes the number of digits in given number and next line contains n digits of given number separated by space.
Output
Print the just next greater number if possible else print -1 in one line for each test case.
Note : There will be no test case which contains zero in starting digits of any given number.
Example
Input: 2 5 1 5 4 8 3 10 1 4 7 4 5 8 4 1 2 6 Output: 15834 1474584162
Added by: | ! include(L.ppt) |
Date: | 2012-08-31 |
Time limit: | 1.297s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | MNNIT OPC 31-08-2012 |
hide comments
|
||||||||||||||
2018-01-23 14:05:24
next_permutation()..... in c++ is all thing about this problem |
||||||||||||||
2018-01-14 05:42:24
mayank_soni055: http://www.spoj.com/ranks/JNEXT/lang=PYTH%203.2.3 Avinash Raj, ritwikshanker: http://www.spoj.com/ranks/JNEXT/lang=JAVA |
||||||||||||||
2018-01-13 11:49:57
Don't try in PYTHON it'll give you TLE use C/C++ |
||||||||||||||
2018-01-10 06:40:07 Avinash Raj
Don't use Java you'll get TLE. |
||||||||||||||
2018-01-08 12:11:21
what is the max possible number? there's info max total number possible... but nothing of max number possible. |
||||||||||||||
2018-01-03 20:58:35
AC in go :) Thanks to STL vector. |
||||||||||||||
2017-12-29 17:56:52
whats wrong with this code ? <snip> Last edit: 2022-08-09 23:07:46 |
||||||||||||||
2017-12-23 19:53:00
Same code shows Passed on Geeksforgeeks but wrong answer here. Is there a case I'm missing ? |
||||||||||||||
2017-12-14 16:55:02
Getting TLE in JAVA. SHITTY SPOJ!!! Same soln in c++ submitted. They need to check their time limit for each language. |
||||||||||||||
2017-11-25 02:27:09
AC in 1 go. :) |