📌  相关文章
📜  最大元素大于 k 的子数组的计数

📅  最后修改于: 2021-09-22 10:25:55             🧑  作者: Mango

给定一个包含n 个元素的数组和一个整数k 。任务是找到最大元素大于 K 的子数组的计数。
例子 :

Input : arr[] = {1, 2, 3} and k = 2.
Output : 3
All the possible subarrays of arr[] are 
{ 1 }, { 2 }, { 3 }, { 1, 2 }, { 2, 3 }, 
{ 1, 2, 3 }.
Their maximum elements are 1, 2, 3, 2, 3, 3.
There are only 3 maximum elements > 2.

这个想法是通过计算最大元素小于或等于 k 的子数组来解决问题,因为计算这些子数组更容易。求最大元素小于等于k的子数组个数,去掉所有大于k的元素,求左边元素的子数组个数。
一旦我们找到上面的计数,我们就可以从 n*(n+1)/2 中减去它以获得我们需要的结果。观察,任何大小为 n 的数组都可以有 n*(n+1)/2 个可能的子数组数。因此,找到最大元素小于或等于 K 的子数组的数量并从 n*(n+1)/2 中减去它,我们得到了答案。
下面是这个方法的实现:

C++
// C++ program to count number of subarrays
// whose maximum element is greater than K.
#include 
using namespace std;
 
// Return number of subarrays whose maximum
// element is less than or equal to K.
int countSubarray(int arr[], int n, int k)
{
    // To store count of subarrays with all
    // elements less than or equal to k.
    int s = 0;
 
    // Traversing the array.
    int i = 0;
    while (i < n) {
        // If element is greater than k, ignore.
        if (arr[i] > k) {
            i++;
            continue;
        }
 
        // Counting the subarray length whose
        // each element is less than equal to k.
        int count = 0;
        while (i < n && arr[i] <= k) {
            i++;
            count++;
        }
 
        // Suming number of subarray whose
        // maximum element is less than equal to k.
        s += ((count * (count + 1)) / 2);
    }
 
    return (n * (n + 1) / 2 - s);
}
 
// Driven Program
int main()
{
    int arr[] = { 1, 2, 3 };
    int k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countSubarray(arr, n, k);
    return 0;
}


Java
// Java program to count number of subarrays
// whose maximum element is greater than K.
import java.util.*;
 
class GFG {
 
    // Return number of subarrays whose maximum
    // element is less than or equal to K.
    static int countSubarray(int arr[], int n, int k)
    {
 
        // To store count of subarrays with all
        // elements less than or equal to k.
        int s = 0;
 
        // Traversing the array.
        int i = 0;
        while (i < n) {
 
            // If element is greater than k, ignore.
            if (arr[i] > k) {
                i++;
                continue;
            }
 
            // Counting the subarray length whose
            // each element is less than equal to k.
            int count = 0;
            while (i < n && arr[i] <= k) {
                i++;
                count++;
            }
 
            // Suming number of subarray whose
            // maximum element is less than equal to k.
            s += ((count * (count + 1)) / 2);
        }
 
        return (n * (n + 1) / 2 - s);
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int arr[] = { 1, 2, 3 };
        int k = 2;
        int n = arr.length;
        System.out.print(countSubarray(arr, n, k));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to count
# number of subarrays
# whose maximum element
# is greater than K.
 
# Return number of
# subarrays whose maximum
# element is less than or equal to K.
def countSubarray(arr, n, k):
 
    # To store count of
    # subarrays with all
    # elements less than
    # or equal to k.
    s = 0
  
    # Traversing the array.
    i = 0
    while (i < n):
     
        # If element is greater
        # than k, ignore.
        if (arr[i] > k):
         
            i = i + 1
            continue
         
        # Counting the subarray
        # length whose
        # each element is less
        # than equal to k.
        count = 0
        while (i < n and arr[i] <= k):
         
            i = i + 1
            count = count + 1
         
  
        # Suming number of subarray whose
        # maximum element is less
        # than equal to k.
        s = s + ((count*(count + 1))//2)
     
  
    return (n*(n + 1)//2 - s)
     
# Driver code
 
arr = [1, 2, 3]
k = 2
n = len(arr)
 
print(countSubarray(arr, n, k))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to count number of subarrays
// whose maximum element is greater than K.
using System;
 
class GFG {
 
    // Return number of subarrays whose maximum
    // element is less than or equal to K.
    static int countSubarray(int[] arr, int n, int k)
    {
        // To store count of subarrays with all
        // elements less than or equal to k.
        int s = 0;
 
        // Traversing the array.
        int i = 0;
        while (i < n) {
 
            // If element is greater than k, ignore.
            if (arr[i] > k) {
                i++;
                continue;
            }
 
            // Counting the subarray length whose
            // each element is less than equal to k.
            int count = 0;
            while (i < n && arr[i] <= k) {
                i++;
                count++;
            }
 
            // Suming number of subarray whose
            // maximum element is less than equal to k.
            s += ((count * (count + 1)) / 2);
        }
 
        return (n * (n + 1) / 2 - s);
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = {1, 2, 3};
        int k = 2;
        int n = arr.Length;
        Console.WriteLine(countSubarray(arr, n, k));
    }
}
 
// This code is contributed by vt_m.


PHP
 $k) {
            $i++;
            continue;
        }
 
        // Counting the subarray length
        // whose each element is less
        // than equal to k.
        $count = 0;
        while ($i < $n and $arr[$i] <= $k) {
            $i++;
            $count++;
        }
 
        // Suming number of subarray whose
        // maximum element is less than
        // equal to k.
        $s += (($count * ($count + 1)) / 2);
    }
 
    return ($n * ($n + 1) / 2 - $s);
}
 
// Driven Program
    $arr = array( 1, 2, 3 );
    $k = 2;
    $n = count($arr);
    echo countSubarray($arr, $n, $k);
 
// This code is contributed by anuj_67.
?>


Javascript


输出:

3

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