Given an array arr[] of N integers where every element is from the range [1000, 9999]. The task is to make the array non-decreasing by changing only one digit from the array elements and the elements of the resultant list will have to be from the given range of elements. If it is possible to make the array non-decreasing with the given operation then print the updated list otherwise print -1.Â
Examples:Â
Input: arr[] = {1095, 1094, 1095}Â
Output: 1005 1014 1015Â
1095 -> 1005Â
1094 -> 1014Â
1095 -> 1015Â
1005 ? 1014 ? 1015Input: arr[] = {5555, 4444, 3333, 2222, 1111}Â
Output: 1555 2444 3033 3222 4111Â
Approach: The idea is to change a digit of the first element to make it as small as possible. To do that, start from 1000 and store the smallest number for which at most 1 digit needs to be changed. Similarly, for the next element find the smallest possible number, not less than the previous one for which the number of change of digit is at most 1. If the last element doesn’t exceed 9999 then the list is possible.
Below is the implementation of the above approach:Â Â
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; Â
const int DIGITS = 4, MIN = 1000, MAX = 9999; Â
// Function to return the minimum element // from the range [prev, MAX] such that // it differs in at most 1 digit with cur int getBest( int prev, int cur) {     // To start with the value     // we have achieved in the last step     int maximum = max(MIN, prev); Â
    for ( int i = maximum; i <= MAX; i++) {         int cnt = 0; Â
        // Store the value with which the         // current will be compared         int a = i; Â
        // Current value         int b = cur; Â
        // There are at most 4 digits         for ( int k = 0; k < DIGITS; k++) { Â
            // If the current digit differs             // in both the numbers             if (a % 10 != b % 10)                 cnt += 1; Â
            // Eliminate last digits in             // both the numbers             a /= 10;             b /= 10;         } Â
        // If the number of different         // digits is at most 1         if (cnt <= 1)             return i;     } Â
    // If we can't find any number for which     // the number of change is less than or     // equal to 1 then return -1     return -1; } Â
// Function to get the non-decreasing list void getList( int arr[], int n) {     // Creating a vector for the updated list     vector< int > myList; Â
    int i, cur; Â
    // Let's assume that it is possible to     // make the list non-decreasing     bool possible = true ; Â
    myList.push_back(0); Â
    for (i = 0; i < n; i++) { Â
        // Element of the original array         cur = arr[i]; Â
        myList.push_back(getBest(myList.back(), cur)); Â
        // Can't make the list non-decreasing         if (myList.back() == -1) {             possible = false ;             break ;         }     } Â
    // If possible then print the list     if (possible) {         for (i = 1; i < myList.size(); i++)             cout << myList[i] << " " ;     } Â
    else         cout << "-1" ; } Â
// Driver code int main() { Â Â Â Â int arr[] = { 1095, 1094, 1095 }; Â Â Â Â int n = sizeof (arr) / sizeof (arr[0]); Â
    getList(arr, n); Â
    return 0; } |
Java
// Java implementation of the approach import java.util.*; Â
class GFG { static int DIGITS = 4 , MIN = 1000 , MAX = 9999 ; Â
// Function to return the minimum element // from the range [prev, MAX] such that // it differs in at most 1 digit with cur static int getBest( int prev, int cur) {     // To start with the value     // we have achieved in the last step     int maximum = Math.max(MIN, prev); Â
    for ( int i = maximum; i <= MAX; i++)     {         int cnt = 0 ; Â
        // Store the value with which the         // current will be compared         int a = i; Â
        // Current value         int b = cur; Â
        // There are at most 4 digits         for ( int k = 0 ; k < DIGITS; k++)         { Â
            // If the current digit differs             // in both the numbers             if (a % 10 != b % 10 )                 cnt += 1 ; Â
            // Eliminate last digits in             // both the numbers             a /= 10 ;             b /= 10 ;         } Â
        // If the number of different         // digits is at most 1         if (cnt <= 1 )             return i;     } Â
    // If we can't find any number for which     // the number of change is less than or     // equal to 1 then return -1     return - 1 ; } Â
// Function to get the non-decreasing list static void getList( int arr[], int n) {     // Creating a vector for the updated list     Vector<Integer> myList = new Vector<Integer>(); Â
    int i, cur; Â
    // Let's assume that it is possible to     // make the list non-decreasing     boolean possible = true ; Â
    myList.add( 0 ); Â
    for (i = 0 ; i < n; i++)     { Â
        // Element of the original array         cur = arr[i]; Â
        myList.add(getBest(myList.lastElement(), cur)); Â
        // Can't make the list non-decreasing         if (myList.lastElement() == - 1 )         {             possible = false ;             break ;         }     } Â
    // If possible then print the list     if (possible)     {         for (i = 1 ; i < myList.size(); i++)             System.out.print(myList.get(i) + " " );     } Â
    else         System.out.print( "-1" ); } Â
// Driver code public static void main(String[] args) { Â Â Â Â int arr[] = { 1095 , 1094 , 1095 }; Â Â Â Â int n = arr.length; Â
    getList(arr, n); } } Â
// This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach DIGITS = 4 ; MIN = 1000 ; MAX = 9999 ; Â
# Function to return the minimum element # from the range [prev, MAX] such that # it differs in at most 1 digit with cur def getBest(prev, cur) : Â
    # To start with the value     # we have achieved in the last step     maximum = max ( MIN , prev); Â
    for i in range (maximum, MAX + 1 ) :         cnt = 0 ; Â
        # Store the value with which the         # current will be compared         a = i; Â
        # Current value         b = cur; Â
        # There are at most 4 digits         for k in range (DIGITS) : Â
            # If the current digit differs             # in both the numbers             if (a % 10 ! = b % 10 ) :                 cnt + = 1 ; Â
            # Eliminate last digits in             # both the numbers             a / / = 10 ;             b / / = 10 ; Â
        # If the number of different         # digits is at most 1         if (cnt < = 1 ) :             return i; Â
    # If we can't find any number for which     # the number of change is less than or     # equal to 1 then return -1     return - 1 ; Â
# Function to get the non-decreasing list def getList(arr, n) : Â
    # Creating a vector for the updated list     myList = [];          # Let's assume that it is possible to     # make the list non-decreasing     possible = True ; Â
    myList.append( 0 ); Â
    for i in range (n) : Â
        # Element of the original array         cur = arr[i]; Â
        myList.append(getBest(myList[ - 1 ], cur)); Â
        # Can't make the list non-decreasing         if (myList[ - 1 ] = = - 1 ) :             possible = False ;             break ; Â
    # If possible then print the list     if (possible) :         for i in range ( 1 , len (myList)) :             print (myList[i], end = " " );     else :         print ( "-1" ); Â
# Driver code if __name__ = = "__main__" : Â
    arr = [ 1095 , 1094 , 1095 ];     n = len (arr); Â
    getList(arr, n); Â
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; using System.Collections.Generic;Â Â Â Â Â Â Â Â Â Â Â Â Â
class GFG { static int DIGITS = 4, MIN = 1000, MAX = 9999; Â
// Function to return the minimum element // from the range [prev, MAX] such that // it differs in at most 1 digit with cur static int getBest( int prev, int cur) {     // To start with the value     // we have achieved in the last step     int maximum = Math.Max(MIN, prev); Â
    for ( int i = maximum; i <= MAX; i++)     {         int cnt = 0; Â
        // Store the value with which the         // current will be compared         int a = i; Â
        // Current value         int b = cur; Â
        // There are at most 4 digits         for ( int k = 0; k < DIGITS; k++)         { Â
            // If the current digit differs             // in both the numbers             if (a % 10 != b % 10)                 cnt += 1; Â
            // Eliminate last digits in             // both the numbers             a /= 10;             b /= 10;         } Â
        // If the number of different         // digits is at most 1         if (cnt <= 1)             return i;     } Â
    // If we can't find any number for which     // the number of change is less than or     // equal to 1 then return -1     return -1; } Â
// Function to get the non-decreasing list static void getList( int []arr, int n) {     // Creating a vector for the updated list     List< int > myList = new List< int >(); Â
    int i, cur; Â
    // Let's assume that it is possible to     // make the list non-decreasing     bool possible = true ; Â
    myList.Add(0); Â
    for (i = 0; i < n; i++)     { Â
        // Element of the original array         cur = arr[i]; Â
        myList.Add(getBest(myList[myList.Count - 1], cur)); Â
        // Can't make the list non-decreasing         if (myList[myList.Count - 1] == -1)         {             possible = false ;             break ;         }     } Â
    // If possible then print the list     if (possible)     {         for (i = 1; i < myList.Count; i++)             Console.Write(myList[i] + " " );     } Â
    else         Console.Write( "-1" ); } Â
// Driver code public static void Main(String[] args) { Â Â Â Â int []arr = { 1095, 1094, 1095 }; Â Â Â Â int n = arr.Length; Â
    getList(arr, n); } } Â
// This code is contributed by 29AjayKumar |
Javascript
<script> Â
// Javascript implementation of the approach var DIGITS = 4, MIN = 1000, MAX = 9999; Â
// Function to return the minimum element // from the range [prev, MAX] such that // it differs in at most 1 digit with cur function getBest(prev, cur) {          // To start with the value     // we have achieved in the last step     var maximum = Math.max(MIN, prev); Â
    for ( var i = maximum; i <= MAX; i++)     {         var cnt = 0;                  // Store the value with which the         // current will be compared         var a = i; Â
        // Current value         var b = cur; Â
        // There are at most 4 digits         for ( var k = 0; k < DIGITS; k++)         {                          // If the current digit differs             // in both the numbers             if (a % 10 != b % 10)                 cnt += 1; Â
            // Eliminate last digits in             // both the numbers             a = parseInt(a / 10);             b = parseInt(b / 10);         } Â
        // If the number of different         // digits is at most 1         if (cnt <= 1)             return i;     } Â
    // If we can't find any number for which     // the number of change is less than or     // equal to 1 then return -1     return -1; } Â
// Function to get the non-decreasing list function getList(arr, n) {          // Creating a vector for the updated list     var myList = []; Â
    var i, cur; Â
    // Let's assume that it is possible to     // make the list non-decreasing     var possible = true ; Â
    myList.push(0); Â
    for (i = 0; i < n; i++)     {                  // Element of the original array         cur = arr[i]; Â
        myList.push(getBest(             myList[myList.length - 1], cur)); Â
        // Can't make the list non-decreasing         if (myList[myList.length - 1] == -1)         {             possible = false ;             break ;         }     } Â
    // If possible then print the list     if (possible)     {         for (i = 1; i < myList.length; i++)             document.write( myList[i] + " " );     }     else         document.write( "-1" ); } Â
// Driver code var arr = [ 1095, 1094, 1095 ]; var n = arr.length; Â
getList(arr, n); Â
// This code is contributed by itsok Â
</script> |
1005 1014 1015
Â
Time Complexity: O(n + (MAX * DIGITS)
Auxiliary Space: O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!