Given two numeric strings N and K (K ≤ N), where digits of K are in non-decreasing order, the task is to rearrange digits of N such that K does not appear as a subsequence in N. If it is not possible to obtain such a permutation, print “-1”. Otherwise print any such valid permutation of N.
Examples:
Input: N = 93039373637, K = 339
Output: 97093736333Input: N=965, K=55
Output: 965
Naive Approach: The simplest approach is to generate every permutation of N and check for each permutation, if K appears as a subsequence in it or not. If found to be false for any permutation, print that permutation.
Time Complexity: O(N*N!)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the observation that the digits of K are in non-decreasing order. Therefore, rearrange the digits of N in decreasing order by sorting. Follow the steps below to solve the problem:
- Store the frequency of digits of N and K in HashMaps M1 and M2, respectively.
- Iterate over the range [0, 9] using a variable, say i, to check if K has any digit with more occurrences than in N. If M2[i] > M1[i], then print N and return.
- Again, iterate over the range [0, 9] using a variable i to check if K contains only 1 distinct digit. If M2[i] is the same as length(K), then print “-1” and return.
- For all other cases, sort N in decreasing order and then print N.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find a permutation of // number N such that the number K does // not appear as a subsequence in N void findArrangement(string n, string k) { // Stores frequency of digits of N unordered_map<char, int> um1; for (int i = 0; i < n.size(); i++) { um1[n[i]]++; } // Stores frequency of digits of K unordered_map<char, int> um2; for (int i = 0; i < k.size(); i++) { um2[k[i]]++; } // Check if any digit in K has // more occurrences than in N for (int i = 0; i <= 9; i++) { char ch = '0' + i; // If true, print N and return if (um2[ch] > um1[ch]) { cout << n; return; } } // Check if K contains only a // single distinct digit for (int i = 0; i <= 9; i++) { char ch = '0' + i; // If true, print -1 and return if (um2[ch] == k.size()) { cout << -1; return; } } // For all other cases, sort N // in decreasing order sort(n.begin(), n.end(), greater<char>()); // Print the value of N cout << n; } // Driver Code int main() { string N = "93039373637", K = "339"; // Function Call findArrangement(N, K); return 0; } |
99776333330
Time Complexity: O(L*log (L)), where L is the size of the string N
Auxiliary Space: O(L)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Rearrange an array to make similar indexed elements different from that of another array
- Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Find smallest number with given number of digits and sum of digits under given constraints
- Length of smallest substring of a given string which contains another string as subsequence
- Check if a given number can be represented in given a no. of digits in any base
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Given a number N in decimal base, find number of its digits in any base (base b)
- Given a large number, check if a subsequence of digits is divisible by 8
- Number of sub-strings which are anagram of any sub-string of another string
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Find length of longest subsequence of one string which is substring of another string
- Maximum length prefix of one string that occurs as subsequence in another
- Given a number N in decimal base, find the sum of digits in any base B
- Remove repeated digits in a given number
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.

