Next Greatest number Program in java

Question:

Write a program to find the next greatest number that can be formed using the digits in the given input number.

If the given number cannot generate the next greatest number then print the input number itselfSample Input1:

23

Sample Output1:

32

Sample Input2:

123

Sample Output2:

132

Sample Input3:

6251

Sample Output3:

6512

CODE:

import java.util.Scanner;
import java.util.Arrays;
public class Main{
    
    static void swap(char ar[], int i, int j){
        char temp = ar[i];
        ar[i] = ar[j];
        ar[j] = temp;
    }
    
    static void findNextGreatest(char ar[], int n){
        int i;
        for(i = n -1; i > 0; i--){
            if(ar[i] > ar[i-1])
                break;
        }
        if(i == 0){
            for(int j = 0; j < ar.length; j++)
                System.out.print(ar[j]);
        }
        else{
            int x = ar[i - 1], min = i;
            for(int k = i + 1; k < n; k++){
                if(ar[k] > x && ar[k] < ar[min])
                    min = k;
            }
            swap(ar, i-1, min);
            Arrays.sort(ar, i, n);
            for(int l = 0; l < ar.length; l++)
                System.out.print(ar[l]);
        }
    }
    
    
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        char arr[] = input.toCharArray();
        findNextGreatest(arr, arr.length);
        
    }
}
Next:
  1. Mark Comparison
  2. Print the characters in descending order
  3. Vowels in a fishBowl
  4. Least offers

Leave a Comment

Scroll to Top