Given an integer X, the task is to partition its digit into two groups either A or B such that the sequence of digits is non decreasing when all the digits of group A are arranged followed by all the digits of group B from left to right as they appear in X. Print -1 if no such partition is possible or else return a string S of the same length as X where S[i] is either A or B.
Input: X = 5164
The digits in group A are 1 and 4 and in group B are 5 and 6. This partition satisfies the condition as when all the digits of A are written and then all the digits of B are written as they appear in X from left to right, the sequence is non-decreasing, i.e., 1456.
Input: X = 654
No such partition is possible that may result in non-decreasing sequence. For example, if we consider BBA and write the sequence, it turns out 465. Similarly for BAA, it is 546. and for AAA it is 654.
- Let us assume a digit D so that all digits less than D goes to group A and all the digits greater than D goes to group B.
- For the digits equal to D, it will go to group A only if any digit of group B is present before it otherwise it will go to group B.
- After such partition, check if it forms a non decreasing sequence. Otherwise try for some different D.
- The value of D ranges from 0 to 9.
Below is the implementation of the above approach:
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.
- Smallest index in the given array that satisfies the given condition
- Maximum size of sub-array that satisfies the given condition
- Find the lexicographically smallest string which satisfies the given condition
- Find permutation of first N natural numbers that satisfies the given condition
- Subarray permutation that satisfies the given condition
- Find an integer in the given range that satisfies the given conditions
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Generate an array B from the given array A which satisfies the given conditions
- Generate an array of size K which satisfies the given conditions
- Find an array of size N that satisfies the given conditions
- Find count of numbers from 0 to n which satisfies the given equation for a value K
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- 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
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Partition N into M parts such that difference between Max and Min part is smallest
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Partitions possible such that the minimum element divides all the other elements of the partition
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.