First negative integer in every window of size k | C ++ implementation to find the first negative integer in every window of size k ; function to find the first negative integer in every window of size k ; flag to check whether window contains a negative integer or not ; Loop for each subarray ( window ) of size k ; traverse through the current window ; if a negative integer is found , then it is the first negative integer for current window . Print it , set the flag and break ; if the current window does not contain a negative integer ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printFirstNegativeInteger ( int arr [ ] , int n , int k ) { bool flag ; for ( int i = 0 ; i < ( n - k + 1 ) ; i ++ ) { flag = false ; for ( int j = 0 ; j < k ; j ++ ) { if ( arr [ i + j ] < 0 ) { cout << arr [ i + j ] << " ▁ " ; flag = true ; break ; } } if ( ! flag ) cout << "0" << " ▁ " ; } } int main ( ) { int arr [ ] = { 12 , -1 , -7 , 8 , -15 , 30 , 16 , 28 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; printFirstNegativeInteger ( arr , n , k ) ; return 0 ; }