Find next higher number using exact same digits

Problem Given a number X find the number Y which is next higher number than X using the exact same digits as in X. Example For example: given 38276 return 38627 Solution Method 1 - Brute force The brute-force method is to generate the numbers with all digit permutations and consider all the higher numbers than the given number and return the minimum number from those higher numbers. [Read More]

Next Greater Element in an array

Problem Given an array, print the next greater element for every element. Elements for which no greater element exist, print next greater element as -1. Example For the elements of the array [4, 5, 2, 25, 20, 11, 13, 21, 3] greater elements are as follows. 4 –> 5 5 –> 25 2 –> 25 25 –> -1 20 –> 21 11 –> 13 13 –> 21 21 –> -1 [Read More]