codeparrot/xlcost-text-to-code huggingface dataset:
Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | C # program for the above approach ; Function to convert obtained binary representation to decimal value ; Stores the resultant number ; Traverse string arr ; Return the number formed ; Function to count the number of set bits in the number num ; Stores the count of set bits ; Iterate until num > 0 ; Function to check if the given matrix satisfies the given condition or not ; To get P , S , MR , and MC ; Stores decimal equivalents of binary representations ; Gett the number of set bits ; Print the answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int convert ( List < int > p ) { int ans = 0 ; foreach ( int i in p ) { ans = ( ans << 1 ) | i ; } return ans ; } static int count ( int num ) { int ans = 0 ; while ( num > 0 ) { ans += num & 1 ; num >>= 1 ; } return ans ; } static void checkGoodMatrix ( int [ , ] mat ) { List < int > P = new List < int > ( ) ; List < int > S = new List < int > ( ) ; List < int > MR = new List < int > ( ) ; List < int > MC = new List < int > ( ) ; for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < mat . GetLength ( 1 ) ; j ++ ) { if ( i == j ) P . Add ( mat [ i , j ] ) ; if ( i + j == mat . GetLength ( 0 ) - 1 ) S . Add ( mat [ i , j ] ) ; if ( i == Math . Floor ( ( mat . GetLength ( 0 ) - 1 ) / 2.0 ) ) MR . Add ( mat [ i , j ] ) ; if ( j == Math . Floor ( ( mat . GetLength ( 0 ) - 1 ) / 2.0 ) ) MC . Add ( mat [ i , j ] ) ; } } S . Reverse ( ) ; int P0 = convert ( P ) ; int S0 = convert ( S ) ; int MR0 = convert ( MR ) ; int MC0 = convert ( MC ) ; int setBitsPS = count ( ( P0 & S0 ) ) ; int setBitsMM = count ( ( MR0 & MC0 ) ) ; if ( setBitsPS > setBitsMM ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( string [ ] args ) { int [ , ] mat = { { 1 , 0 , 1 } , { 0 , 0 , 1 } , { 0 , 1 , 1 } } ; checkGoodMatrix ( mat ) ; } }
Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path | C # program for the above approach ; Function to assign values to nodes of the tree s . t . sum of values of nodes of path between any 2 nodes is not divisible by length of path ; Stores the adjacency list ; Create a adjacency list ; Stores whether node is visited or not ; Stores the node values ; Variable used to assign values to the nodes alternatively to the parent child ; Declare a queue ; Push the 1 st node ; Assign K value to this node ; Dequeue the node ; Mark it as visited ; Upgrade the value of K ; Assign K to the child nodes ; If the child is unvisited ; Enqueue the child ; Assign K to the child ; Print the value assigned to the nodes ; Driver code ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; public class GFG { static void assignValues ( int [ , ] Edges , int n ) { LinkedList < int > [ ] tree = new LinkedList < int > [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) tree [ i ] = new LinkedList < int > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; tree [ u ] . AddLast ( v ) ; tree [ v ] . AddLast ( u ) ; } bool [ ] visited = new bool [ n + 1 ] ; int [ ] answer = new int [ n + 1 ] ; int K = 1 ; Queue q = new Queue ( ) ; q . Enqueue ( 1 ) ; answer [ 1 ] = K ; while ( q . Count > 0 ) { int node = ( int ) q . Peek ( ) ; q . Dequeue ( ) ; visited [ node ] = true ; K = ( ( answer [ node ] == 1 ) ? 2 : 1 ) ; foreach ( var child in tree [ node ] ) { if ( ! visited [ child ] ) { q . Enqueue ( child ) ; answer [ child ] = K ; } } } for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( answer [ i ] + " ▁ " ) ; } } static public void Main ( ) { int N = 11 ; int [ , ] Edges = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 1 , 5 } , { 2 , 6 } , { 2 , 10 } , { 10 , 11 } , { 3 , 7 } , { 4 , 8 } , { 5 , 9 } } ; assignValues ( Edges , N ) ; } }
Number of full binary trees such that each node is product of its children | C # program to find number of full binary tree such that each node is product of its children . ; Return the number of all possible full binary tree with given product property . ; Finding the minimum and maximum values in given array . ; Marking the presence of each array element and initialising the number of possible full binary tree for each integer equal to 1 because single node will also contribute as a full binary tree . ; From minimum value to maximum value of array finding the number of all possible Full Binary Trees . ; Find if value present in the array ; For each multiple of i , less than equal to maximum value of array ; If multiple is not present in the array then continue . ; Finding the number of possible Full binary trees for multiple j by multiplying number of possible Full binary tree from the number i and number of possible Full binary tree from i / j . ; Condition for possiblity when left chid became right child and vice versa . ; Driver code
using System ; class GFG { static int numoffbt ( int [ ] arr , int n ) { int maxvalue = - 2147483647 , minvalue = 2147483647 ; for ( int i = 0 ; i < n ; i ++ ) { maxvalue = Math . Max ( maxvalue , arr [ i ] ) ; minvalue = Math . Min ( minvalue , arr [ i ] ) ; } int [ ] mark = new int [ maxvalue + 2 ] ; int [ ] value = new int [ maxvalue + 2 ] ; for ( int i = 0 ; i < maxvalue + 2 ; i ++ ) { mark [ i ] = 0 ; value [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { mark [ arr [ i ] ] = 1 ; value [ arr [ i ] ] = 1 ; } int ans = 0 ; for ( int i = minvalue ; i <= maxvalue ; i ++ ) { if ( mark [ i ] != 0 ) { for ( int j = i + i ; j <= maxvalue && j / i <= i ; j += i ) { if ( mark [ j ] == 0 ) continue ; value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; if ( i != j / i ) value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; } } ans += value [ i ] ; } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 6 } ; int n = arr . Length ; Console . Write ( numoffbt ( arr , n ) ) ; } }
Minimum prime numbers required to be subtracted to make all array elements equal | C # program for the above approach ; Stores the sieve of prime numbers ; Function that performs the Sieve of Eratosthenes ; Initialize all numbers as prime ; Iterate over the range [ 2 , 1000 ] ; If the current element is a prime number ; Mark all its multiples as false ; Function to find the minimum number of subtraction of primes numbers required to make all array elements the same ; Perform sieve of eratosthenes ; Find the minimum value ; Stores the value to each array element should be reduced ; If an element exists with value ( M + 1 ) ; Stores the minimum count of subtraction of prime numbers ; Traverse the array ; If D is equal to 0 ; If D is a prime number ; Increase count by 1 ; If D is an even number ; Increase count by 2 ; If D - 2 is prime ; Increase count by 2 ; Otherwise , increase count by 3 ; Driver Code
using System ; class GFG { static int limit = 100000 ; static bool [ ] prime ; static void sieve ( ) { prime = new bool [ limit + 1 ] ; Array . Fill ( prime , true ) ; for ( int p = 2 ; p * p <= limit ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= limit ; i += p ) prime [ i ] = false ; } } } static int findOperations ( int [ ] arr , int n ) { sieve ( ) ; int minm = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { minm = Math . Min ( minm , arr [ i ] ) ; } int val = minm ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == minm + 1 ) { val = minm - 2 ; break ; } } int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int D = arr [ i ] - val ; if ( D == 0 ) { continue ; } else if ( prime [ D ] == true ) { cnt += 1 ; } else if ( D % 2 == 0 ) { cnt += 2 ; } else { if ( prime [ D - 2 ] == true ) { cnt += 2 ; } else { cnt += 3 ; } } } return cnt ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 10 , 4 , 5 } ; int N = 4 ; Console . WriteLine ( findOperations ( arr , N ) ) ; } }
Maximize sum of Bitwise AND of same | C # program for the above approach ; Function to calculate sum of Bitwise AND of same indexed elements of the arrays p [ ] and arr [ ] ; Stores the resultant sum ; Traverse the array ; Update sum of Bitwise AND ; Return the value obtained ; Function to generate all permutations and calculate the maximum sum of Bitwise AND of same indexed elements present in any permutation and an array arr [ ] ; If the size of the array is N ; Calculate cost of permutation ; Generate all permutations ; Update chosen [ i ] ; Update the permutation p [ ] ; Generate remaining permutations ; Return the resultant sum ; Function to find the maximum sum of Bitwise AND of same indexed elements in a permutation of first N natural numbers and arr [ ] ; Stores the resultant maximum sum ; Stores the generated permutation P ; Function call to store result ; Print the result ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int calcScore ( List < int > p , int [ ] arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { ans += ( p [ i ] & arr [ i ] ) ; } return ans ; } static int getMaxUtil ( List < int > p , int [ ] arr , int ans , bool [ ] chosen , int N ) { if ( p . Count == N ) { ans = Math . Max ( ans , calcScore ( p , arr ) ) ; return ans ; } for ( int i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) { continue ; } chosen [ i ] = true ; p . Add ( i ) ; ans = getMaxUtil ( p , arr , ans , chosen , N ) ; chosen [ i ] = false ; p . Remove ( p . Count - 1 ) ; } return ans ; } static void getMax ( int [ ] arr , int N ) { int ans = 0 ; bool [ ] chosen = new bool [ N ] ; List < int > p = new List < int > ( ) ; int res = getMaxUtil ( p , arr , ans , chosen , N ) ; Console . Write ( res ) ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 3 , 6 } ; int N = arr . Length ; getMax ( arr , N ) ; } }
Sum of array elements whose count of set bits are unique | C # program for the approach ; Function to count the number of set bits in an integer N ; Stores the count of set bits ; Iterate until N is non - zero ; Stores the resultant count ; Function to calculate sum of all array elements whose count of set bits are unique ; Stores frequency of all possible count of set bits ; Stores the sum of array elements ; Traverse the array ; Count the number of set bits ; Traverse the array And Update the value of ans ; If frequency is 1 ; Driver Code
using System ; using System . Collections . Generic ; class solution { static int setBitCount ( int n ) { int ans = 0 ; while ( n > 0 ) { ans += n & 1 ; n >>= 1 ; } return ans ; } static void getSum ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int key = setBitCount ( arr [ i ] ) ; if ( mp . ContainsKey ( key ) ) mp [ key ] += 1 ; else mp [ key ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) { int key = setBitCount ( arr [ i ] ) ; if ( mp [ key ] == 1 ) ans += arr [ i ] ; } Console . Write ( ans ) ; } public static void Main ( ) { int [ ] arr = { 8 , 3 , 7 , 5 , 3 } ; int n = arr . Length ; getSum ( arr , n ) ; } }
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | C # program for the above approach ; Function to check if array can be split into three equal sum subarrays by removing a pair ; Stores prefix sum array ; Copy array elements ; Traverse the array ; Stores sums of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no such pair exists , print - 1 ; Driver Code ; Given array ; Size of the array
using System ; public class GFG { static void findSplit ( int [ ] arr , int N ) { int [ ] sum = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { sum [ i ] = arr [ i ] ; } for ( int i = 1 ; i < N ; i ++ ) { sum [ i ] += sum [ i - 1 ] ; } for ( int l = 1 ; l <= N - 4 ; l ++ ) { for ( int r = l + 2 ; r <= N - 2 ; r ++ ) { int lsum = 0 , rsum = 0 , msum = 0 ; lsum = sum [ l - 1 ] ; msum = sum [ r - 1 ] - sum [ l ] ; rsum = sum [ N - 1 ] - sum [ r ] ; if ( lsum == rsum && rsum == msum ) { Console . Write ( l + " ▁ " + r + " STRNEWLINE " ) ; return ; } } } Console . Write ( - 1 + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 12 , 7 , 19 , 4 , 3 } ; int N = arr . Length ; findSplit ( arr , N ) ; } }
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | C # program for the above approach ; Function to check if array can be split into three equal sum subarrays by removing a pair ; Two pointers l and r ; Stores prefix sum array ; Traverse the array ; Two pointer approach ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Print split indices if sum is equal ; Move left pointer if lsum < rsum ; Move right pointer if rsum > lsum ; Move both pointers if lsum = rsum but they are not equal to msum ; If no possible pair exists , print - 1 ; Driver code ; Given array ; Size of the array
using System ; class GFG { static void findSplit ( int [ ] arr , int N ) { int l = 1 , r = N - 2 ; int lsum , msum , rsum ; int [ ] sum = new int [ N ] ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { sum [ i ] = sum [ i - 1 ] + arr [ i ] ; } while ( l < r ) { lsum = sum [ l - 1 ] ; msum = sum [ r - 1 ] - sum [ l ] ; rsum = sum [ N - 1 ] - sum [ r ] ; if ( lsum == msum && msum == rsum ) { Console . Write ( l + " ▁ " + r ) ; return ; } if ( lsum < rsum ) l ++ ; else if ( lsum > rsum ) r -- ; else { l ++ ; r -- ; } } Console . Write ( - 1 ) ; } static void Main ( ) { int [ ] arr = { 2 , 5 , 12 , 7 , 19 , 4 , 3 } ; int N = arr . Length ; findSplit ( arr , N ) ; } }
Number of subtrees having odd count of even numbers | C # implementation to find number of subtrees having odd count of even numbers ; A binary tree Node ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ;  ; Returns count of subtrees having odd count of even numbers ; base condition ; count even nodes in left subtree ; Add even nodes in right subtree ; Check if root data is an even number ; if total count of even numbers for the subtree is odd ; Total count of even nodes of the subtree ; A wrapper over countRec ( ) ; Driver Code ; binary tree formation ; 2 ; / \ ; 1 3 ; / \ / \ ; 4 10 8 5 ; / ; 6
using System ; class GFG { public class Node { public int data ; public Node left , right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public class P { public int pcount = 0 ; } static int countRec ( Node root , P p ) { if ( root == null ) return 0 ; int c = countRec ( root . left , p ) ; c += countRec ( root . right , p ) ; if ( root . data % 2 == 0 ) c += 1 ; if ( c % 2 != 0 ) ( p . pcount ) ++ ; return c ; } static int countSubtrees ( Node root ) { P p = new P ( ) ; countRec ( root , p ) ; return p . pcount ; } public static void Main ( String [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 10 ) ; root . right . left = newNode ( 8 ) ; root . right . right = newNode ( 5 ) ; root . left . right . left = newNode ( 6 ) ; Console . WriteLine ( " Count ▁ = ▁ " + countSubtrees ( root ) ) ; } }
Inorder Successor of a node in Binary Tree | C # program to find inorder successor of a node ; A Binary Tree Node ; Temporary node for case 2 ; Utility function to create a new tree node ; function to find left most node in a tree ; function to find right most node in a tree ; recursive function to find the Inorder Scuccessor when the right child of node x is null ; function to find inorder successor of a node ; Case1 : If right child is not null ; Case2 : If right child is null ; case3 : If x is the right most node ; Driver Code ; Case 1 ; case 2 ; case 3
using System ; class GFG { public class Node { public int data ; public Node left , right ; } public static Node temp = new Node ( ) ; public static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static Node leftMostNode ( Node node ) { while ( node != null && node . left != null ) { node = node . left ; } return node ; } public static Node rightMostNode ( Node node ) { while ( node != null && node . right != null ) { node = node . right ; } return node ; } public static Node findInorderRecursive ( Node root , Node x ) { if ( root == null ) { return null ; } if ( root == x || ( temp = findInorderRecursive ( root . left , x ) ) != null || ( temp = findInorderRecursive ( root . right , x ) ) != null ) { if ( temp != null ) { if ( root . left == temp ) { Console . Write ( " Inorder ▁ Successor ▁ of ▁ " + x . data ) ; Console . Write ( " ▁ is ▁ " + root . data + " STRNEWLINE " ) ; return null ; } } return root ; } return null ; } public static void inorderSuccesor ( Node root , Node x ) { if ( x . right != null ) { Node inorderSucc = leftMostNode ( x . right ) ; Console . Write ( " Inorder ▁ Successor ▁ of ▁ " + x . data + " ▁ is ▁ " ) ; Console . Write ( inorderSucc . data + " STRNEWLINE " ) ; } if ( x . right == null ) { int f = 0 ; Node rightMost = rightMostNode ( root ) ; if ( rightMost == x ) { Console . Write ( " No ▁ inorder ▁ successor ! ▁ " + " Right ▁ most ▁ node . STRNEWLINE " ) ; } else { findInorderRecursive ( root , x ) ; } } } public static void Main ( string [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; inorderSuccesor ( root , root . right ) ; inorderSuccesor ( root , root . left . left ) ; inorderSuccesor ( root , root . right . right ) ; } }
Binary Tree | Set 1 ( Introduction ) | A C # program to introduce Binary Tree ; Class containing left and right child of current node and key value ; Binary Tree ; Driver Code
using System ; public class Node { public int key ; public Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } public class BinaryTree { Node root ; BinaryTree ( int key ) { root = new Node ( key ) ; } BinaryTree ( ) { root = null ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; } }
Find distance from root to given node in a binary tree | C # program to find distance of a given node from root . ; A Binary Tree Node ; A utility function to create a new Binary Tree Node ; Returns - 1 if x doesn 't exist in tree. Else  returns distance of x from root ; Base case ; Initialize distance ; Check if x is present at root or in left subtree or right subtree . ; Driver code
using System ; class GfG { class Node { public int data ; public Node left , right ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . left = null ; temp . right = null ; return temp ; } static int findDistance ( Node root , int x ) { if ( root == null ) return - 1 ; int dist = - 1 ; if ( ( root . data == x ) || ( dist = findDistance ( root . left , x ) ) >= 0 || ( dist = findDistance ( root . right , x ) ) >= 0 ) return dist + 1 ; return dist ; } public static void Main ( String [ ] args ) { Node root = newNode ( 5 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 15 ) ; root . left . left = newNode ( 20 ) ; root . left . right = newNode ( 25 ) ; root . left . right . right = newNode ( 45 ) ; root . right . left = newNode ( 30 ) ; root . right . right = newNode ( 35 ) ; Console . WriteLine ( findDistance ( root , 45 ) ) ; } }
Find right sibling of a binary tree with parent pointers | C # program to print right sibling of a node ; A Binary Tree Node ; Constructor ; Method to find right sibling ; GET Parent pointer whose right child is not a parent or itself of this node . There might be case when parent has no right child , but , current node is left child of the parent ( second condition is for that ) . ; Move to the required child , where right sibling can be present ; find right sibling in the given subtree ( from current node ) , when level will be 0 ; Iterate through subtree ; if no child are there , we cannot have right sibling in this path ; This is the case when we reach 9 node in the tree , where we need to again recursively find the right sibling ; Driver Program to test above functions ; passing 10
using System ; public class Right_Sibling { public class Node { public int data ; public Node left , right , parent ; public Node ( int data , Node parent ) { this . data = data ; left = null ; right = null ; this . parent = parent ; } } public static Node findRightSibling ( Node node , int level ) { if ( node == null node . parent == null ) { return null ; } while ( node . parent . right == node || ( node . parent . right == null && node . parent . left == node ) ) { if ( node . parent == null node . parent . parent == null ) { return null ; } node = node . parent ; level -- ; } node = node . parent . right ; while ( level < 0 ) { if ( node . left != null ) { node = node . left ; } else if ( node . right != null ) { node = node . right ; } else { break ; } level ++ ; } if ( level == 0 ) { return node ; } return findRightSibling ( node , level ) ; } public static void Main ( string [ ] args ) { Node root = new Node ( 1 , null ) ; root . left = new Node ( 2 , root ) ; root . right = new Node ( 3 , root ) ; root . left . left = new Node ( 4 , root . left ) ; root . left . right = new Node ( 6 , root . left ) ; root . left . left . left = new Node ( 7 , root . left . left ) ; root . left . left . left . left = new Node ( 10 , root . left . left . left ) ; root . left . right . right = new Node ( 9 , root . left . right ) ; root . right . right = new Node ( 5 , root . right ) ; root . right . right . right = new Node ( 8 , root . right . right ) ; root . right . right . right . right = new Node ( 12 , root . right . right . right ) ; Console . WriteLine ( findRightSibling ( root . left . left . left . left , 0 ) . data ) ; } }