codeparrot/xlcost-text-to-code huggingface dataset:
Cocktail Sort | C # program for implementation of Cocktail Sort ; Sorts arrar a [ 0. . n - 1 ] using Cocktail sort ; reset the swapped flag on entering the loop , because it might be true from a previous iteration . ; loop from bottom to top same as the bubble sort ; if nothing moved , then array is sorted . ; otherwise , reset the swapped flag so that it can be used in the next stage ; move the end point back by one , because item at the end is in its rightful spot ; from top to bottom , doing the same comparison as in the previous stage ; increase the starting point , because the last stage would have moved the next smallest number to its rightful spot . ; Prints the array ; Driver code
using System ; class GFG { static void cocktailSort ( int [ ] a ) { bool swapped = true ; int start = 0 ; int end = a . Length ; while ( swapped == true ) { swapped = false ; for ( int i = start ; i < end - 1 ; ++ i ) { if ( a [ i ] > a [ i + 1 ] ) { int temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; swapped = true ; } } if ( swapped == false ) break ; swapped = false ; end = end - 1 ; for ( int i = end - 1 ; i >= start ; i -- ) { if ( a [ i ] > a [ i + 1 ] ) { int temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; swapped = true ; } } start = start + 1 ; } } static void printArray ( int [ ] a ) { int n = a . Length ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] a = { 5 , 1 , 4 , 2 , 8 , 0 , 2 } ; cocktailSort ( a ) ; Console . WriteLine ( " Sorted ▁ array ▁ " ) ; printArray ( a ) ; } }
Find the point where maximum intervals overlap | C # Program to find maximum guest at any time in a party ; Sort arrival and exit arrays ; guests_in indicates number of guests at a time ; Similar to merge in merge sort to process all events in sorted order ; If next event in sorted order is arrival , increment count of guests ; Update max_guests if needed ; increment index of arrival array ; If event is exit , decrement count of guests . ; of guests . ; Driver Code
using System ; class GFG { static void findMaxGuests ( int [ ] arrl , int [ ] exit , int n ) { Array . Sort ( arrl ) ; Array . Sort ( exit ) ; int guests_in = 1 , max_guests = 1 , time = arrl [ 0 ] ; int i = 1 , j = 0 ; while ( i < n && j < n ) { if ( arrl [ i ] <= exit [ j ] ) { guests_in ++ ; if ( guests_in > max_guests ) { max_guests = guests_in ; time = arrl [ i ] ; } i ++ ; } else { guests_in -- ; j ++ ; } } Console . Write ( " Maximum ▁ Number ▁ of ▁ Guests ▁ = ▁ " + max_guests + " ▁ at ▁ time ▁ " + time ) ; } public static void Main ( ) { int [ ] arrl = { 1 , 2 , 10 , 5 , 5 } ; int [ ] exit = { 4 , 5 , 12 , 9 , 12 } ; int n = arrl . Length ; findMaxGuests ( arrl , exit , n ) ; } }
Find the point where maximum intervals overlap | C # implementation of above approach ; Finding maximum starting time ; Finding maximum ending time ; reating an auxiliary array ; Lazy addition ; Lazily Calculating value at index i ; Driver code
using System ; using System . Linq ; class GFG { public static void maxOverlap ( int [ ] start , int [ ] end , int n ) { int maxa = start . Max ( ) ; int maxb = end . Max ( ) ; int maxc = Math . Max ( maxa , maxb ) ; int [ ] x = new int [ maxc + 2 ] ; int cur = 0 , idx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ++ x [ start [ i ] ] ; -- x [ end [ i ] + 1 ] ; } int maxy = int . MinValue ; for ( int i = 0 ; i <= maxc ; i ++ ) { cur += x [ i ] ; if ( maxy < cur ) { maxy = cur ; idx = i ; } } Console . WriteLine ( " Maximum ▁ value ▁ is ▁ " + maxy + " ▁ at ▁ position : ▁ " + idx + " " ) ; } public static void Main ( ) { int [ ] start = { 13 , 28 , 29 , 14 , 40 , 17 , 3 } ; int [ ] end = { 107 , 95 , 111 , 105 , 70 , 127 , 74 } ; int n = start . Length ; maxOverlap ( start , end , n ) ; } }
Rearrange an array such that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' | Set 1 | A simple C # program to rearrange contents of arr [ ] such that arr [ j ] becomes j if arr [ i ] is j ; A simple method to rearrange ' arr [ 0 . . n - 1 ] ' so that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' ; Create an auxiliary array of same size ; Store result in temp [ ] ; Copy temp back to arr [ ] ; A utility function to print contents of arr [ 0. . n - 1 ] ; Driver program to test above functions
using System ; class RearrangeArray { void rearrangeNaive ( int [ ] arr , int n ) { int [ ] temp = new int [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) temp [ arr [ i ] ] = i ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = temp [ i ] ; } void printArray ( int [ ] arr , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } public static void Main ( ) { RearrangeArray arrange = new RearrangeArray ( ) ; int [ ] arr = { 1 , 3 , 0 , 2 } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ array ▁ is ▁ " ) ; arrange . printArray ( arr , n ) ; arrange . rearrangeNaive ( arr , n ) ; Console . WriteLine ( " Modified ▁ array ▁ is ▁ " ) ; arrange . printArray ( arr , n ) ; } }
Rearrange an array such that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' | Set 1 | A simple C # program to rearrange contents of arr [ ] such that arr [ j ] becomes j if arr [ i ] is j ; A simple method to rearrange ' arr [ 0 . . n - 1 ] ' so that ' arr [ j ] ' becomes ' i ' if ' arr [ i ] ' is ' j ' ; retrieving old value and storing with the new one ; retrieving new value ; A utility function to print contents of arr [ 0. . n - 1 ] ; Driver program
using System ; class GFG { static void rearrange ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ arr [ i ] % n ] += i * n ; } for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] /= n ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static public void Main ( ) { int [ ] arr = { 2 , 0 , 1 , 4 , 5 , 3 } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ array ▁ is ▁ : ▁ " ) ; printArray ( arr , n ) ; rearrange ( arr , n ) ; Console . WriteLine ( " Modified ▁ array ▁ is ▁ : " ) ; printArray ( arr , n ) ; } }
Rearrange an array in maximum minimum form | Set 1 | C # program to rearrange an array in minimum maximum form ; Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; Auxiliary array to hold modified array ; Indexes of smallest and largest elements from remaining array . ; To indicate whether we need to copy rmaining largest or remaining smallest at next position ; Store result in temp [ ] ; Copy temp [ ] to arr [ ] ; Driver Code
using System ; class GFG { static void rearrage ( int [ ] arr , int n ) { int [ ] temp = new int [ n ] ; int small = 0 , large = n - 1 ; bool flag = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( flag ) temp [ i ] = arr [ large -- ] ; else temp [ i ] = arr [ small ++ ] ; flag = ! flag ; } for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = temp [ i ] ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; Console . WriteLine ( " Original ▁ Array " ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; rearrage ( arr , arr . Length ) ; Console . WriteLine ( " STRNEWLINE Modified ▁ Array " ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Minimum number of coins needed to remove all the elements of the array based on given rules | C # implementation for the above approach ; Function to calculate minimum number of coins needed ; Consider the first element separately , add 1 to the total if it 's of type 1 ; Iterate from the second element ; If the current element is of type 2 then any Player can remove the element ; Second pointer to reach end of type 1 elements ; Increment j until arr [ j ] is equal to 1 and j is not out of bounds ; Number of type 1 elements in a continious chunk ; From next iteration i pointer will start from index of j ; Return the minimum count of coins ; Driver Code
using System ; public class GFG { static int minimumcoins ( int [ ] arr , int N ) { int coins = 0 ; int j = 0 ; if ( arr [ 0 ] == 1 ) coins ++ ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 2 ) continue ; j = i ; while ( j < N && arr [ j ] == 1 ) { j ++ ; } int x = ( j - i ) ; coins += x / 3 ; i = j - 1 ; } return coins ; } public static void Main ( String [ ] args ) { int N = 8 ; int [ ] arr = { 1 , 2 , 1 , 1 , 2 , 1 , 1 , 1 } ; Console . WriteLine ( minimumcoins ( arr , N ) ) ; } }
Rearrange an array in maximum minimum form | Set 2 ( O ( 1 ) extra space ) | C # program to rearrange an array in minimum maximum form ; Prints max at first position , min at second position , second max at third position , second min at fourth position and so on . ; initialize index of first minimum and first maximum element ; store maximum element of array ; traverse array elements ; at even index : we have to put maximum element ; at odd index : we have to put minimum element ; array elements back to it 's original form ; Driver code
using System ; class main { public static void rearrange ( int [ ] arr , int n ) { int max_idx = n - 1 , min_idx = 0 ; int max_elem = arr [ n - 1 ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { arr [ i ] += ( arr [ max_idx ] % max_elem ) * max_elem ; max_idx -- ; } else { arr [ i ] += ( arr [ min_idx ] % max_elem ) * max_elem ; min_idx ++ ; } } for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] / max_elem ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . Length ; Console . WriteLine ( " Original ▁ Array " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; rearrange ( arr , n ) ; Console . WriteLine ( " Modified ▁ Array " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Rearrange an array in maximum minimum form | Set 2 ( O ( 1 ) extra space ) | C # program to rearrange an array in minimum maximum form ; Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; initialize index of first minimum and first maximum element ; traverse array elements ; at even index : we have to put maximum element ; at odd index : we have to put minimum element ; Driver code
using System ; class GFG { public static void rearrange ( int [ ] arr , int n ) { int max_ele = arr [ n - 1 ] ; int min_ele = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { arr [ i ] = max_ele ; max_ele -= 1 ; } else { arr [ i ] = min_ele ; min_ele += 1 ; } } } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . Length ; Console . WriteLine ( " Original ▁ Array " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; rearrange ( arr , n ) ; Console . Write ( " STRNEWLINE Modified ▁ Array STRNEWLINE " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Find sum of all left leaves in a given Binary Tree | C # program to find sum of all left leaves ; A binary tree Node has key , pointer to left and right children ; A utility function to check if a given node is leaf or not ; This function returns sum of all left leaves in a given binary tree ; Initialize result ; Update result if root is not NULL ; If left of root is NULL , then add key of left child ; Else recur for left child of root ; Recur for right child of root and update res ; return result ; Driver program
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual bool isLeaf ( Node node ) { if ( node == null ) { return false ; } if ( node . left == null && node . right == null ) { return true ; } return false ; } public virtual int leftLeavesSum ( Node node ) { int res = 0 ; if ( node != null ) { if ( isLeaf ( node . left ) ) { res += node . left . data ; } else { res += leftLeavesSum ( node . left ) ; } res += leftLeavesSum ( node . right ) ; } return res ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 9 ) ; tree . root . right = new Node ( 49 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 5 ) ; tree . root . right . left = new Node ( 23 ) ; tree . root . right . right = new Node ( 52 ) ; tree . root . left . right . right = new Node ( 12 ) ; tree . root . right . right . left = new Node ( 50 ) ; Console . WriteLine ( " The ▁ sum ▁ of ▁ leaves ▁ is ▁ " + tree . leftLeavesSum ( tree . root ) ) ; } }
Move all negative numbers to beginning and positive to end with constant extra space | C # program to put all negative numbers before positive numbers ; A utility function to print an array ; Driver code
using System ; class GFG { static void rearrange ( int [ ] arr , int n ) { int j = 0 , temp ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j ++ ; } } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { - 1 , 2 , - 3 , 4 , 5 , 6 , - 7 , 8 , 9 } ; int n = arr . Length ; rearrange ( arr , n ) ; printArray ( arr , n ) ; } }
Maximize the diamonds by choosing different colour diamonds from adjacent boxes | C # implementation of the approach ; Function to return the maximized value ; Number of rows and columns ; Creating the Dp array ; Populating the first column ; Iterating over all the rows ; Getting the ( i - 1 ) th max value ; Adding it to the current cell ; Getting the max sum from the last column ; Driver code ; Columns are indexed 1 - based
using System ; public class GFG { static int maxSum ( int [ , ] arr ) { int m = ( int ) arr . GetLength ( 0 ) ; int n = ( int ) arr . GetLength ( 1 ) - 1 ; int [ , ] dp = new int [ m + 1 , n + 2 ] ; memset ( arr , 0 , sizeof ( arr ) ) ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n + 1 ; j ++ ) { dp [ i , j ] = 0 ; } } for ( int i = 1 ; i < m ; ++ i ) dp [ i , 1 ] = arr [ i , 1 ] ; for ( int i = 1 ; i < n + 1 ; ++ i ) { for ( int j = 1 ; j < m ; ++ j ) { int mx = 0 ; for ( int k = 1 ; k < m ; ++ k ) { if ( k != j ) { if ( dp [ k , i - 1 ] > mx ) { mx = dp [ k , i - 1 ] ; } } } if ( mx != 0 && arr [ j , i ] != 0 ) { dp [ j , i ] = arr [ j , i ] + mx ; } } } int ans = - 1 ; for ( int i = 1 ; i <= m ; ++ i ) { if ( ( dp [ i , n ] ) != 0 ) ans = Math . Max ( ans , dp [ i , n ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 0 , 0 , 0 , 0 , 0 } , { 0 , 10 , 2 , 20 , 0 } , { 0 , 0 , 0 , 5 , 0 } , { 0 , 0 , 0 , 0 , 6 } , { 0 , 4 , 0 , 11 , 5 } , { 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 } } ; Console . WriteLine ( maxSum ( arr ) ) ; } }
Move all negative elements to end in order with extra space allowed | C # program to Move All - ve Element At End Without changing order Of Array Element ; Moves all - ve element to end of array in same order . ; Create an empty array to store result ; Traversal array and store + ve element in temp array index of temp ; If array contains all positive or all negative . ; Store - ve element in temp array ; Copy contents of temp [ ] to arr [ ] ; Driver code
using System ; class GFG { static void segregateElements ( int [ ] arr , int n ) { int [ ] temp = new int [ n ] ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] >= 0 ) temp [ j ++ ] = arr [ i ] ; if ( j == n j == 0 ) return ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] < 0 ) temp [ j ++ ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = temp [ i ] ; } public static void Main ( ) { int [ ] arr = { 1 , - 1 , - 3 , - 2 , 7 , 5 , 11 , 6 } ; int n = arr . Length ; segregateElements ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Rearrange array such that even index elements are smaller and odd index elements are greater | C # code to rearrange an array such that even index elements are smaller and odd index elements are greater than their next . ; Rearrange ; Utility that prints out an array in a line ; Driver code
using System ; class GFG { static void rearrange ( int [ ] arr , int n ) { int temp ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i % 2 == 0 && arr [ i ] > arr [ i + 1 ] ) { temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } if ( i % 2 != 0 && arr [ i ] < arr [ i + 1 ] ) { temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } } } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 6 , 4 , 2 , 1 , 8 , 3 } ; int n = arr . Length ; Console . WriteLine ( " Before ▁ rearranging : ▁ " ) ; printArray ( arr , n ) ; rearrange ( arr , n ) ; Console . WriteLine ( " After ▁ rearranging : ▁ " ) ; printArray ( arr , n ) ; } }
Minimum distance between duplicates in a String | C # program for the above approach ; This function is used to find minimum distance between same repeating characters ; Store minimum distance between same repeating characters ; For loop to consider each element of string ; Comparison of string characters and updating the minDis value ; As this value would be least therefore break ; If minDis value is not updated that means no repeating characters ; Minimum distance is minDis - 1 ; Driver code ; Given input ; Function call
using System ; class GFG { static int shortestDistance ( string S , int N ) { int minDis = S . Length ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( S [ i ] == S [ j ] && ( j - i ) < minDis ) { minDis = j - i ; break ; } } } if ( minDis == S . Length ) return - 1 ; else return minDis - 1 ; } public static void Main ( String [ ] args ) { string S = " geeksforgeeks " ; int N = 13 ; Console . Write ( shortestDistance ( S , N ) ) ; } }
Positive elements at even and negative at odd positions ( Relative order not maintained ) | C # program to rearrange positive and negative numbers ; Move forward the positive pointer till negative number number not encountered ; Move forward the negative pointer till positive number number not encountered ; Swap array elements to fix their position . ; Break from the while loop when any index exceeds the size of the array ; Driver Code
using System ; class GFG { static void rearrange ( int [ ] a , int size ) { int positive = 0 , negative = 1 , temp ; while ( true ) { while ( positive < size && a [ positive ] >= 0 ) positive += 2 ; while ( negative < size && a [ negative ] <= 0 ) negative += 2 ; if ( positive < size && negative < size ) { temp = a [ positive ] ; a [ positive ] = a [ negative ] ; a [ negative ] = temp ; } else break ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 } ; int n = arr . Length ; rearrange ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Print all numbers that can be obtained by adding A or B to N exactly M times | C # program for tha above approach ; Function to find all possible numbers that can be obtained by adding A or B to N exactly M times ; For maintaining increasing order ; Smallest number that can be achieved ; If A and B are equal , the only number that can be onbtained is N + M * A ; For finding other numbers , subtract A from number 1 time and add B to number 1 time ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static void possibleNumbers ( int N , int M , int A , int B ) { if ( A > B ) { int temp = A ; A = B ; B = temp ; } int number = N + M * A ; Console . Write ( number + " ▁ " ) ; if ( A != B ) { for ( int i = 0 ; i < M ; i ++ ) { number = number - A + B ; Console . Write ( number + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int N = 5 , M = 3 , A = 4 , B = 6 ; possibleNumbers ( N , M , A , B ) ; } }
Positive elements at even and negative at odd positions ( Relative order not maintained ) | C # program to rearrange positive and negative numbers ; Swap function ; Print array function ; Driver code ; before modification ; out of order positive element ; find out of order negative element in remaining array ; out of order negative element ; find out of order positive element in remaining array ; after modification
using System ; class GFG { static void swap ( int [ ] a , int i , int j ) { int temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; } static void printArray ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 } ; int n = arr . Length ; printArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && i % 2 == 1 ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < 0 && j % 2 == 0 ) { swap ( arr , i , j ) ; break ; } } } else if ( arr [ i ] < 0 && i % 2 == 0 ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] >= 0 && j % 2 == 1 ) { swap ( arr , i , j ) ; break ; } } } } printArray ( arr , n ) ; } }
Segregate even and odd numbers | Set 3 | C # code to segregate even odd numbers in an array ; Function to segregate even odd numbers ; Swapping even and odd numbers ; Printing segregated array ; Driver code
using System ; class GFG { static void arrayEvenAndOdd ( int [ ] arr , int n ) { int i = - 1 , j = 0 ; while ( j != n ) { if ( arr [ j ] % 2 == 0 ) { i ++ ; int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } j ++ ; } for ( int k = 0 ; k < n ; k ++ ) Console . Write ( arr [ k ] + " ▁ " ) ; } static void Main ( ) { int [ ] arr = { 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 } ; int n = arr . Length ; arrayEvenAndOdd ( arr , n ) ; } }
Find sum of all left leaves in a given Binary Tree | C # program to find sum of all left leaves ; A binary tree Node has key , pointer to left and right children ; Passing sum as accumulator and implementing pass by reference of sum variable ; Pass in a sum variable as an accumulator ; Check whether this node is a leaf node and is left . ; Pass true for left and false for right ; A wrapper over above recursive function ; use the above recursive function to evaluate sum ; Driver program ; Let us construct the Binary Tree shown in the above figure
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class Sum { public int sum = 0 ; } public class BinaryTree { public Node root ; public virtual void leftLeavesSumRec ( Node node , bool isleft , Sum summ ) { if ( node == null ) { return ; } if ( node . left == null && node . right == null && isleft ) { summ . sum = summ . sum + node . data ; } leftLeavesSumRec ( node . left , true , summ ) ; leftLeavesSumRec ( node . right , false , summ ) ; } public virtual int leftLeavesSum ( Node node ) { Sum suum = new Sum ( ) ; leftLeavesSumRec ( node , false , suum ) ; return suum . sum ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 9 ) ; tree . root . right = new Node ( 49 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 5 ) ; tree . root . right . left = new Node ( 23 ) ; tree . root . right . right = new Node ( 52 ) ; tree . root . left . right . right = new Node ( 12 ) ; tree . root . right . right . left = new Node ( 50 ) ; Console . WriteLine ( " The ▁ sum ▁ of ▁ leaves ▁ is ▁ " + tree . leftLeavesSum ( tree . root ) ) ; } }