📌  相关文章
📜  具有连续元素的最大子数组的长度 |设置 1

📅  最后修改于: 2022-05-13 01:57:48.635000             🧑  作者: Mango

具有连续元素的最大子数组的长度 |设置 1

给定一个由不同整数组成的数组,找出最长的子数组的长度,其中包含可以排列成连续序列的数字。
例子:

Input:  arr[] = {10, 12, 11};
Output: Length of the longest contiguous subarray is 3

Input:  arr[] = {14, 12, 11, 20};
Output: Length of the longest contiguous subarray is 2

Input:  arr[] = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
Output: Length of the longest contiguous subarray is 5

我们强烈建议您将浏览器最小化并先自己尝试一下。
需要注意的重要一点是,所有元素都是不同的。如果所有元素都是不同的,则当且仅当子数组中最大和最小元素之间的差等于子数组的最后一个索引和第一个索引之间的差时,子数组才具有连续元素。所以这个想法是跟踪每个子数组中的最小和最大元素。
以下是上述思想的实现。

C++
#include
using namespace std;
 
// Utility functions to find minimum and maximum of
// two elements
int min(int x, int y) { return (x < y)? x : y; }
int max(int x, int y) { return (x > y)? x : y; }
 
// Returns length of the longest contiguous subarray
int findLength(int arr[], int n)
{
    int max_len = 1;  // Initialize result
    for (int i=0; i


Java
class LargestSubArray2
{
    // Utility functions to find minimum and maximum of
    // two elements
 
    int min(int x, int y)
    {
        return (x < y) ? x : y;
    }
 
    int max(int x, int y)
    {
        return (x > y) ? x : y;
    }
 
    // Returns length of the longest contiguous subarray
    int findLength(int arr[], int n)
    {
        int max_len = 1;  // Initialize result
        for (int i = 0; i < n - 1; i++)
        {
            // Initialize min and max for all subarrays starting with i
            int mn = arr[i], mx = arr[i];
 
            // Consider all subarrays starting with i and ending with j
            for (int j = i + 1; j < n; j++)
            {
                // Update min and max in this subarray if needed
                mn = min(mn, arr[j]);
                mx = max(mx, arr[j]);
 
                // If current subarray has all contiguous elements
                if ((mx - mn) == j - i)
                    max_len = max(max_len, mx - mn + 1);
            }
        }
        return max_len;  // Return result
    }
 
    public static void main(String[] args)
    {
        LargestSubArray2 large = new LargestSubArray2();
        int arr[] = {1, 56, 58, 57, 90, 92, 94, 93, 91, 45};
        int n = arr.length;
        System.out.println("Length of the longest contiguous subarray is "
                + large.findLength(arr, n));
    }
}
 
// This code has been contributed by Mayank Jaiswal


Python3
# Python3 program to find length
# of the longest subarray
 
# Utility functions to find minimum
# and maximum of two elements
def min(x, y):
    return x if(x < y) else y
     
def max(x, y):
    return x if(x > y) else y
 
# Returns length of the longest
# contiguous subarray
def findLength(arr, n):
     
    # Initialize result
    max_len = 1
    for i in range(n - 1):
     
        # Initialize min and max for
        # all subarrays starting with i
        mn = arr[i]
        mx = arr[i]
 
        # Consider all subarrays starting
        # with i and ending with j
        for j in range(i + 1, n):
         
            # Update min and max in
            # this subarray if needed
            mn = min(mn, arr[j])
            mx = max(mx, arr[j])
 
            # If current subarray has
            # all contiguous elements
            if ((mx - mn) == j - i):
                max_len = max(max_len, mx - mn + 1)
         
    return max_len
 
# Driver Code
arr = [1, 56, 58, 57, 90, 92, 94, 93, 91, 45]
n = len(arr)
print("Length of the longest contiguous subarray is ",
                                    findLength(arr, n))
                                     
# This code is contributed by Anant Agarwal.


C#
using System;
 
class GFG {
     
    // Returns length of the longest
    // contiguous subarray
    static int findLength(int []arr, int n)
    {
        int max_len = 1; // Initialize result
        for (int i = 0; i < n - 1; i++)
        {
            // Initialize min and max for all
            // subarrays starting with i
            int mn = arr[i], mx = arr[i];
 
            // Consider all subarrays starting
            // with i and ending with j
            for (int j = i + 1; j < n; j++)
            {
                // Update min and max in this
                // subarray if needed
                mn = Math.Min(mn, arr[j]);
                mx = Math.Max(mx, arr[j]);
 
                // If current subarray has all
                // contiguous elements
                if ((mx - mn) == j - i)
                    max_len = Math.Max(max_len,
                                  mx - mn + 1);
            }
        }
        return max_len; // Return result
    }
 
    public static void Main()
    {
         
        int []arr = {1, 56, 58, 57, 90, 92,
                               94, 93, 91, 45};
        int n = arr.Length;
         
        Console.WriteLine("Length of the longest"
                     + " contiguous subarray is "
                           + findLength(arr, n));
    }
}
 
// This code is contributed by Sam007.


PHP
 $b)
        return $a;
    else
        return $b;
}
     
// Returns length of the longest
// contiguous subarray
function findLength(&$arr, $n)
{
    $max_len = 1; // Initialize result
    for ($i = 0; $i < $n - 1; $i++)
    {
        // Initialize min and max for all
        // subarrays starting with i
        $mn = $arr[$i];
        $mx = $arr[$i];
 
        // Consider all subarrays starting
        // with i and ending with j
        for ($j = $i + 1; $j < $n; $j++)
        {
            // Update min and max in this
            // subarray if needed
            $mn = mins($mn, $arr[$j]);
            $mx = maxi($mx, $arr[$j]);
 
            // If current subarray has all
            // contiguous elements
            if (($mx - $mn) == $j - $i)
                $max_len = maxi($max_len,
                                $mx - $mn + 1);
        }
    }
    return $max_len; // Return result
}
 
// Driver Code
$arr = array(1, 56, 58, 57, 90,
             92, 94, 93, 91, 45);
$n = sizeof($arr);
echo ("Length of the longest contiguous" .
                         " subarray is ");
echo (findLength($arr, $n));
     
// This code is contributed
// by Shivi_Aggarwal.
?>


Javascript


输出:

Length of the longest contiguous subarray is 5

上述解决方案的时间复杂度为 O(n 2 )。