First negative integer in every window of size k | Java 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
import java . util . * ; class solution { static void printFirstNegativeInteger ( int arr [ ] , int n , int k ) { boolean flag ; for ( int i = 0 ; i < ( n - k + 1 ) ; i ++ ) { flag = false ; for ( int j = 0 ; j < k ; j ++ ) { if ( arr [ i + j ] < 0 ) { System . out . print ( ( arr [ i + j ] ) + " ▁ " ) ; flag = true ; break ; } } if ( ! flag ) System . out . print ( "0" + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 12 , - 1 , - 7 , 8 , - 15 , 30 , 16 , 28 } ; int n = arr . length ; int k = 3 ; printFirstNegativeInteger ( arr , n , k ) ; } }