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++
// 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; } |
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 |
1005 1014 1015
Recommended Posts:
- Print all the combinations of N elements by changing sign such that their sum is divisible by M
- Print digit's position to be removed to make a number divisible by 6
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Append a digit in the end to make the number equal to the length of the remaining string
- Maximum subset sum such that no two elements in set have same digit in them
- Right most non-zero digit in multiplication of array elements
- Make alphabets using the elements of an array
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Longest subsequence such that adjacent elements have at least one common digit
- Minimum gcd operations to make all array elements one
- Remove elements to make array sorted
- Make all elements of an array equal with the given operation
- Minimum array element changes to make its elements 1 to N
- Minimum steps to make all the elements of the array divisible by 4
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01



