Divide array into increasing and decreasing subsequence without changing the order

Given a merged sequence which consists of two sequences which got merged, one of them was strictly increasing and the other was strictly decreasing. Elements of increasing sequence were inserted between elements of the decreasing one without changing the order.

Sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences:
[10, 1, 3, 4, 2, 4], [1, 3, 4, 10, 4, 2].

The following sequence cannot be the result of these insertions:
[1, 10, 4, 4, 3, 2] because the order of elements in the increasing sequence was changed.



Given a merged sequence, the task is to find any two suitable initial sequences, one of them should be strictly increasing, and another should be strictly decreasing.

Note: An empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

Examples:

Input: arr[] = {5, 1, 3, 6, 8, 2, 9, 0, 10}
Output: [1, 3, 6, 8, 9, 10] [5, 2, 0]

Input: arr[] = {1, 2, 4, 0, 2}
Output: -1
No such sequences possible.

Method 1: We can modify Longest Increasing Sequence) and solve the required problem. It will take O(nlogn) time.

Method 2: We can also solve this problem only in a single traversal. The Idea used here is that maintain two sorted arrays.
For a new element x,

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print strictly increasing and
// strictly decreasing sequence if possible
void Find_Sequence(int arr[], int n)
{
    // Arrays to store strictly increasing and
    // decreasing sequence
    vector<int> inc_arr, dec_arr;
  
    // Initializing last element of both sequence
    int flag = 0;
    long inc = -1, dec = 1e7;
  
    // Iterating through the array
    for (int i = 0; i < n; i++)
    {
        // If current element can be appended
        // to both the sequences
        if (inc < arr[i] && arr[i] < dec)
        {
            // If next element is greater than
            // the current element
            // Then append it to the strictly
            // increasing array
            if (arr[i] < arr[i + 1])
            {
                inc = arr[i];
                inc_arr.emplace_back(arr[i]);
            }
  
            // Otherwise append it to the
            // strictly decreasing array
            else
            {
                dec = arr[i];
                dec_arr.emplace_back(arr[i]);
            }
        }
          
        // If current element can be appended
        // to the increasing sequence only
        else if (inc < arr[i])
        {
            inc = arr[i];
            inc_arr.emplace_back(arr[i]);
        }
          
        // If current element can be appended
        // to the decreasing sequence only
        else if (dec > arr[i])
        {
            dec = arr[i];
            dec_arr.emplace_back(arr[i]);
        }
          
        // Else we can not make such sequences
        // from the given array
        else
        {
            cout << -1 << endl;
            flag = 1;
            break;
        }
    }
      
    // Print the required sequences
    if (!flag)
    {
        for (auto i = inc_arr.begin(); 
                  i != inc_arr.end(); i++)
            cout << *i << " ";
        cout << endl;
  
        for (auto i = dec_arr.begin(); 
                  i != dec_arr.end(); i++)
            cout << *i << " ";
        cout << endl;
    }
}
  
// Driver code
int main()
{
    int arr[] = { 5, 1, 3, 6, 8, 2, 9, 0, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    Find_Sequence(arr, n);
}
  
// This code is contributed by sanjeev2552
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
    // Function to print strictly increasing and
    // strictly decreasing sequence if possible
    static void Find_Sequence(int[] arr, int n) 
    {
  
        // Arrays to store strictly increasing and
        // decreasing sequence
        Vector<Integer> inc_arr = new Vector<>(), 
                        dec_arr = new Vector<>();
  
        // Initializing last element of both sequence
        int flag = 0;
        long inc = -1, dec = (long) 1e7;
  
        // Iterating through the array
        for (int i = 0; i < n; i++) 
        {
  
            // If current element can be appended
            // to both the sequences
            if (inc < arr[i] && arr[i] < dec)
            {
  
                // If next element is greater than
                // the current element
                // Then append it to the strictly
                // increasing array
                if (arr[i] < arr[i + 1]) 
                {
                    inc = arr[i];
                    inc_arr.add(arr[i]);
                }
  
                // Otherwise append it to the
                // strictly decreasing array
                else 
                {
                    dec = arr[i];
                    dec_arr.add(arr[i]);
                }
            }
  
            // If current element can be appended
            // to the increasing sequence only
            else if (inc < arr[i])
            {
                inc = arr[i];
                inc_arr.add(arr[i]);
            }
  
            // If current element can be appended
            // to the decreasing sequence only
            else if (dec > arr[i])
            {
                dec = arr[i];
                dec_arr.add(arr[i]);
            }
  
            // Else we can not make such sequences
            // from the given array
            else
            {
                System.out.println(-1);
                flag = 1;
                break;
            }
        }
  
        // Print the required sequences
        if (flag == 0
        {
            for (int i : inc_arr)
                System.out.print(i + " ");
            System.out.println();
  
            for (int i : dec_arr)
                System.out.print(i + " ");
            System.out.println();
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 5, 1, 3, 6, 8, 2, 9, 0, 10 };
        int n = arr.length;
        Find_Sequence(arr, n);
    }
}
  
// This code is contributed by
// sanjeev2552
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to print strictly increasing and
# strictly decreasing sequence if possible
def Find_Sequence(array, n):
  
    # Arrays to store strictly increasing and
    # decreasing sequence 
    inc_arr, dec_arr =[], []
  
    # Initializing last element of both sequence
    inc, dec = -1, 1e7
  
    # Iterating through the array
    for i in range(n):
  
        # If current element can be appended 
        # to both the sequences
        if inc < array[i] < dec:
  
            # If next element is greater than 
            # the current element 
            # Then append it to the strictly 
            # increasing array 
            if array[i] < array[i + 1]:
                inc = array[i]
                inc_arr.append(array[i])
  
            # Otherwise append it to the 
            # strictly decreasing array
            else:
                dec = array[i]
                dec_arr.append(array[i])
  
        # If current element can be appended 
        # to the increasing sequence only
        elif inc < array[i]:
            inc = array[i]
            inc_arr.append(array[i])
  
        # If current element can be appended 
        # to the decreasing sequence only
        elif dec > array[i]:
            dec = array[i]
            dec_arr.append(array[i])
  
        # Else we can not make such sequences 
        # from the given array
        else:
            print('-1')
            break
  
    # Print the required sequences
    else:
        print(inc_arr, dec_arr)
  
# Driver code
arr = [5, 1, 3, 6, 8, 2, 9, 0, 10]
n = len(arr)
Find_Sequence(arr, n)
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Php implementation of the approach 
  
// Function to print strictly increasing and
// strictly decreasing sequence if possible 
function Find_Sequence($arr, $n)
{
  
    // Arrays to store strictly increasing and
    // decreasing sequence 
    $inc_arr = array(); $dec_arr = array(); 
  
    // Initializing last element of both sequence 
    $inc = -1; $dec = 1e7;
  
    // Iterating through the array
    for ($i = 0; $i < $n ; $i++)
    {
  
        // If current element can be appended 
        // to both the sequences 
        if ($inc < $arr[$i] && $arr[$i] < $dec)
        {
  
            // If next element is greater than 
            // the current element 
            // Then append it to the strictly 
            // increasing array
            if ($arr[$i] < $arr[$i + 1])
            {
                $inc = $arr[$i];
                array_push($inc_arr, $arr[$i]);
            }
  
            // Otherwise append it to the 
            // strictly decreasing array
            else
            {
                $dec = $arr[$i];
                array_push($dec_arr, $arr[$i]);
            }
        }
          
        // If current element can be appended 
        // to the increasing sequence only 
        else if ($inc < $arr[$i]) 
        {
            $inc = $arr[$i]; 
            array_push($inc_arr, $arr[$i]);
        }
  
        // If current element can be appended 
        // to the decreasing sequence only 
        else if($dec > $arr[$i])
        {
            $dec = $arr[$i];
            array_push($dec_arr, $arr[$i]); 
        }
  
        // Else we can not make such sequences 
        // from the given array
        else
        {
            echo '-1'
            break;
        }
    }
      
    // Print the required sequences 
    print_r($inc_arr);
    print_r($dec_arr);
}
  
// Driver code 
$arr = array(5, 1, 3, 6, 8, 2, 9, 0, 10);
$n = count($arr);
Find_Sequence($arr, $n);
  
// This code is contributed by Ryuga
?>
chevron_right

Output:
[1, 3, 6, 8, 9, 10] [5, 2, 0]



Check out this Author's contributed articles.

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, sanjeev2552



Article Tags :
Practice Tags :