📜  打印数组中所有最大递增的连续子数组

📅  最后修改于: 2021-04-30 03:30:15             🧑  作者: Mango

给定数组arr [] ,任务是在给定数组中找到所有最大的连续递增子数组。

例子

方法:遍历数组,然后将每个元素与其下一个相邻元素进行比较,以使该元素(如果小于下一个元素)进行打印,否则在下一行单独打印。

下面是上述方法的实现。

C++
// C++ Implementation to print all the
// Maximal Increasing Sub-array of array
#include 
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;
}


Java
// 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


Python3
# 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


C#
// 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


输出:
9
8 11 13
10 15
14 16 20
5

时间复杂度: O(n)