# First negative integer in every window of size k in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] consisting of N integer values and a window of size N. Our task is to create a program for finding the first negative integer in every window of size k. We will be printing the first negative number if it exists otherwise print 0, denoting no negative exists.

Let's take an example to understand the problem,

Input: arr[] = {-2, 2, -1, 4, 3, -6}, k = 2
Output: -2, -1, -1, 0, -6

Explanation

Size of window k = 2,
{-2, 2}, first negative is -2
{2, -1}, first negative is -1
{-1, 4}, first negative is -1
{4, 3}, first negative is 0 (no negative exists)
{3, -6}, first negative is -6

## Solution Approach

A simple method to solve the problem is by traversing the array arr[] making windows of size k. And in each window finding the first negative integer and print it.

The solution is a naive one that uses two nested loops for performing the operation. Hence the time complexity of the solution is of the order O(n*k).

## Example

Program to illustrate the working of our solution

#include <iostream>
using namespace std;
void findFirstNegIntWindowK(int arr[], int n, int k){
bool negFound;
for (int i = 0; i<(n-k+1); i++)
{
negFound = false;
for (int j = 0; j<k; j++)
{
if (arr[i+j] < 0)
{
cout<<arr[i+j]<<"\t";
negFound = true;
break;
}
}
if (!negFound)
cout<<"0\t";
}
}
int main(){
int arr[] = {-2, 2, -1, 4, 3, -6};
int n = sizeof(arr)/sizeof(arr[0]);
int k = 2;
cout<<"First negative integer in each with of size "<<k<<" is \n";
findFirstNegIntWindowK(arr, n, k);
return 0;
}

## Output

First negative integer in each with of size 2 is
-2 -1 -1 0 -6

Another method to solve the problem is by using a concept similar to the sliding window. For this, we will be creating a dequeue (a double-ended queue) of size k for all the elements of the window of size k. We will start traversing the array from start and filling the element in the dequeue of size k. And then, for each element of the array, we will remove one element from the end and add one element to another. For each slided window we will be finding and printing the first negative number. For finding the negative number we will be checking if the removed number is the first negative number of the window or not, if yes we will check the next elements for first negative number otherwise not.

## Example

Program to illustrate the working of our solution

#include <bits/stdc++.h>
using namespace std;
void findFirstNegIntWindowK(int arr[], int n, int k){
deque<int> windowKsize;
int i = 0;
for (; i < k; i++)
if (arr[i] < 0)
windowKsize.push_back(i);
for (; i < n; i++){
if (!windowKsize.empty())
cout<<arr[windowKsize.front()]<<"\t";
else
cout<<"0\t";
while ( (!windowKsize.empty()) && windowKsize.front() < (i - k + 1))
windowKsize.pop_front();
if (arr[i] < 0)
windowKsize.push_back(i);
}
if (!windowKsize.empty())
cout<<arr[windowKsize.front()]<<" \t";
else
cout<<"0\t";
}
int main(){
int arr[] = {-2, 2, -1, 4, 3, -6};
int n = sizeof(arr)/sizeof(arr[0]);
int k = 2;
cout<<"First negative integer in each with of size "<<k<<" is \n";
findFirstNegIntWindowK(arr, n, k);
return 0;
}

## Output

First negative integer in each with of size 2 is
-2 -1 -1 0 -6
Updated on 01-Feb-2022 11:16:06