📜  最大值为 K 的子数组计数

📅  最后修改于: 2021-09-17 07:39:22             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[]和一个整数K 。任务是找到最大值等于K的子数组的数量

例子:

方法:数组arr[]中的子数组个数等于最大不大于K的子数组个数减去最大不大于K-1的子数组个数参考这里计算最大大于K的子数组的个数按照以下步骤解决问题:

  • 将变量count1初始化为0以计算最大不大于K-1的子数组的数量
  • 将变量count2初始化为0以计算最大不大于K的子数组的数量
  • 定义一个函数,例如totalSubarrays(arr, N, K)来计算最大不大于K的子数组的数量,并执行以下步骤:
    • 将变量 say, ans初始化为 0以存储最大不大于K的子数组的计数
    • [0, N-1]范围内迭代并执行以下步骤。
      • 如果arr[i]的值大于K,则将i的值增加1并继续。
      • 将变量count初始化为0以存储可能的子数组总数
      • 在范围内迭代直到i小于Narr[i]小于等于K。
        • icount的值增加1。
      • (count*(count+1))/2的值与ans的值相加。
    • 返回ans的值
  • 调用函数totalSubarrays(arr, N, K-1)计算最大不大于K-1的子数组个数,并存入count1。
  • 调用函数totalSubarrays(arr, N, K)计算最大不大于K的子数组个数,并存入count2。
  • (count2 – count1)的值存储在ans的最终值中并打印出来。

下面是上述方法的实现。

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to count the subarrays with maximum
// not greater than K
int totalSubarrays(int arr[], int n, int k)
{
    int ans = 0, i = 0;
  
    while (i < n) {
        // If arr[i]>k then arr[i] cannot be
        // a part of any subarray.
        if (arr[i] > k) {
            i++;
            continue;
        }
  
        int count = 0;
        // Count the number of elements where
        // arr[i] is not greater than k.
        while (i < n && arr[i] <= k) {
            i++;
            count++;
        }
  
        // Summation of all possible subarrays
        // in the variable ans.
        ans += ((count * (count + 1)) / 2);
    }
  
    return ans;
}
  
// Function to count the subarrays with
// maximum value is equal to K
int countSubarrays(int arr[], int n, int k)
{
    // Stores count of subarrays with max <= k - 1.
    int count1 = totalSubarrays(arr, n, k - 1);
  
    // Stores count of subarrays with max >= k + 1.
    int count2 = totalSubarrays(arr, n, k);
  
    // Stores count of subrrays with max = k.
    int ans = count2 - count1;
  
    return ans;
}
  
// Driver Code
int main()
{
    // Given Input
    int n = 4, k = 3;
    int arr[] = { 2, 1, 3, 4 };
  
    // Function Call
    cout << countSubarrays(arr, n, k);
  
    return 0;
}


Java
// Java program for the above approach
  
import java.io.*;
  
class GFG {
    // Function to count the subarrays with maximum
    // not greater than K
    static int totalSubarrays(int arr[], int n, int k)
    {
        int ans = 0, i = 0;
  
        while (i < n) {
            // If arr[i]>k then arr[i] cannot be
            // a part of any subarray.
            if (arr[i] > k) {
                i++;
                continue;
            }
  
            int count = 0;
            // Count the number of elements where
            // arr[i] is not greater than k.
            while (i < n && arr[i] <= k) {
                i++;
                count++;
            }
  
            // Summation of all possible subarrays
            // in the variable ans.
            ans += ((count * (count + 1)) / 2);
        }
  
        return ans;
    }
  
    // Function to count the subarrays with
    // maximum value is equal to K
    static int countSubarrays(int arr[], int n, int k)
    {
        // Stores count of subarrays with max <= k - 1.
        int count1 = totalSubarrays(arr, n, k - 1);
  
        // Stores count of subarrays with max >= k + 1.
        int count2 = totalSubarrays(arr, n, k);
  
        // Stores count of subrrays with max = k.
        int ans = count2 - count1;
  
        return ans;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Given Input
        int n = 4, k = 3;
        int arr[] = { 2, 1, 3, 4 };
  
        // Function Call
  
        System.out.println(countSubarrays(arr, n, k));
      // This code is contributed by Potta Lokesh
    }
}


Python3
# Python3 implementation of the above approach
  
# Function to count the subarrays with maximum
# not greater than K
def totalSubarrays(arr, n, k):
      
    ans = 0
    i = 0
  
    while (i < n):
          
        # If arr[i]>k then arr[i] cannot be
        # a part of any subarray.
        if (arr[i] > k):
            i += 1
            continue
  
        count = 0
          
        # Count the number of elements where
        # arr[i] is not greater than k.
        while (i < n and arr[i] <= k):
            i += 1
            count += 1
  
        # Summation of all possible subarrays
        # in the variable ans.
        ans += ((count * (count + 1)) // 2)
  
    return ans
  
# Function to count the subarrays with
# maximum value is equal to K
def countSubarrays(arr, n, k):
      
    # Stores count of subarrays with max <= k - 1.
    count1 = totalSubarrays(arr, n, k - 1)
  
    # Stores count of subarrays with max >= k + 1.
    count2 = totalSubarrays(arr, n, k)
  
    # Stores count of subrrays with max = k.
    ans = count2 - count1
  
    return ans
  
# Driver Code
if __name__ == '__main__':
      
    # Given Input
    n = 4
    k = 3
    arr = [ 2, 1, 3, 4 ]
  
    # Function Call
    print(countSubarrays(arr, n, k))
  
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
  
class GFG
{
    // Function to count the subarrays with maximum
    // not greater than K
    static int totalSubarrays(int[] arr, int n, int k)
    {
        int ans = 0, i = 0;
  
        while (i < n)
        {
            
            // If arr[i]>k then arr[i] cannot be
            // a part of any subarray.
            if (arr[i] > k) {
                i++;
                continue;
            }
  
            int count = 0;
            
            // Count the number of elements where
            // arr[i] is not greater than k.
            while (i < n && arr[i] <= k) {
                i++;
                count++;
            }
  
            // Summation of all possible subarrays
            // in the variable ans.
            ans += ((count * (count + 1)) / 2);
        }
  
        return ans;
    }
  
    // Function to count the subarrays with
    // maximum value is equal to K
    static int countSubarrays(int[] arr, int n, int k)
    {
        // Stores count of subarrays with max <= k - 1.
        int count1 = totalSubarrays(arr, n, k - 1);
  
        // Stores count of subarrays with max >= k + 1.
        int count2 = totalSubarrays(arr, n, k);
  
        // Stores count of subrrays with max = k.
        int ans = count2 - count1;
  
        return ans;
    }
    static void Main()
    {
        // Given Input
        int n = 4, k = 3;
        int[] arr = { 2, 1, 3, 4 };
  
        // Function Call
  
        Console.WriteLine(countSubarrays(arr, n, k));
    }
}
  
// This code is contributed by abhinavjain194


Javascript


输出
3

时间复杂度: O(N)
辅助空间: O(1)

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