Subarreglo creciente más largo – Part 1

Dada una array que contiene n números. El problema es encontrar la longitud del subarreglo contiguo más largo tal que cada elemento del subarreglo sea estrictamente mayor que su elemento anterior en el mismo subarreglo. La complejidad del tiempo debe ser O(n).

Ejemplos:  

Input : arr[] = {5, 6, 3, 5, 7, 8, 9, 1, 2}
Output : 5
The subarray is {3, 5, 7, 8, 9}

Input : arr[] = {12, 13, 1, 5, 4, 7, 8, 10, 10, 11}
Output : 4
The subarray is {4, 7, 8, 10} 

Algoritmo:  

lenOfLongIncSubArr(arr, n)
    Declare max = 1, len = 1
    for i = 1 to n-1
    if arr[i] > arr[i-1]
        len++
    else
        if max < len
            max = len
        len = 1
    if max < len
        max = len
    return max 

Implementación:

C++

// C++ implementation to find the length of
// longest increasing contiguous subarray
#include <bits/stdc++.h>
 
using namespace std;
 
// function to find the length of longest increasing
// contiguous subarray
int lenOfLongIncSubArr(int arr[], int n)
{
    // 'max' to store the length of longest
    // increasing subarray
    // 'len' to store the lengths of longest
    // increasing subarray at different
    // instants of time
    int max = 1, len = 1;
     
    // traverse the array from the 2nd element
    for (int i=1; i<n; i++)
    {
        // if current element if greater than previous
        // element, then this element helps in building
        // up the previous increasing subarray encountered
        // so far
        if (arr[i] > arr[i-1])
            len++;
        else
        {
            // check if 'max' length is less than the length
            // of the current increasing subarray. If true,
            // then update 'max'
            if (max < len)   
                max = len;
                 
            // reset 'len' to 1 as from this element
            // again the length of the new increasing
            // subarray is being calculated   
            len = 1;   
        }   
    }
     
    // comparing the length of the last
    // increasing subarray with 'max'
    if (max < len)
        max = len;
     
    // required maximum length
    return max;
}
 
// Driver program to test above
int main()
{
    int arr[] = {5, 6, 3, 5, 7, 8, 9, 1, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Length = "
         << lenOfLongIncSubArr(arr, n);
    return 0;    
}

Java

// JAVA Code to find length of
// Longest increasing subarray
import java.util.*;
 
class GFG {
     
    // function to find the length of longest
    // increasing contiguous subarray
    public static int lenOfLongIncSubArr(int arr[],
                                            int n)
    {
        // 'max' to store the length of longest
        // increasing subarray
        // 'len' to store the lengths of longest
        // increasing subarray at different
        // instants of time
        int max = 1, len = 1;
          
        // traverse the array from the 2nd element
        for (int i=1; i<n; i++)
        {
            // if current element if greater than
            // previous element, then this element
            // helps in building up the previous
            // increasing subarray encountered
            // so far
            if (arr[i] > arr[i-1])
                len++;
            else
            {
                // check if 'max' length is less
                // than the length of the current
                // increasing subarray. If true,
                // than update 'max'
                if (max < len)   
                    max = len;
                      
                // reset 'len' to 1 as from this
                // element again the length of the
                // new increasing subarray is being
                // calculated   
                len = 1;   
            }   
        }
          
        // comparing the length of the last
        // increasing subarray with 'max'
        if (max < len)
            max = len;
          
        // required maximum length
        return max;
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
         int arr[] = {5, 6, 3, 5, 7, 8, 9, 1, 2};
            int n = arr.length;
            System.out.println("Length = " +
                      lenOfLongIncSubArr(arr, n));
         
        }
    }
   
// This code is contributed by Arnav Kr. Mandal.

Python3

# Python 3 implementation to find the length of
# longest increasing contiguous subarray
 
  
# function to find the length of longest
# increasing contiguous subarray
def lenOfLongIncSubArr(arr, n) :
 
    # 'max' to store the length of longest
    # increasing subarray
    # 'len' to store the lengths of longest
    # increasing subarray at different
    # instants of time
    m = 1
    l = 1
      
    # traverse the array from the 2nd element
    for i in range(1, n) :
 
        # if current element if greater than previous
        # element, then this element helps in building
        # up the previous increasing subarray encountered
        # so far
        if (arr[i] > arr[i-1]) :
            l =l + 1
        else :
 
            # check if 'max' length is less than the length
            # of the current increasing subarray. If true,
            # then update 'max'
            if (m < l)  :
                m = l
 
            # reset 'len' to 1 as from this element
            # again the length of the new increasing
            # subarray is being calculated   
            l = 1
         
         
    # comparing the length of the last
    # increasing subarray with 'max'
    if (m < l) :
        m = l
      
    # required maximum length
    return m
 
# Driver program to test above
 
arr = [5, 6, 3, 5, 7, 8, 9, 1, 2]
n = len(arr)
print("Length = ", lenOfLongIncSubArr(arr, n))
 
 
# This code is contributed
# by Nikita Tiwari.

C#

// C# Code to find length of
// Longest increasing subarray
using System;
 
class GFG {
 
    // function to find the length of longest
    // increasing contiguous subarray
    public static int lenOfLongIncSubArr(int[] arr,
                                             int n)
    {
        // 'max' to store the length of longest
        // increasing subarray
        // 'len' to store the lengths of longest
        // increasing subarray at different
        // instants of time
        int max = 1, len = 1;
 
        // traverse the array from the 2nd element
        for (int i = 1; i < n; i++) {
             
            // if current element if greater than
            // previous element, then this element
            // helps in building up the previous
            // increasing subarray encountered
            // so far
            if (arr[i] > arr[i - 1])
                len++;
            else {
                 
                // check if 'max' length is less
                // than the length of the current
                // increasing subarray. If true,
                // than update 'max'
                if (max < len)
                    max = len;
 
                // reset 'len' to 1 as from this
                // element again the length of the
                // new increasing subarray is being
                // calculated
                len = 1;
            }
        }
 
        // comparing the length of the last
        // increasing subarray with 'max'
        if (max < len)
            max = len;
 
        // required maximum length
        return max;
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int[] arr = { 5, 6, 3, 5, 7, 8, 9, 1, 2 };
        int n = arr.Length;
        Console.WriteLine("Length = " +
                           lenOfLongIncSubArr(arr, n));
    }
}
 
// This code is contributed by Sam007

PHP

<?php
// PHP implementation to find the length of
// longest increasing contiguous subarray
 
// function to find the length of
// longest increasing contiguous
// subarray
function lenOfLongIncSubArr($arr, $n)
{
     
    // 'max' to store the length of longest
    // increasing subarray
    // 'len' to store the lengths of longest
    // increasing subarray at different
    // instants of time
    $max = 1;
    $len = 1;
     
    // traverse the array from
    // the 2nd element
    for ($i = 1; $i < $n; $i++)
    {
         
        // if current element if
        // greater than previous
        // element, then this element
        // helps in building up the
        // previous increasing subarray
        // encountered so far
        if ($arr[$i] > $arr[$i-1])
            $len++;
        else
        {
             
            // check if 'max' length is
            // less than the length
            // of the current increasing
            // subarray. If true,
            // then update 'max'
            if ($max < $len)
                $max = $len;
                 
            // reset 'len' to 1 as
            // from this element
            // again the length of
            // the new increasing
            // subarray is being
            // calculated
            $len = 1;
        }
    }
     
    // comparing the length of the last
    // increasing subarray with 'max'
    if ($max < $len)
        $max = $len;
     
    // required maximum length
    return $max;
}
 
    // Driver Code
    $arr = array(5, 6, 3, 5, 7, 8, 9, 1, 2);
    $n = sizeof($arr);
    echo "Length = ", lenOfLongIncSubArr($arr, $n);
     
// This code is contributed by nitin mittal.
?>

Javascript

<script>
  
// Javascript implementation to find the length of
// longest increasing contiguous subarray
  
// function to find the length of longest increasing
// contiguous subarray
function lenOfLongIncSubArr(arr, n)
{
    // 'max' to store the length of longest
    // increasing subarray
    // 'len' to store the lengths of longest
    // increasing subarray at different
    // instants of time
    var max = 1, len = 1;
      
    // traverse the array from the 2nd element
    for (var i=1; i<n; i++)
    {
        // if current element if greater than previous
        // element, then this element helps in building
        // up the previous increasing subarray encountered
        // so far
        if (arr[i] > arr[i-1])
            len++;
        else
        {
            // check if 'max' length is less than the length
            // of the current increasing subarray. If true,
            // then update 'max'
            if (max < len)  
            {
                max = len;
            }
                  
            // reset 'len' to 1 as from this element
            // again the length of the new increasing
            // subarray is being calculated  
            len = 1;  
        }  
    }
      
    // comparing the length of the last
    // increasing subarray with 'max'
    if (max < len)
    {
        max = len;
    }
     
    // required maximum length
    return max;
}
  
// Driver program to test above
var arr = [5, 6, 3, 5, 7, 8, 9, 1, 2];
var n = arr.length;
document.write("Length = " + lenOfLongIncSubArr(arr, n));
// This code is contributed by shivani.
</script>
Producción

Length = 5

Complejidad de tiempo: O(n)

¿Cómo imprimir el subarreglo?  
Podemos imprimir el subarreglo haciendo un seguimiento del índice con la mayor longitud. 

Implementación:

C++

// C++ implementation to find the length of
// longest increasing contiguous subarray
#include <bits/stdc++.h>
using namespace std;
 
// function to find the length of longest increasing
// contiguous subarray
void printLogestIncSubArr(int arr[], int n)
{
    // 'max' to store the length of longest
    // increasing subarray
    // 'len' to store the lengths of longest
    // increasing subarray at different
    // instants of time
    int max = 1, len = 1, maxIndex = 0;
     
    // traverse the array from the 2nd element
    for (int i=1; i<n; i++)
    {
        // if current element if greater than previous
        // element, then this element helps in building
        // up the previous increasing subarray encountered
        // so far
        if (arr[i] > arr[i-1])
            len++;
        else
        {
            // check if 'max' length is less than the length
            // of the current increasing subarray. If true,
            // then update 'max'
            if (max < len)   
            {
                max = len;
                 
                // index assign the starting index of
                // longest increasing contiguous subarray.  
                maxIndex = i - max;
            }
                 
            // reset 'len' to 1 as from this element
            // again the length of the new increasing
            // subarray is being calculated   
            len = 1;   
        }   
    }
     
    // comparing the length of the last
    // increasing subarray with 'max'
    if (max < len)
    {
        max = len;
        maxIndex = n - max;
    }
 
    // Print the elements of longest increasing
    // contiguous subarray.
    for (int i=maxIndex; i<max+maxIndex; i++)
        cout << arr[i] << " ";
}
 
// Driver program to test above
int main()
{
    int arr[] = {5, 6, 3, 5, 7, 8, 9, 1, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    printLogestIncSubArr(arr, n);
    return 0;    
}
// This code is contributed by Dharmendra kumar

Java

// JAVA Code For Longest increasing subarray
import java.util.*;
 
class GFG {
     
    // function to find the length of longest
    // increasing contiguous subarray
    public static void printLogestIncSubArr(int arr[],
                                              int n)
    {
        // 'max' to store the length of longest
        // increasing subarray
        // 'len' to store the lengths of longest
        // increasing subarray at different
        // instants of time
        int max = 1, len = 1, maxIndex = 0;
          
        // traverse the array from the 2nd element
        for (int i = 1; i < n; i++)
        {
            // if current element if greater than
            // previous element, then this element
            // helps in building up the previous
            // increasing subarray encountered
            // so far
            if (arr[i] > arr[i-1])
                len++;
            else
            {
                // check if 'max' length is less
                // than the length of the current
                // increasing subarray. If true,
                // then update 'max'
                if (max < len)   
                {
                    max = len;
                      
                    // index assign the starting
                    // index of longest increasing
                    // contiguous subarray.  
                    maxIndex = i - max;
                }
                      
                // reset 'len' to 1 as from this
                // element again the length of the
                // new increasing subarray is
                // being calculated   
                len = 1;   
            }   
        }
          
        // comparing the length of the last
        // increasing subarray with 'max'
        if (max < len)
        {
            max = len;
            maxIndex = n - max;
        }
      
        // Print the elements of longest
        // increasing contiguous subarray.
        for (int i = maxIndex; i < max+maxIndex; i++)
            System.out.print(arr[i] + " ");
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int arr[] = {5, 6, 3, 5, 7, 8, 9, 1, 2};
        int n = arr.length;
        printLogestIncSubArr(arr, n);
         
    }
}
   
// This code is contributed by Arnav Kr. Mandal.

Python3

# Python 3 implementation to find the length of
# longest increasing contiguous subarray
 
 
# function to find the length of longest increasing
# contiguous subarray
def printLogestIncSubArr( arr, n) :
 
    # 'max' to store the length of longest
    # increasing subarray
    # 'len' to store the lengths of longest
    # increasing subarray at different
    # instants of time
    m = 1
    l = 1
    maxIndex = 0
      
    # traverse the array from the 2nd element
    for i in range(1, n) :
 
        # if current element if greater than previous
        # element, then this element helps in building
        # up the previous increasing subarray
        # encountered so far
        if (arr[i] > arr[i-1]) :
            l =l + 1
        else :
 
            # check if 'max' length is less than the length
            # of the current increasing subarray. If true,
            # then update 'max'
            if (m < l)  :
                m = l
 
                # index assign the starting index of
                # longest increasing contiguous subarray.  
                maxIndex = i - m
                   
            # reset 'len' to 1 as from this element
            # again the length of the new increasing
            # subarray is being calculated   
            l = 1   
         
         
    # comparing the length of the last
    # increasing subarray with 'max'
    if (m < l) :
        m = l
        maxIndex = n - m
     
    # Print the elements of longest
    # increasing contiguous subarray.
    for i in range(maxIndex, (m+maxIndex)) :
        print(arr[i] , end=" ")
         
# Driver program to test above
arr = [5, 6, 3, 5, 7, 8, 9, 1, 2]
n = len(arr)
printLogestIncSubArr(arr, n)
     
     
# This code is contributed
# by Nikita Tiwari

C#

// C# Code to print
// Longest increasing subarray
using System;
 
class GFG {
 
    // function to find the length of longest
    // increasing contiguous subarray
    public static void printLogestIncSubArr(int[] arr,
                                                int n)
    {
        // 'max' to store the length of longest
        // increasing subarray
        // 'len' to store the lengths of longest
        // increasing subarray at different
        // instants of time
        int max = 1, len = 1, maxIndex = 0;
 
        // traverse the array from the 2nd element
        for (int i = 1; i < n; i++) {
             
            // if current element if greater than
            // previous element, then this element
            // helps in building up the previous
            // increasing subarray encountered
            // so far
            if (arr[i] > arr[i - 1])
                len++;
            else
            {
                // check if 'max' length is less
                // than the length of the current
                // increasing subarray. If true,
                // then update 'max'
                if (max < len) {
                    max = len;
 
                    // index assign the starting
                    // index of longest increasing
                    // contiguous subarray.
                    maxIndex = i - max;
                }
 
                // reset 'len' to 1 as from this
                // element again the length of the
                // new increasing subarray is
                // being calculated
                len = 1;
            }
        }
 
        // comparing the length of the last
        // increasing subarray with 'max'
        if (max < len) {
            max = len;
            maxIndex = n - max;
        }
 
        // Print the elements of longest
        // increasing contiguous subarray.
        for (int i = maxIndex; i < max + maxIndex; i++)
            Console.Write(arr[i] + " ");
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int[] arr = { 5, 6, 3, 5, 7, 8, 9, 1, 2 };
        int n = arr.Length;
        printLogestIncSubArr(arr, n);
    }
}
 
// This code is contributed by Sam007

PHP

<?php
// PHP implementation to find
// the length of longest increasing
// contiguous subarray
 
// function to find the length of
// longest increasing contiguous subarray
function printLogestIncSubArr(&$arr, $n)
{
    // 'max' to store the length of
    // longest increasing subarray
    // 'len' to store the lengths of
    // longest increasing subarray at
    // different instants of time
    $max = 1;
    $len = 1;
    $maxIndex = 0;
     
    // traverse the array from
    // the 2nd element
    for ($i = 1; $i < $n; $i++)
    {
        // if current element if greater
        // than previous element, then
        // this element helps in building
        // up the previous increasing
        // subarray encountered so far
        if ($arr[$i] > $arr[$i - 1])
            $len++;
        else
        {
            // check if 'max' length is less
            // than the length of the current
            // increasing subarray. If true,
            // then update 'max'
            if ($max < $len)
            {
                $max = $len;
                 
                // index assign the starting
                // index of longest increasing
                // contiguous subarray.
                $maxIndex = $i - $max;
            }
                 
            // reset 'len' to 1 as from this
            // element again the length of
            // the new increasing subarray
            // is being calculated
            $len = 1;
        }
    }
     
    // comparing the length of
    // the last increasing
    // subarray with 'max'
    if ($max < $len)
    {
        $max = $len;
        $maxIndex = $n - $max;
    }
 
    // Print the elements of
    // longest increasing
    // contiguous subarray.
    for ($i = $maxIndex;
         $i < ($max + $maxIndex); $i++)
        echo($arr[$i] . " ") ;
         
}
 
// Driver Code
$arr = array(5, 6, 3, 5, 7,
             8, 9, 1, 2);
$n = sizeof($arr);
printLogestIncSubArr($arr, $n);
     
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript

<script>
 
// Javascript implementation to find the length of
// longest increasing contiguous subarray
 
// function to find the length of longest increasing
// contiguous subarray
function printLogestIncSubArr(arr, n)
{
    // 'max' to store the length of longest
    // increasing subarray
    // 'len' to store the lengths of longest
    // increasing subarray at different
    // instants of time
    var max = 1, len = 1, maxIndex = 0;
     
    // traverse the array from the 2nd element
    for (var i=1; i<n; i++)
    {
        // if current element if greater than previous
        // element, then this element helps in building
        // up the previous increasing subarray encountered
        // so far
        if (arr[i] > arr[i-1])
            len++;
        else
        {
            // check if 'max' length is less than the length
            // of the current increasing subarray. If true,
            // then update 'max'
            if (max < len)   
            {
                max = len;
                 
                // index assign the starting index of
                // longest increasing contiguous subarray.  
                maxIndex = i - max;
            }
                 
            // reset 'len' to 1 as from this element
            // again the length of the new increasing
            // subarray is being calculated   
            len = 1;   
        }   
    }
     
    // comparing the length of the last
    // increasing subarray with 'max'
    if (max < len)
    {
        max = len;
        maxIndex = n - max;
    }
 
    // Print the elements of longest increasing
    // contiguous subarray.
    for (var i=maxIndex; i<max+maxIndex; i++)
        document.write( arr[i] + " ");
}
 
// Driver program to test above
var arr = [5, 6, 3, 5, 7, 8, 9, 1, 2];
var n = arr.length;
printLogestIncSubArr(arr, n);
 
// This code is contributed by rrrtnx.
</script>
Producción

3 5 7 8 9 

Este artículo es una contribución de Ayush Jauhari . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks. 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *