codeparrot/xlcost-text-to-code huggingface dataset:
Check if two nodes are cousins in a Binary Tree | C # program to check if two binary tree are cousins ; A Binary Tree Node ; Recursive function to check if two Nodes are siblings ; Base case ; Recursive function to find level of Node ' ptr ' in a binary tree ; base cases ; Return level if Node is present in left subtree ; Else search in right subtree ; Returns 1 if a and b are cousins , otherwise 0 ; 1. The two Nodes should be on the same level in the binary tree . 2. The two Nodes should not be siblings ( means that they should not have the same parent Node ) . ; Driver Code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual bool isSibling ( Node node , Node a , Node b ) { if ( node == null ) { return false ; } return ( ( node . left == a && node . right == b ) || ( node . left == b && node . right == a ) || isSibling ( node . left , a , b ) || isSibling ( node . right , a , b ) ) ; } public virtual int level ( Node node , Node ptr , int lev ) { if ( node == null ) { return 0 ; } if ( node == ptr ) { return lev ; } int l = level ( node . left , ptr , lev + 1 ) ; if ( l != 0 ) { return l ; } return level ( node . right , ptr , lev + 1 ) ; } public virtual bool isCousin ( Node node , Node a , Node b ) { return ( ( level ( node , a , 1 ) == level ( node , b , 1 ) ) && ( ! isSibling ( node , a , b ) ) ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . left . right . right = new Node ( 15 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . right . left . right = new Node ( 8 ) ; Node Node1 , Node2 ; Node1 = tree . root . left . left ; Node2 = tree . root . right . right ; if ( tree . isCousin ( tree . root , Node1 , Node2 ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Check if all leaves are at same level | C # program to check if all leaves are at same level ; A binary tree node ; Recursive function which checks whether all leaves are at same level ; Base case ; If a leaf node is encountered ; When a leaf node is found first time ; Set first found leaf 's level ; If this is not first leaf node , compare its level with first leaf 's level ; If this node is not leaf , recursively check left and right subtrees ; The main function to check if all leafs are at same level . It mainly uses checkUtil ( ) ; Driver Code ; Let us create the tree as shown in the example
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class Leaf { public int leaflevel = 0 ; } class GFG { public Node root ; public Leaf mylevel = new Leaf ( ) ; public virtual bool checkUtil ( Node node , int level , Leaf leafLevel ) { if ( node == null ) { return true ; } if ( node . left == null && node . right == null ) { if ( leafLevel . leaflevel == 0 ) { leafLevel . leaflevel = level ; return true ; } return ( level == leafLevel . leaflevel ) ; } return checkUtil ( node . left , level + 1 , leafLevel ) && checkUtil ( node . right , level + 1 , leafLevel ) ; } public virtual bool check ( Node node ) { int level = 0 ; return checkUtil ( node , level , mylevel ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 12 ) ; tree . root . left = new Node ( 5 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 9 ) ; tree . root . left . left . left = new Node ( 1 ) ; tree . root . left . right . left = new Node ( 1 ) ; if ( tree . check ( tree . root ) ) { Console . WriteLine ( " Leaves ▁ are ▁ at ▁ same ▁ level " ) ; } else { Console . WriteLine ( " Leaves ▁ are ▁ not ▁ at ▁ same ▁ level " ) ; } } }
Number of siblings of a given Node in n | C # program to find number of siblings of a given node ; Represents a node of an n - ary tree ; Function to calculate number of siblings of a given node ; Creating a queue and pushing the root ; Dequeue an item from queue and check if it is equal to x If YES , then return number of children ; Enqueue all children of the dequeued item ; If the value of children is equal to x , then return the number of siblings ; Driver code ; Creating a generic tree as shown in above figure ; Node whose number of siblings is to be calculated ; Function calling
using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public List < Node > child ; public Node ( int data ) { key = data ; child = new List < Node > ( ) ; } } ; static int numberOfSiblings ( Node root , int x ) { if ( root == null ) return 0 ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count > 0 ) { Node p = q . Peek ( ) ; q . Dequeue ( ) ; for ( int i = 0 ; i < p . child . Count ; i ++ ) { if ( p . child [ i ] . key == x ) return p . child . Count - 1 ; q . Enqueue ( p . child [ i ] ) ; } } return - 1 ; } public static void Main ( String [ ] args ) { Node root = new Node ( 50 ) ; ( root . child ) . Add ( new Node ( 2 ) ) ; ( root . child ) . Add ( new Node ( 30 ) ) ; ( root . child ) . Add ( new Node ( 14 ) ) ; ( root . child ) . Add ( new Node ( 60 ) ) ; ( root . child [ 0 ] . child ) . Add ( new Node ( 15 ) ) ; ( root . child [ 0 ] . child ) . Add ( new Node ( 25 ) ) ; ( root . child [ 0 ] . child [ 1 ] . child ) . Add ( new Node ( 70 ) ) ; ( root . child [ 0 ] . child [ 1 ] . child ) . Add ( new Node ( 100 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 6 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 1 ) ) ; ( root . child [ 2 ] . child ) . Add ( new Node ( 7 ) ) ; ( root . child [ 2 ] . child [ 0 ] . child ) . Add ( new Node ( 17 ) ) ; ( root . child [ 2 ] . child [ 0 ] . child ) . Add ( new Node ( 99 ) ) ; ( root . child [ 2 ] . child [ 0 ] . child ) . Add ( new Node ( 27 ) ) ; ( root . child [ 3 ] . child ) . Add ( new Node ( 16 ) ) ; int x = 100 ; Console . WriteLine ( numberOfSiblings ( root , x ) ) ; } }
Check if all leaves are at same level | C # program to check if all leaf nodes are at same level of binary tree ; User defined node class ; Constructor to create a new tree node ; return true if all leaf nodes are at same level , else false ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check for left child ; if its leaf node ; if it 's first leaf node, then update result ; if it 's not first leaf node, then compare  the level with level of previous leaf node. ; check for right child ; if its leaf node ; if it 's first leaf node, then update result ; if it 's not first leaf node, then compare  the level with level of previous leaf node. ; Driver code ; construct a tree
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int key ) { int data = key ; left = right = null ; } } public class GFG { static bool checkLevelLeafNode ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; int result = int . MaxValue ; int level = 0 ; while ( q . Count != 0 ) { int size = q . Count ; level ++ ; while ( size > 0 ) { Node temp = q . Dequeue ( ) ; if ( temp . left != null ) { q . Enqueue ( temp . left ) ; if ( temp . left . left != null && temp . left . right != null ) { if ( result == int . MaxValue ) result = level ; else if ( result != level ) return false ; } } if ( temp . right != null ) { q . Enqueue ( temp . right ) ; if ( temp . right . left != null && temp . right . right != null ) { if ( result == int . MaxValue ) result = level ; else if ( result != level ) return false ; } } size -- ; } } return true ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . right = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; bool result = checkLevelLeafNode ( root ) ; if ( result == true ) Console . WriteLine ( " All ▁ leaf ▁ nodes ▁ are ▁ at ▁ same ▁ level " ) ; else Console . WriteLine ( " Leaf ▁ nodes ▁ not ▁ at ▁ same ▁ level " ) ; } }
FIFO ( First | C # program to demonstrate working of FIFO ; Driver code ; Adds elements { 0 , 1 , 2 , 3 , 4 } to queue ; Display contents of the queue . ; To remove the head of queue . In this the oldest element '0' will be removed ; To view the head of queue ; Rest all methods of collection interface , Like size and contains can be used with this implementation .
using System ; using System . Collections . Generic ; public class QueueExample { public static void Main ( String [ ] args ) { Queue < int > q = new Queue < int > ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) q . Enqueue ( i ) ; Console . Write ( " Elements ▁ of ▁ queue - " ) ; foreach ( int s in q ) Console . Write ( s + " ▁ " ) ; int removedele = q . Dequeue ( ) ; Console . Write ( " STRNEWLINE removed ▁ element - " + removedele + " STRNEWLINE " ) ; foreach ( int s in q ) Console . Write ( s + " ▁ " ) ; int head = q . Peek ( ) ; Console . Write ( " STRNEWLINE head ▁ of ▁ queue - " + head ) ; int size = q . Count ; Console . WriteLine ( " STRNEWLINE Size ▁ of ▁ queue - " + size ) ; } }
LIFO ( Last | C # program to demonstrate working of LIFO using Stack in C # ; Pushing element on the top of the stack ; Popping element from the top of the stack ; Displaying element on the top of the stack ; Searching element in the stack ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void stack_push ( Stack < int > stack ) { for ( int i = 0 ; i < 5 ; i ++ ) { stack . Push ( i ) ; } } static void stack_pop ( Stack < int > stack ) { Console . WriteLine ( " Pop ▁ : " ) ; for ( int i = 0 ; i < 5 ; i ++ ) { int y = ( int ) stack . Pop ( ) ; Console . WriteLine ( y ) ; } } static void stack_peek ( Stack < int > stack ) { int element = ( int ) stack . Peek ( ) ; Console . WriteLine ( " Element ▁ on ▁ stack ▁ top ▁ : ▁ " + element ) ; } static void stack_search ( Stack < int > stack , int element ) { bool pos = stack . Contains ( element ) ; if ( pos == false ) Console . WriteLine ( " Element ▁ not ▁ found " ) ; else Console . WriteLine ( " Element ▁ is ▁ found ▁ at ▁ position ▁ " + pos ) ; } public static void Main ( String [ ] args ) { Stack < int > stack = new Stack < int > ( ) ; stack_push ( stack ) ; stack_pop ( stack ) ; stack_push ( stack ) ; stack_peek ( stack ) ; stack_search ( stack , 2 ) ; stack_search ( stack , 6 ) ; } }
Reversing a queue using recursion | C # code for reversing a queue ; Utility function to print the queue ; Recursive function to reverse the queue ; Base case ; Dequeue current item ( from front ) ; Reverse remaining queue ; Enqueue current item ( to rear ) ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printQueue ( Queue < long > queue ) { while ( queue . Count != 0 ) { Console . Write ( queue . Peek ( ) + " ▁ " ) ; queue . Dequeue ( ) ; } } static void reverseQueue ( ref Queue < long > q ) { if ( q . Count == 0 ) return ; long data = q . Peek ( ) ; q . Dequeue ( ) ; reverseQueue ( ref q ) ; q . Enqueue ( data ) ; } static void Main ( ) { Queue < long > queue = new Queue < long > ( ) ; queue . Enqueue ( 56 ) ; queue . Enqueue ( 27 ) ; queue . Enqueue ( 30 ) ; queue . Enqueue ( 45 ) ; queue . Enqueue ( 85 ) ; queue . Enqueue ( 92 ) ; queue . Enqueue ( 58 ) ; queue . Enqueue ( 80 ) ; queue . Enqueue ( 90 ) ; queue . Enqueue ( 100 ) ; reverseQueue ( ref queue ) ; printQueue ( queue ) ; } }
Sorting a Queue without extra space | C # program to implement sorting a queue data structure ; Queue elements after sorted Index are already sorted . This function returns index of minimum element from front to sortedIndex ; This is dequeue ( ) in C ++ STL ; we add the condition i <= sortedIndex because we don 't want to traverse  on the sorted part of the   queue, which is the right part. ; This is enqueue ( ) in C ++ STL ; Moves given minimum element to rear of queue ; SortQueue Function ; Driver Code ; Sort queue ; Print sorted queue
using System ; using System . Collections . Generic ; class GFG { static int minIndex ( ref Queue < int > q , int sortedIndex ) { int min_index = - 1 ; int min_val = int . MaxValue ; int n = q . Count ; for ( int i = 0 ; i < n ; i ++ ) { int curr = q . Peek ( ) ; q . Dequeue ( ) ; if ( curr <= min_val && i <= sortedIndex ) { min_index = i ; min_val = curr ; } q . Enqueue ( curr ) ; } return min_index ; } static void insertMinToRear ( ref Queue < int > q , int min_index ) { int min_val = 0 ; int n = q . Count ; for ( int i = 0 ; i < n ; i ++ ) { int curr = q . Peek ( ) ; q . Dequeue ( ) ; if ( i != min_index ) q . Enqueue ( curr ) ; else min_val = curr ; } q . Enqueue ( min_val ) ; } static void sortQueue ( ref Queue < int > q ) { for ( int i = 1 ; i <= q . Count ; i ++ ) { int min_index = minIndex ( ref q , q . Count - i ) ; insertMinToRear ( ref q , min_index ) ; } } static void Main ( ) { Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( 30 ) ; q . Enqueue ( 11 ) ; q . Enqueue ( 15 ) ; q . Enqueue ( 4 ) ; sortQueue ( ref q ) ; while ( q . Count != 0 ) { Console . Write ( q . Peek ( ) + " ▁ " ) ; q . Dequeue ( ) ; } Console . WriteLine ( ) ; } }
Check if removing an edge can divide a Binary Tree in two halves | C # program to check if there exist an edge whose removal creates two trees of same size ; To calculate size of tree with given root ; This function returns true if there is an edge whose removal can divide the tree in two halves n is size of tree ; Base cases ; Check for root ; Check for rest of the nodes ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Now recursively check all nodes ; Driver code
using System ; public class Node { public int key ; public Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class GFG { public Node root ; public virtual int count ( Node node ) { if ( node == null ) { return 0 ; } return count ( node . left ) + count ( node . right ) + 1 ; } public virtual bool checkRec ( Node node , int n ) { if ( node == null ) { return false ; } if ( count ( node ) == n - count ( node ) ) { return true ; } return checkRec ( node . left , n ) || checkRec ( node . right , n ) ; } public virtual bool check ( Node node ) { int n = count ( node ) ; return checkRec ( node , n ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 1 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 4 ) ; if ( tree . check ( tree . root ) == true ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
Sliding Window Maximum ( Maximum of all subarrays of size k ) | C # Program to find the maximum for each and every contiguous subarray of size k . ; A Dequeue ( Double ended queue ) based method for printing maximum element of all subarrays of size k ; Create a Double Ended Queue , Qi that will store indexes of array elements The queue will store indexes of useful elements in every window and it will maintain decreasing order of values from front to rear in Qi , i . e . , arr [ Qi . front [ ] ] to arr [ Qi . rear ( ) ] are sorted in decreasing order ; Process first k ( or first window ) elements of array ; For every element , the previous smaller elements are useless so remove them from Qi ; Remove from rear ; Add new element at rear of queue ; Process rest of the elements , i . e . , from arr [ k ] to arr [ n - 1 ] ; The element at the front of the queue is the largest element of previous window , so print it ; Remove the elements which are out of this window ; Remove from front of queue ; Remove all elements smaller than the currently being added element ( remove useless elements ) ; Add current element at the rear of Qi ; Print the maximum element of last window ; Driver code
using System ; using System . Collections . Generic ; public class SlidingWindow { static void printMax ( int [ ] arr , int n , int k ) { List < int > Qi = new List < int > ( ) ; int i ; for ( i = 0 ; i < k ; ++ i ) { while ( Qi . Count != 0 && arr [ i ] >= arr [ Qi [ Qi . Count - 1 ] ] ) Qi . RemoveAt ( Qi . Count - 1 ) ; Qi . Insert ( Qi . Count , i ) ; } for ( ; i < n ; ++ i ) { Console . Write ( arr [ Qi [ 0 ] ] + " ▁ " ) ; while ( ( Qi . Count != 0 ) && Qi [ 0 ] <= i - k ) Qi . RemoveAt ( 0 ) ; while ( ( Qi . Count != 0 ) && arr [ i ] >= arr [ Qi [ Qi . Count - 1 ] ] ) Qi . RemoveAt ( Qi . Count - 1 ) ; Qi . Insert ( Qi . Count , i ) ; } Console . Write ( arr [ Qi [ 0 ] ] ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 1 , 78 , 90 , 57 , 89 , 56 } ; int k = 3 ; printMax ( arr , arr . Length , k ) ; } }
Sum of minimum and maximum elements of all subarrays of size k . | C # program to find sum of all minimum and maximum elements Of Sub - array Size k . ; Returns sum of min and max element of all subarrays of size k ; Initialize result ; The queue will store indexes of useful elements in every window In deque ' G ' we maintain decreasing order of values from front to rear In deque ' S ' we maintain increasing order of values from front to rear ; Process first window of size K ; Remove all previous greater elements that are useless . ; Remove from rear ; Remove all previous smaller that are elements are useless . ; Remove from rear ; Add current element at rear of both deque ; Process rest of the Array elements ; Element at the front of the deque ' G ' & ' S ' is the largest and smallest element of previous window respectively ; Remove all elements which are out of this window ; remove all previous greater element that are useless ; Remove from rear ; remove all previous smaller that are elements are useless ; Remove from rear ; Add current element at rear of both deque ; Sum of minimum and maximum element of last window ; Driver code
using System ; using System . Collections . Generic ; class Geeks { public static int SumOfKsubArray ( int [ ] arr , int k ) { int sum = 0 ; List < int > S = new List < int > ( ) ; List < int > G = new List < int > ( ) ; int i = 0 ; for ( i = 0 ; i < k ; i ++ ) { while ( S . Count != 0 && arr [ S [ S . Count - 1 ] ] >= arr [ i ] ) S . RemoveAt ( S . Count - 1 ) ; while ( G . Count != 0 && arr [ G [ G . Count - 1 ] ] <= arr [ i ] ) G . RemoveAt ( G . Count - 1 ) ; G . Add ( i ) ; S . Add ( i ) ; } for ( ; i < arr . Length ; i ++ ) { sum += arr [ S [ 0 ] ] + arr [ G [ 0 ] ] ; while ( S . Count != 0 && S [ 0 ] <= i - k ) S . RemoveAt ( 0 ) ; while ( G . Count != 0 && G [ 0 ] <= i - k ) G . RemoveAt ( 0 ) ; while ( S . Count != 0 && arr [ S [ S . Count - 1 ] ] >= arr [ i ] ) S . RemoveAt ( S . Count - 1 ) ; while ( G . Count != 0 && arr [ G [ G . Count - 1 ] ] <= arr [ i ] ) G . RemoveAt ( G . Count - 1 ) ; G . Add ( i ) ; S . Add ( i ) ; } sum += arr [ S [ 0 ] ] + arr [ G [ 0 ] ] ; return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , - 1 , 7 , - 3 , - 1 , - 2 } ; int k = 3 ; Console . WriteLine ( SumOfKsubArray ( arr , k ) ) ; } }
Check if removing an edge can divide a Binary Tree in two halves | C # program to check if there exist an edge whose removal creates two trees of same size ; To calculate size of tree with given root ; This function returns size of tree rooted with given root . It also set " res " as true if there is an edge whose removal divides tree in two halves . n is size of tree ; Base case ; Compute sizes of left and right children ; If required property is true for current node set " res " as true ; Return size ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Initialize result and recursively check all nodes ; Driver code
using System ; public class Node { public int key ; public Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } public class Res { public bool res = false ; } public class BinaryTree { public Node root ; public virtual int count ( Node node ) { if ( node == null ) { return 0 ; } return count ( node . left ) + count ( node . right ) + 1 ; } public virtual int checkRec ( Node root , int n , Res res ) { if ( root == null ) { return 0 ; } int c = checkRec ( root . left , n , res ) + 1 + checkRec ( root . right , n , res ) ; if ( c == n - c ) { res . res = true ; } return c ; } public virtual bool check ( Node root ) { int n = count ( root ) ; Res res = new Res ( ) ; checkRec ( root , n , res ) ; return res . res ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 1 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 4 ) ; if ( tree . check ( tree . root ) == true ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
Distance of nearest cell having 1 in a binary matrix | C # program to find the distance of nearest cell having 1 in a binary matrix . ; Print the distance of nearest cell having 1 for each cell . ; Initialise the answer matrix with int . MaxValue . ; For each cell ; Traversing thewhole matrix to find the minimum distance . ; If cell contain 1 , check for minimum distance . ; Printing the answer . ; Driven Program
using System ; class GFG { static int N = 3 ; static int M = 4 ; static void printDistance ( int [ , ] mat ) { int [ , ] ans = new int [ N , M ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) ans [ i , j ] = int . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) for ( int l = 0 ; l < M ; l ++ ) { if ( mat [ k , l ] == 1 ) ans [ i , j ] = Math . Min ( ans [ i , j ] , Math . Abs ( i - k ) + Math . Abs ( j - l ) ) ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) Console . Write ( ans [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { int [ , ] mat = { { 0 , 0 , 0 , 1 } , { 0 , 0 , 1 , 1 } , { 0 , 1 , 1 , 0 } } ; printDistance ( mat ) ; } }
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 code
using System ; class GFG { static 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 ) { Console . Write ( ( arr [ i + j ] ) + " ▁ " ) ; flag = true ; break ; } } if ( ! flag ) Console . Write ( "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 ) ; } }