📌  相关文章
📜  计算所有元素大于K的子数组

📅  最后修改于: 2021-06-27 01:40:42             🧑  作者: Mango

给定一个N个整数数组和一个数字K,任务是找到子数组的数量,以使其中的所有元素都大于K。

例子:

方法:想法是开始使用计数器遍历数组。如果当前元素大于给定值X,则增加计数器,否则将counter *(counter + 1)/ 2添加到子数组数中,并将counter重新初始化为0。

下面是上述方法的实现:

C++
// C++ program to print the number of subarrays such
// that all elements are greater than K
#include 
using namespace std;
 
// Function to count number of subarrays
int countSubarrays(int a[], int n, int x)
{
    int count = 0;
 
    int number = 0;
 
    // Iterate in the array
    for (int i = 0; i < n; i++) {
 
        // check if array element
        // greater then X or not
        if (a[i] > x) {
            count += 1;
        }
        else {
 
            number += (count) * (count + 1) / 2;
            count = 0;
        }
    }
 
    // After iteration complete
    // check for the last set of subarrays
    if (count)
        number += (count) * (count + 1) / 2;
 
    return number;
}
 
// Driver Code
int main()
{
    int a[] = { 3, 4, 5, 6, 7, 2, 10, 11 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 5;
 
    cout << countSubarrays(a, n, k);
 
    return 0;
}


Java
// Java program to print the number of subarrays such
// that all elements are greater than K
 
import java.io.*;
 
class GFG {
     
// Function to count number of subarrays
static int countSubarrays(int a[], int n, int x)
{
    int count = 0;
 
    int number = 0;
 
    // Iterate in the array
    for (int i = 0; i < n; i++) {
 
        // check if array element
        // greater then X or not
        if (a[i] > x) {
            count += 1;
        }
        else {
 
            number += (count) * (count + 1) / 2;
            count = 0;
        }
    }
 
    // After iteration complete
    // check for the last set of subarrays
    if (count!=0)
        number += (count) * (count + 1) / 2;
 
    return number;
}
 
// Driver Code
    public static void main (String[] args) {
        int a[] = { 3, 4, 5, 6, 7, 2, 10, 11 };
        int n = a.length;
        int k = 5;
 
        System.out.println (countSubarrays(a, n, k));
         
    }
}


Python3
# Python program to print the number of
# subarrays such that all elements are
# greater than K
 
# Function to count number of subarrays
def countSubarrays(a, n, x):
    count = 0
    number = 0
     
    # Iterate in the array
    for i in range(n):
         
        # check if array element greater
        # then X or not
        if (a[i] > x):
            count += 1
        else:
            number += (count) * (count + 1) / 2
            count = 0
             
    # After iteration complete check for
    # the last set of subarrays
    if (count):
        number += (count) * (count + 1) / 2
    return int(number)
 
# Driver Code
if __name__ == '__main__':
    a = [3, 4, 5, 6, 7, 2, 10, 11]
    n = len(a)
    k = 5
    print(countSubarrays(a, n, k))
 
# This code is contributed by 29AjayKumar


C#
// C# program to print the number of subarrays such
// that all elements are greater than K
 
using System;
 
class GFG {
     
// Function to count number of subarrays
static int countSubarrays(int []a, int n, int x)
{
    int count = 0;
 
    int number = 0;
 
    // Iterate in the array
    for (int i = 0; i < n; i++) {
 
        // check if array element
        // greater then X or not
        if (a[i] > x) {
            count += 1;
        }
        else {
 
            number += (count) * (count + 1) / 2;
            count = 0;
        }
    }
 
    // After iteration complete
    // check for the last set of subarrays
    if (count!=0)
        number += (count) * (count + 1) / 2;
 
    return number;
}
 
// Driver Code
    public static void Main () {
        int []a = { 3, 4, 5, 6, 7, 2, 10, 11 };
        int n = a.Length;
        int k = 5;
 
        Console.WriteLine(countSubarrays(a, n, k));
         
    }
}
// This code is contributed by anuj_67..


PHP
 $x)
        {
            $count += 1;
        }
        else
        {
            $number += ($count) *
                       ($count + 1) / 2;
            $count = 0;
        }
    }
 
    // After iteration complete
    // check for the last set
    // of subarrays
    if ($count)
        $number += ($count) *
                   ($count + 1) / 2;
 
    return $number;
}
 
// Driver Code
$a = array(3, 4, 5, 6, 7, 2, 10, 11);
$n = count($a);
$k = 5;
 
echo countSubarrays($a, $n, $k);
 
// This code is contributed by anuj_67
?>


Javascript


输出:
6

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。