codeparrot/xlcost-text-to-code huggingface dataset:
Find a number in minimum steps | C # program to Find a number in minimum steps ; Steps sequence ; Current sum ; Sign of the number ; Basic steps required to get sum >= required value . ; If we have reached ahead to destination . ; If the last step was an odd number , then it has following mechanism for negating a particular number and decreasing the sum to required number Also note that it may require 1 more step in order to reach the sum . ; If the current time instance is even and sum is odd than it takes 2 more steps and few negations in previous elements to reach there . ; Driver Program
using System ; using System . Collections . Generic ; public class GFG { static List < int > find ( int n ) { List < int > ans = new List < int > ( ) ; int sum = 0 ; int i = 1 ; int sign = ( n >= 0 ? 1 : - 1 ) ; n = Math . Abs ( n ) ; for ( ; sum < n ; ) { ans . Add ( sign * i ) ; sum += i ; i ++ ; } if ( sum > sign * n ) { if ( i % 2 != 0 ) { sum -= n ; if ( sum % 2 != 0 ) { ans . Add ( sign * i ) ; sum += i ; i ++ ; } int a = ans [ ( ( sum / 2 ) - 1 ) ] ; ans . RemoveAt ( ( sum / 2 ) - 1 ) ; ans . Insert ( ( ( sum / 2 ) - 1 ) , a * ( - 1 ) ) ; } else { sum -= n ; if ( sum % 2 != 0 ) { sum -- ; ans . Add ( sign * i ) ; ans . Add ( sign * - 1 * ( i + 1 ) ) ; } ans . Insert ( ( sum / 2 ) - 1 , ans [ ( sum / 2 ) - 1 ] * - 1 ) ; } } return ans ; } public static void Main ( String [ ] args ) { int n = 20 ; if ( n == 0 ) Console . Write ( " Minimum ▁ number ▁ of ▁ Steps : ▁ 0 STRNEWLINE Step ▁ sequence : STRNEWLINE 0" ) ; else { List < int > a = find ( n ) ; Console . Write ( " Minimum ▁ number ▁ of ▁ Steps : ▁ " + a . Count + " STRNEWLINE Step ▁ sequence : STRNEWLINE " ) ; foreach ( int i in a ) Console . Write ( i + " ▁ " ) ; } } }
Maximum range length such that A [ i ] is maximum in given range for all i from [ 1 , N ] | C # program for the above approach . ; Function to find maximum range for each i such that arr [ i ] is max in range ; Vector to store the left and right index for each i such that left [ i ] > arr [ i ] and right [ i ] > arr [ i ] ; Traverse the array ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify left [ i ] ; Clear the stack ; Traverse the array to find right [ i ] for each i ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify right [ i ] ; Print the value range for each i ; adding elements in firstlist ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void MaxRange ( List < int > A , int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; Stack s = new Stack ( ) ; s . Push ( new Tuple < int , int > ( Int32 . MaxValue , - 1 ) ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( ( ( Tuple < int , int > ) s . Peek ( ) ) . Item1 < A [ i ] ) s . Pop ( ) ; left [ i ] = ( ( Tuple < int , int > ) s . Peek ( ) ) . Item2 ; s . Push ( new Tuple < int , int > ( A [ i ] , i ) ) ; } while ( s . Count > 0 ) s . Pop ( ) ; s . Push ( new Tuple < int , int > ( Int32 . MaxValue , n ) ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( ( ( Tuple < int , int > ) s . Peek ( ) ) . Item1 < A [ i ] ) s . Pop ( ) ; right [ i ] = ( ( Tuple < int , int > ) s . Peek ( ) ) . Item2 ; s . Push ( new Tuple < int , int > ( A [ i ] , i ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { Console . WriteLine ( ( left [ i ] + 1 ) + " ▁ " + ( right [ i ] - 1 ) ) ; } } static void Main ( ) { List < int > arr = new List < int > ( ) ; arr . Add ( 1 ) ; arr . Add ( 3 ) ; arr . Add ( 2 ) ; int n = arr . Count ; MaxRange ( arr , n ) ; } }
Find Nth smallest number having exactly 4 divisors | C # program for the above approach ; Function to find minimum number of elements required to obtain sum K ; Function to find the nth number which has exactly 4 divisors ; The divs [ ] array to store number of divisors of every element ; The vis [ ] array to check if given number is considered or not ; The cnt stores number of elements having exactly 4 divisors ; Iterate while cnt less than n ; If i is a prime ; Iterate in the range [ 2 * i , 1000000 ] with increment of i ; If the number j is already considered ; Dividing currNum by i until currNum % i is equal to 0 ; Case a single prime in its factorization ; Case of two distinct primes which divides j exactly once each ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static int nthNumber ( int n ) { int [ ] divs = new int [ 1000000 ] ; int [ ] vis = new int [ 1000000 ] ; int cnt = 0 ; for ( int i = 2 ; cnt < n ; i ++ ) { if ( divs [ i ] == 0 ) { for ( int j = 2 * i ; j < 1000000 ; j += i ) { if ( vis [ j ] != 0 ) { continue ; } vis [ j ] = 1 ; int currNum = j ; int count = 0 ; while ( currNum % i == 0 ) { divs [ j ] ++ ; currNum = currNum / i ; count ++ ; } if ( currNum == 1 && count == 3 && divs [ j ] == 3 ) { cnt ++ ; } else if ( currNum != 1 && divs [ currNum ] == 0 && count == 1 && divs [ j ] == 1 ) { cnt ++ ; } if ( cnt == n ) { return j ; } } } } return - 1 ; } static public void Main ( ) { int N = 24 ; Console . Write ( nthNumber ( N ) ) ; } }
Maximum number formed from the digits of given three numbers | C # program for the above approach ; Function to find the maximum number formed by taking the maximum digit at the same position from each number ; Stores the result ; Stores the position value of a digit ; Stores the digit at the unit place ; Stores the digit at the unit place ; Stores the digit at the unit place ; Update A , B and C ; Stores the maximum digit ; Increment ans cur * a ; Update cur ; Return ans ; Driver Code ; Given Input ; Function call
using System ; class GFG { static int findkey ( int A , int B , int C ) { int ans = 0 ; int cur = 1 ; while ( A > 0 ) { int a = A % 10 ; int b = B % 10 ; int c = C % 10 ; A = A / 10 ; B = B / 10 ; C = C / 10 ; int m = Math . Max ( a , Math . Max ( c , b ) ) ; ans += cur * m ; cur = cur * 10 ; } return ans ; } static public void Main ( ) { int A = 3521 , B = 2452 , C = 1352 ; Console . Write ( findkey ( A , B , C ) ) ; } }
Count of distinct GCDs among all the non | C # program for the above approach ; Function to calculate gcd ; Function to calculate the number of distinct GCDs among all non - empty subsequences of an array ; Variables to store the largest element in array and the required count ; Map to store whether a number is present in A ; Calculate largest number in A and mapping A to Mp ; Iterate over all possible values of GCD ; Variable to check current GCD ; Iterate over all multiples of i ; If j is present in A ; Calculate gcd of all encountered multiples of i ; Current GCD is possible ; Return answer ; Driver code ; Input ; Function calling
using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static int distinctGCDs ( int [ ] arr , int N ) { int M = - 1 , ans = 0 ; Dictionary < int , int > Mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { M = Math . Max ( M , arr [ i ] ) ; if ( Mp . ContainsKey ( arr [ i ] ) ) Mp [ arr [ i ] ] = 1 ; else Mp . Add ( arr [ i ] , 1 ) ; } for ( int i = 1 ; i <= M ; i ++ ) { int currGcd = 0 ; for ( int j = i ; j <= M ; j += i ) { if ( Mp . ContainsKey ( j ) ) { currGcd = gcd ( currGcd , j ) ; if ( currGcd == i ) { ans ++ ; break ; } } } } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 11 , 14 , 6 , 12 } ; int N = arr . Length ; Console . Write ( distinctGCDs ( arr , N ) ) ; } }
Egg Dropping Puzzle | DP |  ; Function to get minimum number of trials needed in worst case with n eggs and k floors ; If there are no floors , then no trials needed . OR if there is one floor , one trial needed . ; We need k trials for one egg and k floors ; Consider all droppings from 1 st floor to kth floor and return the minimum of these values plus 1. ; Driver code
using System ; class GFG { static int eggDrop ( int n , int k ) { if ( k == 1 k == 0 ) return k ; if ( n == 1 ) return k ; int min = int . MaxValue ; int x , res ; for ( x = 1 ; x <= k ; x ++ ) { res = Math . Max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ; if ( res < min ) min = res ; } return min + 1 ; } static void Main ( ) { int n = 2 , k = 10 ; Console . Write ( " Minimum ▁ number ▁ of ▁ " + " trials ▁ in ▁ worst ▁ case ▁ with ▁ " + n + " ▁ eggs ▁ and ▁ " + k + " ▁ floors ▁ is ▁ " + eggDrop ( n , k ) ) ; } }
K | C # program for the above approach ; Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Divide the number n by 10 upto k - 1 times ; If the number n is equal 0 ; Print the right most digit ; Driver code ; Given input ; Function call
using System ; class GFG { public static void kthDigitFromLast ( int n , int k ) { if ( k <= 0 ) { Console . Write ( - 1 ) ; return ; } while ( ( k - 1 ) > 0 && n > 0 ) { n = n / 10 ; k -- ; } if ( n == 0 ) { Console . Write ( - 1 ) ; } else { Console . Write ( n % 10 ) ; } } public static void Main ( String [ ] args ) { int n = 2354 ; int k = 2 ; kthDigitFromLast ( n , k ) ; } }
Edit Distance | DP | A Space efficient Dynamic Programming based C # program to find minimum number operations to convert str1 to str2 ; Create a DP array to memoize result of previous computations ; Base condition when second String is empty then we remove all characters ; Start filling the DP This loop run for every character in second String ; This loop compares the char from second String with first String characters ; if first String is empty then we have to perform add character operation to get second String ; if character from both String is same then we do not perform any operation . here i % 2 is for bound the row number . ; if character from both String is not same then we take the minimum from three specified operation ; after complete fill the DP array if the len2 is even then we end up in the 0 th row else we end up in the 1 th row so we take len2 % 2 to get row ; Driver program
using System ; class GFG { static void EditDistDP ( String str1 , String str2 ) { int len1 = str1 . Length ; int len2 = str2 . Length ; int [ , ] DP = new int [ 2 , len1 + 1 ] ; for ( int i = 0 ; i <= len1 ; i ++ ) DP [ 0 , i ] = i ; for ( int i = 1 ; i <= len2 ; i ++ ) { for ( int j = 0 ; j <= len1 ; j ++ ) { if ( j == 0 ) DP [ i % 2 , j ] = i ; else if ( str1 [ j - 1 ] == str2 [ i - 1 ] ) { DP [ i % 2 , j ] = DP [ ( i - 1 ) % 2 , j - 1 ] ; } else { DP [ i % 2 , j ] = 1 + Math . Min ( DP [ ( i - 1 ) % 2 , j ] , Math . Min ( DP [ i % 2 , j - 1 ] , DP [ ( i - 1 ) % 2 , j - 1 ] ) ) ; } } } Console . Write ( DP [ len2 % 2 , len1 ] + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String str1 = " food " ; String str2 = " money " ; EditDistDP ( str1 , str2 ) ; } }
Sum of Bitwise XOR of elements of an array with all elements of another array | C # Program for the above approach ; Function to calculate sum of Bitwise XOR of elements of arr [ ] with k ; Initialize sum to be zero ; Iterate over each set bit ; Stores contribution of i - th bet to the sum ; If the i - th bit is set ; Stores count of elements whose i - th bit is not set ; Update value ; Update value ; Add value to sum ; Move to the next power of two ; Stores the count of elements whose i - th bit is set ; Initialize count to 0 for all positions ; Traverse the array ; Iterate over each bit ; If the i - th bit is set ; Increase count ; Driver Code
using System ; public class GFG { static int xorSumOfArray ( int [ ] arr , int n , int k , int [ ] count ) { int sum = 0 ; int p = 1 ; for ( int i = 0 ; i < 31 ; i ++ ) { int val = 0 ; if ( ( k & ( 1 << i ) ) != 0 ) { int not_set = n - count [ i ] ; val = ( ( not_set ) * p ) ; } else { val = ( count [ i ] * p ) ; } sum += val ; p = ( p * 2 ) ; } return sum ; } static void sumOfXors ( int [ ] arr , int n , int [ ] queries , int q ) { int [ ] count = new int [ 32 ] ; for ( int i = 0 ; i < 32 ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 31 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) != 0 ) count [ j ] ++ ; } } for ( int i = 0 ; i < q ; i ++ ) { int k = queries [ i ] ; Console . Write ( xorSumOfArray ( arr , n , k , count ) + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 5 , 2 , 3 } ; int [ ] queries = { 3 , 8 , 7 } ; int n = arr . Length ; int q = queries . Length ; sumOfXors ( arr , n , queries , q ) ; } }
Factor Tree of a given Number | C # program to conFactor Tree for a given number ; Tree node ; Utility function to create a new tree Node ; Constructs factor tree for given value and stores root of tree at given reference . ; the number is factorized ; If we found a factor , we conleft and right subtrees and return . Since we traverse factors starting from smaller to greater , left child will always have smaller factor ; Iterative method to find the height of Binary Tree ; Base Case ; Print front of queue and remove it from queue ; Driver program ; int val = 48 ; sample value
using System ; using System . Collections . Generic ; public class GFG { public class Node { public Node left , right ; public int key ; } ; static Node root ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return temp ; } static Node createFactorTree ( Node node_ref , int v ) { ( node_ref ) = newNode ( v ) ; for ( int i = 2 ; i < v / 2 ; i ++ ) { if ( v % i != 0 ) continue ; node_ref . left = createFactorTree ( ( ( node_ref ) . left ) , i ) ; node_ref . right = createFactorTree ( ( ( node_ref ) . right ) , v / i ) ; return node_ref ; } return node_ref ; } static void printLevelOrder ( Node root ) { if ( root == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 ) { Node node = q . Peek ( ) ; Console . Write ( node . key + " ▁ " ) ; q . Dequeue ( ) ; if ( node . left != null ) q . Enqueue ( node . left ) ; if ( node . right != null ) q . Enqueue ( node . right ) ; } } public static void Main ( String [ ] args ) { root = null ; root = createFactorTree ( root , val ) ; Console . WriteLine ( " Level ▁ order ▁ traversal ▁ of ▁ " + " constructed ▁ factor ▁ tree " ) ; printLevelOrder ( root ) ; } }
Maximum height of an elevation possible such that adjacent matrix cells have a difference of at most height 1 | C # program for the above approach ; Utility function to find the matrix having the maximum height ; Stores index pairs for bfs ; Stores info about the visited cells ; Traverse the matrix ; Breadth First Search ; x & y are the row & column of current cell ; Check all the 4 adjacent cells and marking them as visited if not visited yet also marking their height as 1 + height of cell ( x , y ) ; Function to find the matrix having the maximum height ; Stores output matrix ; Calling the helper function ; Print the readonly output matrix ; Driver Code ; Given matrix ; Function call to find the matrix having the maximum height
using System ; using System . Collections . Generic ; public class GFG { static readonly int M = 3 ; static readonly int N = 3 ; class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findHeightMatrixUtil ( int [ , ] mat , int [ , ] height ) { Queue < pair > q = new Queue < pair > ( ) ; int [ , ] vis = new int [ M , N ] ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mat [ i , j ] == 1 ) { q . Enqueue ( new pair ( i , j ) ) ; height [ i , j ] = 0 ; vis [ i , j ] = 1 ; } } } while ( q . Count != 0 ) { pair k = q . Peek ( ) ; q . Dequeue ( ) ; int x = k . first , y = k . second ; if ( x > 0 && vis [ x - 1 , y ] == 0 ) { height [ x - 1 , y ] = height [ x , y ] + 1 ; vis [ x - 1 , y ] = 1 ; q . Enqueue ( new pair ( x - 1 , y ) ) ; } if ( y > 0 && vis [ x , y - 1 ] == 0 ) { height [ x , y - 1 ] = height [ x , y ] + 1 ; vis [ x , y - 1 ] = 1 ; q . Enqueue ( new pair ( x , y - 1 ) ) ; } if ( x < M - 1 && vis [ x + 1 , y ] == 0 ) { height [ x + 1 , y ] = height [ x , y ] + 1 ; vis [ x + 1 , y ] = 1 ; q . Enqueue ( new pair ( x + 1 , y ) ) ; } if ( y < N - 1 && vis [ x , y + 1 ] == 0 ) { height [ x , y + 1 ] = height [ x , y ] + 1 ; vis [ x , y + 1 ] = 1 ; q . Enqueue ( new pair ( x , y + 1 ) ) ; } } } static void findHeightMatrix ( int [ , ] mat ) { int [ , ] height = new int [ M , N ] ; findHeightMatrixUtil ( mat , height ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) Console . Write ( height [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 0 } } ; findHeightMatrix ( mat ) ; } }
Median of all nodes from a given range in a Binary Search Tree ( BST ) | C # program for the above approach ; Tree Node structure ; Function to create a new BST node ; Function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Return the node pointer ; Function to find all the nodes that lies over the range [ node1 , node2 ] ; If the tree is empty , return ; Traverse for the left subtree ; If a second node is found , then update the flag as false ; Traverse the right subtree ; Function to find the median of all the values in the given BST that lies over the range [ node1 , node2 ] ; Stores all the nodes in the range [ node1 , node2 ] ; Store the size of the array ; Print the median of array based on the size of array ; Driver Code ; Given BST
using System ; using System . Collections . Generic ; public class GFG { class Node { public Node left , right ; public int key ; } ; static List < int > interNodes = new List < int > ( ) ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return temp ; } static Node insertNode ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insertNode ( node . left , key ) ; else if ( key > node . key ) node . right = insertNode ( node . right , key ) ; return node ; } static void getIntermediateNodes ( Node root , int node1 , int node2 ) { if ( root == null ) return ; getIntermediateNodes ( root . left , node1 , node2 ) ; if ( root . key <= node2 && root . key >= node1 ) { interNodes . Add ( root . key ) ; } getIntermediateNodes ( root . right , node1 , node2 ) ; } static float findMedian ( Node root , int node1 , int node2 ) { getIntermediateNodes ( root , node1 , node2 ) ; int nSize = interNodes . Count ; return ( nSize % 2 == 1 ) ? ( float ) interNodes [ nSize / 2 ] : ( float ) ( interNodes [ ( nSize - 1 ) / 2 ] + interNodes [ nSize / 2 ] ) / 2 ; } public static void Main ( String [ ] args ) { Node root = null ; root = insertNode ( root , 8 ) ; insertNode ( root , 3 ) ; insertNode ( root , 1 ) ; insertNode ( root , 6 ) ; insertNode ( root , 4 ) ; insertNode ( root , 11 ) ; insertNode ( root , 15 ) ; Console . Write ( findMedian ( root , 3 , 11 ) ) ; } }
Count number of pairs ( i , j ) up to N that can be made equal on multiplying with a pair from the range [ 1 , N / 2 ] | C # program for the above approach ; Function to compute totient of all numbers smaller than or equal to N ; Iterate over the range [ 2 , N ] ; If phi [ p ] is not computed already , then p is prime ; Phi of a prime number p is ( p - 1 ) ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Function to count the pairs ( i , j ) from the range [ 1 , N ] , satisfying the given condition ; Stores the counts of first and second type of pairs respectively ; Count of first type of pairs ; Stores the phi or totient values ; Calculate the Phi values ; Iterate over the range [ N / 2 + 1 , N ] ; Update the value of cnt_type2 ; Print the total count ; Driver Code
using System ; class GFG { static void computeTotient ( int N , int [ ] phi ) { for ( int p = 2 ; p <= N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i <= N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } static void countPairs ( int N ) { int cnt_type1 = 0 , cnt_type2 = 0 ; int half_N = N / 2 ; cnt_type1 = ( half_N * ( half_N - 1 ) ) / 2 ; int [ ] phi = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { phi [ i ] = i ; } computeTotient ( N , phi ) ; for ( int i = ( N / 2 ) + 1 ; i <= N ; i ++ ) cnt_type2 += ( i - phi [ i ] - 1 ) ; Console . Write ( cnt_type1 + cnt_type2 ) ; } public static void Main ( ) { int N = 6 ; countPairs ( N ) ; } }
Sum of subsets nearest to K possible from two given arrays | C # program of the above approach ; Stores the sum closest to K ; Stores the minimum absolute difference ; Function to choose the elements from the array B [ ] ; If absolute difference is less then minimum value ; Update the minimum value ; Update the value of ans ; If absolute difference between curr and K is equal to minimum ; Update the value of ans ; If i is greater than M - 1 ; Includes the element B [ i ] once ; Includes the element B [ i ] twice ; Excludes the element B [ i ] ; Function to find a subset sum whose sum is closest to K ; Traverse the array A [ ] ; Function Call ; Return the ans ; Driver Code ; Input ; Function Call
using System ; class GFG { static int ans = Int32 . MaxValue ; static int mini = Int32 . MaxValue ; static void findClosestTarget ( int i , int curr , int [ ] B , int M , int K ) { if ( Math . Abs ( curr - K ) < mini ) { mini = Math . Abs ( curr - K ) ; ans = curr ; } if ( Math . Abs ( curr - K ) == mini ) { ans = Math . Min ( ans , curr ) ; } if ( i >= M ) return ; findClosestTarget ( i + 1 , curr + B [ i ] , B , M , K ) ; findClosestTarget ( i + 1 , curr + 2 * B [ i ] , B , M , K ) ; findClosestTarget ( i + 1 , curr , B , M , K ) ; } static int findClosest ( int [ ] A , int [ ] B , int N , int M , int K ) { for ( int i = 0 ; i < N ; i ++ ) { findClosestTarget ( 0 , A [ i ] , B , M , K ) ; } return ans ; } public static void Main ( ) { int [ ] A = { 2 , 3 } ; int [ ] B = { 4 , 5 , 30 } ; int N = A . Length ; int M = B . Length ; int K = 18 ; Console . WriteLine ( findClosest ( A , B , N , M , K ) ) ; } }
Check if every node can me made accessible from a node of a Tree by at most N / 2 given operations | C # program for the above approach ; Function to check if there is a node in tree from where all other nodes are accessible or not ; Store the indegree of every node ; Store the nodes having indegree equal to 0 ; Traverse the array ; If the indegree of i - th node is 0 ; Increment count0 by 1 ; If the number of operations needed is at most floor ( n / 2 ) ; Otherwise ; Driver code ; Given number of nodes ; Given Directed Tree
using System ; using System . Collections . Generic ; public class GFG { public static void findNode ( Dictionary < int , int > map , int n ) { int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . ContainsKey ( i + 1 ) ) a [ i ] = map [ i + 1 ] ; else a [ i ] = 0 ; } int count0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count0 ++ ; } } count0 -= 1 ; if ( count0 <= Math . Floor ( ( ( double ) n ) / ( ( double ) 2 ) ) ) { Console . WriteLine ( " Yes " ) ; } else Console . WriteLine ( " No ▁ " ) ; } static public void Main ( ) { int N = 3 ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; map [ 1 ] = 0 ; map [ 2 ] = 2 ; map [ 3 ] = 0 ; findNode ( map , N ) ; } }