The Wayback Machine - https://web.archive.org/web/20210903160833/https://www.geeksforgeeks.org/divide-given-numeric-string-into-at-most-two-increasing-subsequences-which-form-an-increasing-string-upon-concatenation/
Skip to content
Related Articles

Related Articles

Improve Article

Divide given numeric string into at most two increasing subsequences which form an increasing string upon concatenation

  • Last Updated : 03 Sep, 2021

Given a string S consisting of N digits, the task is to partition the string into at most two increasing subsequences such that concatenation of them also forms an increasing string. If it is not possible to do so, then print “-1”.

Examples:

Input: S = “040425524644”
Output: 0022444 44556
Explanation:
One of the possible way to partition the given string S is {“0022444”, “44556”}. Both are in increasing order and the concatenation of them are also increasing “0022444” + ‘”4556″ = “002244444556”.
Therefore, print both the subsequence formed.

Input: S = “123456789”
Output: 123456789

Naive Approach: The simplest approach to solve the problem is to generate all possible subsequences and check whether any two non-overlapping subsequences satisfy the given condition or not. If found to be true, then print both the two subsequences.



Time Complexity: O(N*2N)
Auxiliary Space: O(N)

Efficient Approach: The above approach can be optimized by putting all the values less than X in the first subsequence and all the elements greater than X in the second subsequence and all the elements equal to X is decided on the basis of their position for all X in the range [0, 9]. Follow the steps below to solve the problem:

  • Initialize a variable say, pos to store the position where less than pos are in the first subsequence, greater than pos in the second subsequence, and element equal to pos will be on the subsequence based on their position.
  • Initialize an array res[] that will store which element belongs to which subsequence.
  • Iterate pos in the range [0, 9] and perform the following steps:
    1. Initialize two variables last1 as 0 and last2 as pos which stores the last elements that have been put in subsequence 1 and 2 respectively.
    2. Initialize a boolean variable flag as 1 that stores whether the processed subsequence is valid or not.
    3. Iterate in the range [0, N-1] using i as a variable and perform the following steps:
      • If last2 ≤ S[i], then modify the value of last2 as S[i] and res[i] as 2.
      • Otherwise if last1 ≤ S[i], then modify the value of last1 as S[i] and res[i] as 1.
      • Otherwise, modify the value of the flag as 0.
    4. Check if the value of last is greater than pos, then modify the value of flag as 0.
    5. If the value of flag is 1, then print the array res as the answer and break out of the loop.
  • After completing the above steps, print -1 if no possible subsequence has been found.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check for valid subsequences
void findSubsequence(string str)
{
    int n = str.size();
  
    // Stores which element belongs to
    // which subsequence
    char res[n];
    for (int i = 0; i < n; i++)
        res[i] = 0;
  
    // Check for each pos if a possible
    // subsequence exist or not
    for (int pos = 0; pos <= 9; pos++) {
  
        // Last member of 1 subsequence
        char lst1 = '0';
        bool flag = 1;
  
        // Last Member of 2nd subsequence
        char lst2 = pos + '0';
        for (int i = 0; i < n; i++) {
  
            // Check if current element can
            // go to 2nd subsequence
            if (lst2 <= str[i]) {
                res[i] = '2';
                lst2 = str[i];
            }
  
            // Check if the current elements
            // belongs to first subsequence
            else if (lst1 <= str[i]) {
                res[i] = '1';
                lst1 = str[i];
            }
  
            // If the current element does
            // not belong to any subsequence
            else
                flag = 0;
        }
  
        // Check if last digit of first
        // subsequence is greater than pos
        if (lst1 > pos + '0')
            flag = 0;
  
        // If a subsequence is found,
        // find the subsequences
        if (flag) {
  
            // Stores the resulting
            // subsequences
            string S1 = "";
            string S2 = "";
  
            for (int i = 0; i < n; i++) {
  
                if (res[i] == '1') {
                    S1 += str[i];
                }
                else {
                    S2 += str[i];
                }
            }
  
            // Print the subsequence
            cout << S1 << ' ' << S2 << endl;
            return;
        }
    }
  
    // If no subsequence found, print -1
    cout << "-1";
}
  
// Driver Code
int main()
{
    string S = "040425524644";
    findSubsequence(S);
  
    S = "123456789";
    findSubsequence(S);
  
    return 0;
}
Output:
0022444 44556 
123456789

Time Complexity: O(N)
Auxiliary Space: O(N)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :