Make the list non-decreasing by changing only one digit of the elements
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 ? 1015
Input: 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++
#include <bits/stdc++.h>
using namespace std;
const int DIGITS = 4, MIN = 1000, MAX = 9999;
int getBest( int prev, int cur)
{
int maximum = max(MIN, prev);
for ( int i = maximum; i <= MAX; i++) {
int cnt = 0;
int a = i;
int b = cur;
for ( int k = 0; k < DIGITS; k++) {
if (a % 10 != b % 10)
cnt += 1;
a /= 10;
b /= 10;
}
if (cnt <= 1)
return i;
}
return -1;
}
void getList( int arr[], int n)
{
vector< int > myList;
int i, cur;
bool possible = true ;
myList.push_back(0);
for (i = 0; i < n; i++) {
cur = arr[i];
myList.push_back(getBest(myList.back(), cur));
if (myList.back() == -1) {
possible = false ;
break ;
}
}
if (possible) {
for (i = 1; i < myList.size(); i++)
cout << myList[i] << " " ;
}
else
cout << "-1" ;
}
int main()
{
int arr[] = { 1095, 1094, 1095 };
int n = sizeof (arr) / sizeof (arr[0]);
getList(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int DIGITS = 4 , MIN = 1000 , MAX = 9999 ;
static int getBest( int prev, int cur)
{
int maximum = Math.max(MIN, prev);
for ( int i = maximum; i <= MAX; i++)
{
int cnt = 0 ;
int a = i;
int b = cur;
for ( int k = 0 ; k < DIGITS; k++)
{
if (a % 10 != b % 10 )
cnt += 1 ;
a /= 10 ;
b /= 10 ;
}
if (cnt <= 1 )
return i;
}
return - 1 ;
}
static void getList( int arr[], int n)
{
Vector<Integer> myList = new Vector<Integer>();
int i, cur;
boolean possible = true ;
myList.add( 0 );
for (i = 0 ; i < n; i++)
{
cur = arr[i];
myList.add(getBest(myList.lastElement(), cur));
if (myList.lastElement() == - 1 )
{
possible = false ;
break ;
}
}
if (possible)
{
for (i = 1 ; i < myList.size(); i++)
System.out.print(myList.get(i) + " " );
}
else
System.out.print( "-1" );
}
public static void main(String[] args)
{
int arr[] = { 1095 , 1094 , 1095 };
int n = arr.length;
getList(arr, n);
}
}
|
Python3
DIGITS = 4 ; MIN = 1000 ; MAX = 9999 ;
def getBest(prev, cur) :
maximum = max ( MIN , prev);
for i in range (maximum, MAX + 1 ) :
cnt = 0 ;
a = i;
b = cur;
for k in range (DIGITS) :
if (a % 10 ! = b % 10 ) :
cnt + = 1 ;
a / / = 10 ;
b / / = 10 ;
if (cnt < = 1 ) :
return i;
return - 1 ;
def getList(arr, n) :
myList = [];
possible = True ;
myList.append( 0 );
for i in range (n) :
cur = arr[i];
myList.append(getBest(myList[ - 1 ], cur));
if (myList[ - 1 ] = = - 1 ) :
possible = False ;
break ;
if (possible) :
for i in range ( 1 , len (myList)) :
print (myList[i], end = " " );
else :
print ( "-1" );
if __name__ = = "__main__" :
arr = [ 1095 , 1094 , 1095 ];
n = len (arr);
getList(arr, n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int DIGITS = 4, MIN = 1000, MAX = 9999;
static int getBest( int prev, int cur)
{
int maximum = Math.Max(MIN, prev);
for ( int i = maximum; i <= MAX; i++)
{
int cnt = 0;
int a = i;
int b = cur;
for ( int k = 0; k < DIGITS; k++)
{
if (a % 10 != b % 10)
cnt += 1;
a /= 10;
b /= 10;
}
if (cnt <= 1)
return i;
}
return -1;
}
static void getList( int []arr, int n)
{
List< int > myList = new List< int >();
int i, cur;
bool possible = true ;
myList.Add(0);
for (i = 0; i < n; i++)
{
cur = arr[i];
myList.Add(getBest(myList[myList.Count - 1], cur));
if (myList[myList.Count - 1] == -1)
{
possible = false ;
break ;
}
}
if (possible)
{
for (i = 1; i < myList.Count; i++)
Console.Write(myList[i] + " " );
}
else
Console.Write( "-1" );
}
public static void Main(String[] args)
{
int []arr = { 1095, 1094, 1095 };
int n = arr.Length;
getList(arr, n);
}
}
|
Javascript
<script>
var DIGITS = 4, MIN = 1000, MAX = 9999;
function getBest(prev, cur)
{
var maximum = Math.max(MIN, prev);
for ( var i = maximum; i <= MAX; i++)
{
var cnt = 0;
var a = i;
var b = cur;
for ( var k = 0; k < DIGITS; k++)
{
if (a % 10 != b % 10)
cnt += 1;
a = parseInt(a / 10);
b = parseInt(b / 10);
}
if (cnt <= 1)
return i;
}
return -1;
}
function getList(arr, n)
{
var myList = [];
var i, cur;
var possible = true ;
myList.push(0);
for (i = 0; i < n; i++)
{
cur = arr[i];
myList.push(getBest(
myList[myList.length - 1], cur));
if (myList[myList.length - 1] == -1)
{
possible = false ;
break ;
}
}
if (possible)
{
for (i = 1; i < myList.length; i++)
document.write( myList[i] + " " );
}
else
document.write( "-1" );
}
var arr = [ 1095, 1094, 1095 ];
var n = arr.length;
getList(arr, n);
</script>
|
Time Complexity: O(n + (MAX * DIGITS)
Auxiliary Space: O(n)
Last Updated :
18 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...