📌  相关文章
📜  计算每个 K 长度子数组中存在的负元素

📅  最后修改于: 2022-05-13 01:56:05.813000             🧑  作者: Mango

计算每个 K 长度子数组中存在的负元素

给定一个大小为N的数组arr[]和一个整数K ,任务是计算所有K长度子数组中存在的负元素的数量。

例子:

朴素方法:最简单的方法是遍历给定数组,考虑每个大小为K的窗口, 并找到每个窗口中的负数计数。

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

有效的方法:这个问题可以使用窗口滑动技术来解决。请按照以下步骤解决问题:

  • 将变量count初始化为0以将负元素的计数存储在大小为K的窗口中。
  • 将两个变量ij初始化为0 ,分别存储窗口的第一个和最后一个索引。
  • j时循环并执行以下步骤:
    • 如果arr[j] < 0 ,则将count加 1。
    • 如果窗口的大小,即j-i+1等于K ,则打印count的值, 并检查arr[i] < 0 ,然后将count减 1。另外,将i加 1。
    • j的值增加 1。

下面是上述方法的实现

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to count the number of
// negative elements in every window
// of size K
void countNegative(vector arr, int k)
{
     
    // Initialize the window pointers
    int i = 0;
    int j = 0;
 
    // Store the count of negative numbers
    int count = 0;
    int n = arr.size();
 
    // Traverse the array, arr[]
    while (j < n)
    {
         
        // Increase the count
        // if element is less then 0
        if (arr[j] < 0)
        {
            count++;
        }
 
        // If size of the window equal to k
        if (j - i + 1 == k)
        {
            cout << count << " ";
 
            // If the first element of
            // the window is less than 0,
            // decrement count by 1
            if (arr[i] < 0)
            {
                count--;
            }
            i++;
        }
        j++;
    }
}
 
// Driver Code
int main()
{
     
    // Given Input
    vector arr{ -1, 2, -2, 3, 5, -7, -5 };
    int k = 3;
 
    // Function Call
    countNegative(arr, k);
}
     
// This code is contributed by bgangwar59


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to count the number of
    // negative elements in every window
    // of size K
    public static void countNegative(int[] arr, int k)
    {
        // Initialize the window pointers
        int i = 0;
        int j = 0;
 
        // Store the count of negative numbers
        int count = 0;
        int n = arr.length;
 
        // Traverse the array, arr[]
        while (j < n) {
 
            // Increase the count
            // if element is less then 0
            if (arr[j] < 0) {
                count++;
            }
 
            // If size of the window equal to k
            if (j - i + 1 == k) {
                System.out.print(count + " ");
 
                // If the first element of
                // the window is less than 0,
                // decrement count by 1
                if (arr[i] < 0) {
                    count--;
                }
                i++;
            }
 
            j++;
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Input
        int[] arr = { -1, 2, -2, 3, 5, -7, -5 };
        int k = 3;
 
        // Function Call
        countNegative(arr, k);
    }
}


Python3
# Function to count the number of
# negative elements in every window
# of size K
def countNegative(arr,k):
     
    # Initialize the window pointers
    i = 0
    j = 0
     
    # Store the count of negative numbers
    count = 0
    n = len(arr)
     
    while(j < n):
         
        # Increase the count
        # if element is less then 0
         
        if (arr[j] < 0):
            count = count + 1
             
        # If size of the window equal to k  
        if (j - i + 1 == k):
            print(count,end=" ")
             
            # If the first element of
            # the window is less than 0,
            # decrement count by 1
             
            if(arr[i] < 0):
                count = count - 1
             
            i = i+1
        j = j+1
         
# Driver Code
 
# Given Input
arr = [-1, 2, -2, 3, 5, -7, -5]
k = 3
countNegative(arr, k)
 
# This code is contributed by abhinavjain194.


C#
// C#  program for the above approach
using System;
 
class GFG{
 
// Function to count the number of
// negative elements in every window
// of size K
public static void countNegative(int[] arr, int k)
{
     
    // Initialize the window pointers
    int i = 0;
    int j = 0;
 
    // Store the count of negative numbers
    int count = 0;
    int n = arr.Length;
 
    // Traverse the array, arr[]
    while (j < n)
    {
         
        // Increase the count
        // if element is less then 0
        if (arr[j] < 0)
        {
            count++;
        }
 
        // If size of the window equal to k
        if (j - i + 1 == k)
        {
            Console.Write(count + " ");
 
            // If the first element of
            // the window is less than 0,
            // decrement count by 1
            if (arr[i] < 0)
            {
                count--;
            }
            i++;
        }
        j++;
    }
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given Input
    int[] arr = { -1, 2, -2, 3, 5, -7, -5 };
    int k = 3;
 
    // Function Call
    countNegative(arr, k);
}
}    
 
// This code is contributed by ukasp


Javascript


输出:
2 1 1 1 2

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