Adding a Digit to Maintain Ascending Order

The program must accept a number N as input. Determine if it’s possible to add at most one digit (0-9) anywhere in the number to make its digits appear in strictly ascending order. If it’s possible, print the new number. If not, or if the number is already in ascending order, print NO.

Boundary Condition(s):
2 <= Length of N <= 100

Input Format:
The first line contains the number N.

Output Format:
The first line contains the modified number or NO.

Example Input/Output 1:
Input:
12457

Output:
12457

Explanation:
The digits of the number 12457 are already in ascending order.

Example Input/Output 2:
Input:
12957

Output:
129357

Explanation:
By adding ‘3’ before ‘5’, the number becomes in ascending order.

number = input().strip()
for i in range(len(number) - 1):
    if number[i] > number[i + 1]:
        for j in range(9, -1, -1):
            if number[i] < str(j) < number[i + 1]:
                print(number[:i + 1] + str(j) + number[i + 1:])
                break
        else:
            print("NO")
        break
else:
    print("NO")
#include <stdio.h>
#include <string.h>

int main() {
    char num[105];
    scanf("%s", num);
    int len = strlen(num), i, j;
    for(i = 0; i < len - 1; i++) {
        if(num[i] > num[i+1]) break;
    }
    if(i == len - 1) {
        printf("NO");
        return 0;
    }
    for(j = 9; j >= 0; j--) {
        if(num[i] < (j + '0') && (i == len - 2 || num[i+1] > (j + '0'))) {
            memmove(num + i + 2, num + i + 1, len - i);
            num[i+1] = j + '0';
            break;
        }
    }
    for(i = 0; i < len; i++) {
        if(num[i] > num[i+1]) {
            printf("NO");
            return 0;
        }
    }
    printf("%s", num);
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string num;
    cin >> num;
    bool modified = false;
    for(int i = 0; i < num.size() - 1; i++) {
        if(num[i] > num[i+1]) {
            for(char d = '9'; d >= '0'; d--) {
                if(d > num[i] && (i == num.size() - 2 || d < num[i+1])) {
                    num.insert(i+1, 1, d);
                    modified = true;
                    break;
                }
            }
        }
        if(modified) break;
    }
    if(is_sorted(num.begin(), num.end()) && modified) {
        cout << num;
    } else {
        cout << "NO";
    }
    return 0;
}
import java.util.Scanner;

public class AddDigitForAscendingOrder {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String num = sc.next();
        boolean modified = false;
        StringBuilder newNum = new StringBuilder(num);
        for(int i = 0; i < num.length() - 1; i++) {
            if(num.charAt(i) > num.charAt(i+1)) {
                for(char d = '9'; d >= '0'; d--) {
                    if(d > num.charAt(i) && (i == num.length() - 2 || d < num.charAt(i+1))) {
                        newNum.insert(i+1, d);
                        modified = true;
                        break;
                    }
                }
            }
            if(modified) break;
        }
        String sortedNum = newNum.toString();
        char[] chars = sortedNum.toCharArray();
        java.util.Arrays.sort(chars);
        String sortedStr = new String(chars);
        if(sortedStr.equals(newNum.toString()) && modified) {
            System.out.println(newNum);
        } else {
            System.out.println("NO");
        }
    }
}

Leave a Reply

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

More posts. You may also be interested in.