Next Greater Number Problem Statement
Given a string S
which represents a number, determine the smallest number strictly greater than the original number composed of the same digits. Each digit's frequency from 0-9 must match the original.
Input:
The first line is an integer T, the number of test cases.
Each test case contains one line with the string S.
Output:
For each test case, output the smallest number strictly greater using the same set of digits or -1 if it doesn't exist.
Example:
If given S = '56789'
, the output should be 56798
. Note that 56790
is also greater but uses different digits.
Constraints:
1 ≤ T ≤ 100
1 ≤ len(S) ≤ 104
- The function has a time limit of 1 second.
Note:
The input string is non-empty with no leading zeros. Implement the function; printing is handled elsewhere.
AnswerBot
1y
The task is to find the smallest number greater than the given number, with the same set of digits.
Iterate through the digits of the given number from right to left.
Find the first digit that is smalle...read more
Help your peers!
Add answer anonymously...
Top Cisco Data Engineer interview questions & answers
Popular interview questions of Data Engineer
Top HR questions asked in Cisco Data Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app