📌  相关文章
📜  计算超过前 K 个元素总和的数组元素

📅  最后修改于: 2021-09-07 02:19:42             🧑  作者: Mango

给定的阵列ARR大小为N [],和整数K,任务是找到数组元素的计数的常用3 [I],其是大于(ARR [I – 1] + ARR [I – 2] + … + arr[ i – K])

例子:

处理方法:按照以下步骤解决问题:

  1. 计算并存储给定数组的前缀和
  2. 遍历索引[K, N – 1] 中存在的数组元素。
  3. 对于每个数组元素,检查arr[i] 是否超过(arr[i – 1] + arr[i – 2] + … + arr[i – K]) 。因此,任务是检查arr[i] 是否超过prefix[i – 1] – prefix[i – (K + 1)] for K > i > N或者arr[i] 是否超过prefix[i – 1] for我 = K。
  4. 满足上述条件的数组元素的递增计数。最后,打印所需的count

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count array elements
// exceeding sum of preceding K elements
int countPrecedingK(int a[], int n, int K)
{
    int prefix[n];
    prefix[0] = a[0];
 
    // Iterate over the array
    for (int i = 1; i < n; i++) {
 
        // Update prefix sum
        prefix[i]
            = prefix[i - 1] + a[i];
    }
 
    int ctr = 0;
 
    // Check if arr[K] >
    // arr[0] + .. + arr[K - 1]
    if (prefix[K - 1] < a[K])
 
        // Increment count
        ctr++;
 
    for (int i = K + 1; i < n; i++) {
        // Check if arr[i] >
        // arr[i - K - 1] + .. + arr[i - 1]
        if (prefix[i - 1] - prefix[i - K - 1]
            < a[i])
 
            // Increment count
            ctr++;
    }
 
    return ctr;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 2, 3, 8, 10, -2,
                  7, 5, 5, 9, 15 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 2;
 
    cout << countPrecedingK(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to count array elements
// exceeding sum of preceding K elements
static int countPrecedingK(int a[], int n,
                           int K)
{
    int []prefix = new int[n];
    prefix[0] = a[0];
 
    // Iterate over the array
    for(int i = 1; i < n; i++)
    {
         
        // Update prefix sum
        prefix[i] = prefix[i - 1] + a[i];
    }
 
    int ctr = 0;
 
    // Check if arr[K] >
    // arr[0] + .. + arr[K - 1]
    if (prefix[K - 1] < a[K])
 
        // Increment count
        ctr++;
 
    for(int i = K + 1; i < n; i++)
    {
         
        // Check if arr[i] >
        // arr[i - K - 1] + .. + arr[i - 1]
        if (prefix[i - 1] -
            prefix[i - K - 1] < a[i])
 
            // Increment count
            ctr++;
    }
    return ctr;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array
    int arr[] = { 2, 3, 8, 10, -2,
                  7, 5, 5, 9, 15 };
 
    int N = arr.length;
    int K = 2;
 
    System.out.print(countPrecedingK(arr, N, K));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to count array elements
# exceeding sum of preceding K elements
def countPrecedingK(a, n, K):
 
    prefix = [0] * n
    prefix[0] = a[0]
 
    # Iterate over the array
    for i in range(1, n):
 
        # Update prefix sum
        prefix[i] = prefix[i - 1] + a[i]
 
    ctr = 0
 
    # Check if arr[K] >
    # arr[0] + .. + arr[K - 1]
    if (prefix[K - 1] < a[K]):
 
        # Increment count
        ctr += 1
 
    for i in range(K + 1, n):
         
        # Check if arr[i] >
        # arr[i - K - 1] + .. + arr[i - 1]
        if (prefix[i - 1] -
            prefix[i - K - 1] < a[i]):
 
            # Increment count
            ctr += 1
             
    return ctr
 
# Driver Code
if __name__ == '__main__':
 
    # Given array
    arr = [ 2, 3, 8, 10, -2,
            7, 5, 5, 9, 15 ]
 
    N = len(arr)
    K = 2
 
    print(countPrecedingK(arr, N, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to count array elements
// exceeding sum of preceding K elements
static int countPrecedingK(int []a,
                           int n, int K)
{
  int []prefix = new int[n];
  prefix[0] = a[0];
 
  // Iterate over the array
  for(int i = 1; i < n; i++)
  {
    // Update prefix sum
    prefix[i] = prefix[i - 1] + a[i];
  }
 
  int ctr = 0;
 
  // Check if arr[K] >
  // arr[0] + .. + arr[K - 1]
  if (prefix[K - 1] < a[K])
 
    // Increment count
    ctr++;
 
  for(int i = K + 1; i < n; i++)
  {
    // Check if arr[i] >
    // arr[i - K - 1] + .. + arr[i - 1]
    if (prefix[i - 1] -
        prefix[i - K - 1] < a[i])
 
      // Increment count
      ctr++;
  }
  return ctr;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array
  int []arr = {2, 3, 8, 10, -2,
               7, 5, 5, 9, 15};
 
  int N = arr.Length;
  int K = 2;
 
  Console.Write(countPrecedingK(arr, N, K));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
2

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

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