Accenture Coding Question | Next Greater Number | CodeWindow

Next Greater Number

Write a number ‘N’ (containing at most 10,000 digits), find the next grater number having the same digits. It is guaranteed that there exists a next greater number having the same digits as N.

Input specification:
Input1: The length of the string ‘N’.
Input2: The number ‘N’ in the form of a string.

Output Specification:
Return the next greater number having the same digits as ‘N’ in the form of a string.

Example 1:
Input 1: 3
Input 2: 182

Output: 218
Explanation:
The next greater number after 182 is 218 using {1,8,2}

Example 2:
Input 1: 4
Input 2: 2345

Output: 2354
Explanation:
The next greater number after 2345 is 2345 using {2,3,4,5}

Solution in Python 3:

#https://codewindow.in
#join our telegram channel

from itertools import permutations

r=int(input()) #length
n=input() #taking the input as a string

a = permutations(n, r)
#print(a)

y = [''.join(i) for i in a] # joining the digits to a y
  
y=sorted(y)

q=y.index(n) #finding the index of the input value from the list
#print(q)
print(y[q+1])

# Telegram @codewindow

Output:

4
2345
2354
Recent Posts

Leave a Comment

Your email address will not be published. Required fields are marked *