Count the number of contiguous increasing and decreasing subsequences in a sequence - GeeksforGeeks

13 May, 2021
For a given distinct integer sequence of size N, the task is to count the number of contiguous increasing subsequence and contiguous decreasing subsequence in this sequence.Examples:  
Input: arr[] = { 80, 50, 60, 70, 40 } Output: 1 2 Explanation: The only one increasing subsequence is (50, 60, 70) and two decreasing subsequences are (80, 50) and (70, 40).Input: arr[] = { 10, 20, 23, 12, 5, 4, 61, 67, 87, 9 } Output: 2 2 Explanation: The increasing subsequences are (10, 20, 23) and (4, 61, 67, 87) whereas the decreasing subsequences are (23, 12, 5, 4) and (87, 9).  
 
Approach: The idea behind solving this problem is to use two arrays which keeps the track of increasing or decreasing indices based on next elements.  
Define two arrays max and min, such that the index of an element of the array is stored. For the first element of the array, if it is greater than its next element, it’s index is stored in the max array, else it is stored in the min array and so on.For the last element of the array, if it is greater than the previous element, it’s index is stored in the max array, else it is stored in min array.After this, all the maximal contiguous increasing subsequences are matched from (min to max), while all the maximal contiguous decreasing subsequences are matched from (max to min).Finally, if the index of the first element of array is stored in the first index of min array, then the number of maximal contiguous increasing subsequence possible is size of min array and maximal contiguous decreasing subsequences possible is (size of max array – 1).If the index of the first element of the array is stored in the first index of the max array, then the number of maximal contiguous increasing subsequence possible is(size of the max array -1) and maximal contiguous decreasing subsequences possible is the size of min array.
Define two arrays max and min, such that the index of an element of the array is stored. For the first element of the array, if it is greater than its next element, it’s index is stored in the max array, else it is stored in the min array and so on.
For the last element of the array, if it is greater than the previous element, it’s index is stored in the max array, else it is stored in min array.
After this, all the maximal contiguous increasing subsequences are matched from (min to max), while all the maximal contiguous decreasing subsequences are matched from (max to min).
Finally, if the index of the first element of array is stored in the first index of min array, then the number of maximal contiguous increasing subsequence possible is size of min array and maximal contiguous decreasing subsequences possible is (size of max array – 1).
If the index of the first element of the array is stored in the first index of the max array, then the number of maximal contiguous increasing subsequence possible is(size of the max array -1) and maximal contiguous decreasing subsequences possible is the size of min array.
Below is the implementation of the above approach.  
C++
Java
Python3
C#
Javascript
// C++ implementation of the above approach. #include <bits/stdc++.h>using namespace std; // Function to count the number of maximal contiguous// increasing and decreasing subsequencesvoid numOfSubseq(int arr[], int n) {     int i, inc_count, dec_count;    int max[n], min[n];     // k2, k1 are used to store the    // count of max and min array     int k1 = 0, k2 = 0;     // Comparison to store the index of    // first element of array    if (arr[0] < arr[1])        min[k1++] = 0;    else        max[k2++] = 0;     // Comparison to store the index    // from second to second last    // index of array    for (i = 1; i < n - 1; i++) {        if (arr[i] < arr[i - 1]            && arr[i] < arr[i + 1])            min[k1++] = i;         if (arr[i] > arr[i - 1]            && arr[i] > arr[i + 1])            max[k2++] = i;    }     // Comparison to store the index    // of last element of array    if (arr[n - 1] < arr[n - 2])        min[k1++] = n - 1;    else        max[k2++] = n - 1;     // Count of number of maximal contiguous    // increasing and decreasing subsequences    if (min[0] == 0) {        inc_count = k2;        dec_count = k1 - 1;    }    else {        inc_count = k2 - 1;        dec_count = k1;    }     cout << "Increasing Subsequence Count: "         << inc_count << "\n";    cout << "Decreasing Subsequence Count: "         << dec_count << "\n";} // Driver program to test aboveint main(){    int arr[] = { 12, 8, 11, 13, 10, 15, 14, 16, 20 };    int n = sizeof(arr) / sizeof(arr[0]);    numOfSubseq(arr, n);    return 0;}
// Java implementation of the above approach.class GFG{         // Function to count the number of maximal contiguous    // increasing and decreasing subsequences    static void numOfSubseq(int arr[], int n)         {             int i, inc_count, dec_count;        int max[] = new int[n];        int min[] = new int[n];             // k2, k1 are used to store the        // count of max and min array             int k1 = 0, k2 = 0;             // Comparison to store the index of        // first element of array        if (arr[0] < arr[1])            min[k1++] = 0;        else            max[k2++] = 0;             // Comparison to store the index        // from second to second last        // index of array        for (i = 1; i < n - 1; i++)        {            if (arr[i] < arr[i - 1]                && arr[i] < arr[i + 1])                min[k1++] = i;                 if (arr[i] > arr[i - 1]                && arr[i] > arr[i + 1])                max[k2++] = i;        }             // Comparison to store the index        // of last element of array        if (arr[n - 1] < arr[n - 2])            min[k1++] = n - 1;        else            max[k2++] = n - 1;             // Count of number of maximal contiguous        // increasing and decreasing subsequences        if (min[0] == 0)        {            inc_count = k2;            dec_count = k1 - 1;        }        else        {            inc_count = k2 - 1;            dec_count = k1;        }             System.out.println("Increasing Subsequence" +                            " Count: " + inc_count) ;        System.out.println("Decreasing Subsequence" +                            " Count: " + dec_count) ;    }         // Driver code    public static void main (String[] args)    {        int arr[] = { 12, 8, 11, 13, 10, 15, 14, 16, 20 };        int n = arr.length;        numOfSubseq(arr, n);    }} // This code is contributed by AnkitRai01
# Python3 implementation of the above approach. # Function to count the number of maximal contiguous# increasing and decreasing subsequencesdef numOfSubseq(arr, n):    i, inc_count, dec_count = 0, 0, 0;    max = [0]*n;    min = [0]*n;     # k2, k1 are used to store the    # count of max and min array    k1 = 0;    k2 = 0;     # Comparison to store the index of    # first element of array    if (arr[0] < arr[1]):        min[k1] = 0;        k1 += 1;    else:        max[k2] = 0;        k2 += 1;         # Comparison to store the index    # from second to second last    # index of array    for i in range(1, n-1):        if (arr[i] < arr[i - 1] and arr[i] < arr[i + 1]):            min[k1] = i;            k1 += 1;                 if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]):            max[k2] = i;            k2 += 1;     # Comparison to store the index    # of last element of array    if (arr[n - 1] < arr[n - 2]):        min[k1] = n - 1;        k1 += 1;    else:        max[k2] = n - 1;        k2 += 1;         # Count of number of maximal contiguous    # increasing and decreasing subsequences    if (min[0] == 0):        inc_count = k2;        dec_count = k1 - 1;    else:        inc_count = k2 - 1;        dec_count = k1;         print("Increasing Subsequence Count: " , inc_count);    print("Decreasing Subsequence Count: " , dec_count); # Driver codeif __name__ == '__main__':    arr = [ 12, 8, 11, 13, 10, 15, 14, 16, 20 ];    n = len(arr);    numOfSubseq(arr, n); # This code is contributed by 29AjayKumar
// C# implementation of the above approach.using System; class GFG{         // Function to count the number of maximal contiguous// increasing and decreasing subsequencesstatic void numOfSubseq(int []arr, int n) {     int i, inc_count, dec_count;    int []max = new int[n];    int []min = new int[n];     // k2, k1 are used to store the    // count of max and min array    int k1 = 0, k2 = 0;     // Comparison to store the index of    // first element of array    if (arr[0] < arr[1])        min[k1++] = 0;    else        max[k2++] = 0;     // Comparison to store the index    // from second to second last    // index of array    for (i = 1; i < n - 1; i++)    {        if (arr[i] < arr[i - 1]            && arr[i] < arr[i + 1])            min[k1++] = i;         if (arr[i] > arr[i - 1]            && arr[i] > arr[i + 1])            max[k2++] = i;    }     // Comparison to store the index    // of last element of array    if (arr[n - 1] < arr[n - 2])        min[k1++] = n - 1;    else        max[k2++] = n - 1;     // Count of number of maximal contiguous    // increasing and decreasing subsequences    if (min[0] == 0)    {        inc_count = k2;        dec_count = k1 - 1;    }    else    {        inc_count = k2 - 1;        dec_count = k1;    }     Console.WriteLine("Increasing Subsequence" +                        " Count: " + inc_count) ;    Console.WriteLine("Decreasing Subsequence" +                        " Count: " + dec_count) ;}     // Driver codestatic public void Main (){    int []arr = { 12, 8, 11, 13, 10, 15, 14, 16, 20 };    int n = arr.Length;    numOfSubseq(arr, n);}} // This code is contributed by ajit.
<script>    // Javascript implementation of the above approach.         // Function to count the number of maximal contiguous    // increasing and decreasing subsequences    function numOfSubseq(arr, n)     {         let i, inc_count, dec_count;        let max = new Array(n);        max.fill(0);        let min = new Array(n);        min.fill(0);         // k2, k1 are used to store the        // count of max and min array        let k1 = 0, k2 = 0;         // Comparison to store the index of        // first element of array        if (arr[0] < arr[1])            min[k1++] = 0;        else            max[k2++] = 0;         // Comparison to store the index        // from second to second last        // index of array        for (i = 1; i < n - 1; i++)        {            if (arr[i] < arr[i - 1]                && arr[i] < arr[i + 1])                min[k1++] = i;             if (arr[i] > arr[i - 1]                && arr[i] > arr[i + 1])                max[k2++] = i;        }         // Comparison to store the index        // of last element of array        if (arr[n - 1] < arr[n - 2])            min[k1++] = n - 1;        else            max[k2++] = n - 1;         // Count of number of maximal contiguous        // increasing and decreasing subsequences        if (min[0] == 0)        {            inc_count = k2;            dec_count = k1 - 1;        }        else        {            inc_count = k2 - 1;            dec_count = k1;        }         document.write("Increasing Subsequence" +                            " Count: " + inc_count + "</br>") ;        document.write("Decreasing Subsequence" +                            " Count: " + dec_count + "</br>") ;    }         let arr = [ 12, 8, 11, 13, 10, 15, 14, 16, 20 ];    let n = arr.length;    numOfSubseq(arr, n); // This code is contributed by suresh07.</script>
Increasing Subsequence Count: 3
Decreasing Subsequence Count: 3
 
Time Complexity: O(N) 
ankthon
jit_t
29AjayKumar
suresh07
subsequence
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Linked List vs Array
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Python | Using 2D arrays/lists the right way
Search an element in a sorted and rotated array