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

📅  最后修改于: 2021-04-29 03:28:11             🧑  作者: 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.
?>


输出:

3

时间复杂度: O(n)。