📜  包含从 1 到子数组长度的数字的数组中的子数组计数

📅  最后修改于: 2021-09-06 11:27:44             🧑  作者: Mango

给定一个长度为 N 的数组arr[]包含从 1 到 N 的所有元素,任务是找到包含从 1 到 M 的数字的子数组的数量,其中 M 是子数组的长度。
例子:

朴素的方法:生成数组的所有子数组并检查每个子数组是否包含每个元素 1 到子数组的长度。
高效的方法:创建一个向量,该向量以排序的顺序映射数组的每个元素及其索引。现在迭代这个向量并检查直到第 i元素的最大和最小索引的差异是否小于到目前为止迭代的元素数,即 i 本身的值。
下面是上述方法的实现

C++
// C++ Implementation to Count the no. of
// Sub-arrays which contains all elements
// from 1 to length of subarray
 
#include 
using namespace std;
 
// Function to count the number
// Sub-arrays which contains all elements
// 1 to length of subarray
int countOfSubarrays(int* arr, int n)
{
    int count = 0;
    vector v(n + 1);
 
    // Map all elements of array with their index
    for (int i = 0; i < n; i++)
        v[arr[i]] = i;
 
    // Set the max and min index equal to the
    // min and max value of integer respectively.
    int maximum = INT_MIN;
    int minimum = INT_MAX;
 
    for (int i = 1; i <= n; i++) {
 
        // Update the value of maximum index
        maximum = max(maximum, v[i]);
 
        // Update the value of minimum index
        minimum = min(minimum, v[i]);
 
        // Increase the counter if difference of
        // max. and min. index is less than the
        // elements iterated till now
        if (maximum - minimum < i)
            count = count + 1;
    }
 
    return count;
}
 
// Driver Function
int main()
{
    int arr[] = { 4, 1, 3, 2, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countOfSubarrays(arr, n) << endl;
    return 0;
}


Java
// Java Implementation to Count the no. of
// Sub-arrays which contains all elements
// from 1 to length of subarray
class GFG
{
 
// Function to count the number
// Sub-arrays which contains all elements
// 1 to length of subarray
static int countOfSubarrays(int []arr, int n)
{
    int count = 0;
    int []v = new int[n + 1];
 
    // Map all elements of array with their index
    for (int i = 0; i < n; i++)
        v[arr[i]] = i;
 
    // Set the max and min index equal to the
    // min and max value of integer respectively.
    int maximum = Integer.MIN_VALUE;
    int minimum = Integer.MAX_VALUE;
 
    for (int i = 1; i <= n; i++)
    {
 
        // Update the value of maximum index
        maximum = Math.max(maximum, v[i]);
 
        // Update the value of minimum index
        minimum = Math.min(minimum, v[i]);
 
        // Increase the counter if difference of
        // max. and min. index is less than the
        // elements iterated till now
        if (maximum - minimum < i)
            count = count + 1;
    }
 
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 1, 3, 2, 5, 6 };
    int n = arr.length;
    System.out.print(countOfSubarrays(arr, n) +"\n");
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 Implementation to Count the no. of
# Sub-arrays which contains all elements
# from 1 to length of subarray
import sys
 
INT_MAX = sys.maxsize;
INT_MIN = -(sys.maxsize - 1);
 
# Function to count the number
# Sub-arrays which contains all elements
# 1 to length of subarray
def countOfSubarrays(arr, n) :
 
    count = 0;
    v = [0]*(n + 1);
 
    # Map all elements of array with their index
    for i in range(n) :
        v[arr[i]] = i;
 
    # Set the max and min index equal to the
    # min and max value of integer respectively.
    maximum = INT_MIN;
    minimum = INT_MAX;
 
    for i in range(1, n + 1) :
 
        # Update the value of maximum index
        maximum = max(maximum, v[i]);
 
        # Update the value of minimum index
        minimum = min(minimum, v[i]);
 
        # Increase the counter if difference of
        # max. and min. index is less than the
        # elements iterated till now
        if (maximum - minimum < i) :
            count = count + 1;
 
    return count;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 4, 1, 3, 2, 5, 6 ];
    n = len(arr);
    print(countOfSubarrays(arr, n));
 
# This code is contributed by AnkitRai01


C#
// C# Implementation to Count the no. of
// Sub-arrays which contains all elements
// from 1 to length of subarray
using System;
 
class GFG
{
 
// Function to count the number
// Sub-arrays which contains all elements
// 1 to length of subarray
static int countOfSubarrays(int []arr, int n)
{
    int count = 0;
    int []v = new int[n + 1];
 
    // Map all elements of array with their index
    for (int i = 0; i < n; i++)
        v[arr[i]] = i;
 
    // Set the max and min index equal to the
    // min and max value of integer respectively.
    int maximum = int.MinValue;
    int minimum = int.MaxValue;
 
    for (int i = 1; i <= n; i++)
    {
 
        // Update the value of maximum index
        maximum = Math.Max(maximum, v[i]);
 
        // Update the value of minimum index
        minimum = Math.Min(minimum, v[i]);
 
        // Increase the counter if difference of
        // max. and min. index is less than the
        // elements iterated till now
        if (maximum - minimum < i)
            count = count + 1;
    }
 
    return count;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 4, 1, 3, 2, 5, 6 };
    int n = arr.Length;
    Console.Write(countOfSubarrays(arr, n) +"\n");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:

5

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live