Print all maximal increasing contiguous sub-array in an array

Given an array arr[], the task is to find all the maximal contiguous increasing subarray in a given array.

Examples:

Input:
arr[] = { 80, 50, 60, 70, 40, 50, 80, 70 }
Output:
80
50 60 70
40 50 80
70

Input:
arr[] = { 10, 20, 23, 12, 5, 4, 61, 67, 87, 9 }
Output:
10 20 23
12
5
4 61 67 87
9

Approach: Iterate over the array and compare every element with its next neighboring element such that, if it is less than the next element, print it, else print it individually in the next line.



Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Implementation to print all the
// Maximal Increasing Sub-array of array
#include <bits/stdc++.h>
using namespace std;
  
// Function to print each of maximal
// contiguous increasing subarray
void printmaxSubseq(int arr[], int n)
{
    int i;
  
    // Loop to iterate through the array and print
    // the maximal contiguous increasing subarray.
    for (i = 0; i < n; i++) {
        // Condition to check whether the element at i, is
        // greater than its next neighbouring element or not.
        if (arr[i] < arr[i + 1])
            cout << arr[i] << " ";
        else
            cout << arr[i] << "\n";
    }
}
  
// Driver function
int main()
{
    int arr[] = { 9, 8, 11, 13, 10, 15, 14, 16, 20, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printmaxSubseq(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Implementation to print all the
// Maximal Increasing Sub-array of array
import java.util.*;
  
class GFG
{
  
// Function to print each of maximal
// contiguous increasing subarray
static void printmaxSubseq(int arr[], int n)
{
    int i;
  
    // Loop to iterate through the array and print
    // the maximal contiguous increasing subarray.
    for (i = 0; i < n ; i++) 
    {
        // Condition to check whether the element at i, is
        // greater than its next neighbouring element or not.
        if (i + 1 < n && arr[i] < arr[i + 1])
            System.out.print(arr[i] + " ");
        else
            System.out.print(arr[i] + "\n");
    }
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 9, 8, 11, 13, 10, 15, 14, 16, 20, 5 };
    int n = arr.length;
    printmaxSubseq(arr, n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Implementation to print all the 
# Maximal Increasing Sub-array of array 
  
# Function to print each of maximal 
# contiguous increasing subarray 
def printmaxSubseq(arr, n) :
      
    # Loop to iterate through the array and print 
    # the maximal contiguous increasing subarray. 
    for i in range(n - 1) :
          
        # Condition to check whether the element at i, is 
        # greater than its next neighbouring element or not. 
        if (arr[i] < arr[i + 1]) :
            print(arr[i], end = " "); 
        else :
            print(arr[i]);
              
    print(arr[n - 1]);
      
# Driver function 
if __name__ == "__main__"
  
    arr = [ 9, 8, 11, 13, 10, 15, 14, 16, 20, 5 ]; 
    n = len(arr); 
    printmaxSubseq(arr, n); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Implementation to print all the 
// Maximal Increasing Sub-array of array 
using System;
  
class GFG 
      
    // Function to print each of maximal 
    // contiguous increasing subarray 
    static void printmaxSubseq(int []arr, int n) 
    
        int i; 
      
        // Loop to iterate through the array and print 
        // the maximal contiguous increasing subarray. 
        for (i = 0; i < n ; i++) 
        
            // Condition to check whether the element at i, is 
            // greater than its next neighbouring element or not. 
            if (i + 1 < n && arr[i] < arr[i + 1]) 
                Console.Write(arr[i] + " "); 
            else
                Console.WriteLine(arr[i]); 
        
    
      
    // Driver code 
    public static void Main() 
    
        int []arr = { 9, 8, 11, 13, 10, 15, 14, 16, 20, 5 }; 
        int n = arr.Length; 
        printmaxSubseq(arr, n); 
    
  
// This code is contributed by AnkitRai01

chevron_right


Output:

9
8 11 13
10 15
14 16 20
5

Time Complexity : O(n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

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 : 29AjayKumar, AnkitRai01