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

📅  最后修改于: 2021-10-27 07:31:31             🧑  作者: Mango

给定一个整数数组,找出包含可以按连续序列排列的数字的最长子数组的长度。
在上一篇文章中,我们讨论了一个假设给定数组中的元素不同的解决方案。在这里,我们讨论一种即使输入数组有重复也能工作的解决方案。
例子:

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

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

这个想法与之前的帖子类似。在上一篇文章中,我们检查了最大值减去最小值是否等于结束索引减去起始索引。由于允许重复元素,我们还需要检查子数组是否包含重复元素。例如,数组 {12, 14, 12} 跟在第一个属性之后,但其中的数字不是连续元素。
为了检查子数组中的重复元素,我们为每个子数组创建一个散列集,如果我们发现一个元素已经在散列中,我们不考虑当前的子数组。
下面是上述想法的实现。

C++
/* CPP program to find length of the largest
subarray which has all contiguous elements */
#include
using namespace std;
 
// This function prints all distinct elements
int findLength(int arr[], int n)
{
    int max_len = 1; // Initialize result
 
    // One by one fix the starting points
    for (int i=0; i myset;
        myset.insert(arr[i]);
 
        // Initialize max and min in
        // current subarray
        int mn = arr[i], mx = arr[i];
 
        // One by one fix ending points
        for (int j=i+1; j


Java
/* Java program to find length of the largest subarray which has
   all contiguous elements */
import java.util.*;
 
class Main
{
    // This function prints all distinct elements
    static int findLength(int arr[])
    {
        int n = arr.length;
        int max_len = 1; // Initialize result
 
        // One by one fix the starting points
        for (int i=0; i set = new HashSet<>();
            set.add(arr[i]);
 
            // Initialize max and min in current subarray
            int mn = arr[i], mx = arr[i];
 
            // One by one fix ending points
            for (int j=i+1; j


Python3
# python program to find length of the largest
# subarray which has all contiguous elements */
 
# This function prints all distinct elements
def findLenght(arr, n):
    max_len = 1
    for i in range(0,n - 1):
 
        # Create an empty hash set and
        # add i'th element to it
        myset = set()
        myset.add(arr[i])
 
        # Initialize max and min in
        # current subarray
        mn = arr[i]
        mx = arr[i]
        for j in range(i + 1,n):
 
            # If current element is already
            # in hash set, then this subarray
            # cannot contain contiguous elements
            if arr[j] in myset:
                break
 
 
            # Else add current element to hash
            # set and update min, max if required.
            myset.add(arr[j])
            mn = min(mn, arr[j])
            mx = max(mx, arr[j])
 
            # We have already checked for
            # duplicates, now check for other
            #property and update max_len
            # if needed
            if mx - mn == j - i:
                max_len = max(max_len, mx - mn + 1)
 
    return max_len # Return result
 
 
# Driver code
 
arr = [10, 12, 12, 10, 10, 11, 10]
n = len(arr)
print("Length of the longest contiguous subarray is",
                                findLenght(arr,n))
 
# This code is contributed by Shrikant13


C#
/* C# program to find length of the largest
subarray which has all contiguous elements */
using System;
using System.Collections.Generic;
 
class GFG {
     
    // This function prints all distinct
    // elements
    static int findLength(int []arr)
    {
        int n = arr.Length;
        int max_len = 1; // Initialize result
 
        // One by one fix the starting points
        for (int i = 0; i < n-1; i++)
        {
             
            // Create an empty hash set and
            // add i'th element to it.
            HashSet set = new HashSet();
            set.Add(arr[i]);
 
            // Initialize max and min in current
            // subarray
            int mn = arr[i], mx = arr[i];
 
            // One by one fix ending points
            for (int j = i+1; j < n; j++)
            {
                 
                // If current element is already
                // in hash set, then this subarray
                // cannot contain contiguous
                // elements
                if (set.Contains(arr[j]))
                    break;
 
                // Else add curremt element to
                // hash set and update min,
                // max if required.
                set.Add(arr[j]);
                mn = Math.Min(mn, arr[j]);
                mx = Math.Max(mx, arr[j]);
 
                // We have already checked for
                // duplicates, now check for
                // other property and update
                // max_len if needed
                if (mx-mn == j-i)
                    max_len = Math.Max(max_len,
                                  mx - mn + 1);
            }
        }
         
        return max_len; // Return result
    }
     
    // Driver function
    public static void Main()
    {
        int []arr = {10, 12, 12, 10, 10, 11, 10};
        Console.WriteLine("Length of the longest"
                   + " contiguous subarray is " +
                               findLength(arr));
    }
}
 
// This code is contributed by Sam007


Javascript


输出:

Length of the longest contiguous subarray is 2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程