codeparrot/xlcost-text-to-code huggingface dataset:
Count of all prime weight nodes between given nodes in the given Tree | C # program to count prime weight nodes between two nodes in the given tree ; Function to perform Sieve Of Eratosthenes for prime number ; Initialize all entries of prime it as true a value in prime [ i ] will finally be false if i is Not a prime , else true . ; Check if prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to perform dfs ; Stores parent of each node ; Stores level of each node from root ; Function to perform prime number between the path ; The node which is present farthest from the root node is taken as v If u is farther from root node then swap the two ; Find the ancestor of v which is at same level as u ; If Weight is prime increment count ; If u is the ancestor of v then u is the LCA of u and v Now check if weigh [ v ] is prime or not ; When v and u are on the same level but are in different subtree . Now move both u and v up by 1 till they are not same ; If weight of first ancestor is prime ; Driver code ; Precompute all the prime numbers till MAX ; Weights of the node ; Edges of the tree
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 1000 ; static int [ ] weight = new int [ MAX ] ; static int [ ] level = new int [ MAX ] ; static int [ ] par = new int [ MAX ] ; static bool [ ] prime = new bool [ MAX + 1 ] ; static List < int > [ ] graph = new List < int > [ MAX ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= MAX ; i += p ) prime [ i ] = false ; } } } static void dfs ( int node , int parent , int h ) { par [ node ] = parent ; level [ node ] = h ; foreach ( int child in graph [ node ] ) { if ( child == parent ) continue ; dfs ( child , node , h + 1 ) ; } } static int findPrimeOnPath ( int u , int v ) { int count = 0 ; if ( level [ u ] > level [ v ] ) { int temp = v ; v = u ; u = temp ; } int d = level [ v ] - level [ u ] ; while ( d -- > 0 ) { if ( prime [ weight [ v ] ] ) count ++ ; v = par [ v ] ; } if ( v == u ) { if ( prime [ weight [ v ] ] ) count ++ ; return count ; } while ( v != u ) { if ( prime [ weight [ v ] ] ) count ++ ; if ( prime [ weight [ u ] ] ) count ++ ; u = par [ u ] ; v = par [ v ] ; } if ( prime [ weight [ v ] ] ) count ++ ; return count ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < graph . Length ; i ++ ) graph [ i ] = new List < int > ( ) ; SieveOfEratosthenes ( ) ; weight [ 1 ] = 5 ; weight [ 2 ] = 10 ; weight [ 3 ] = 11 ; weight [ 4 ] = 8 ; weight [ 5 ] = 6 ; graph [ 1 ] . Add ( 2 ) ; graph [ 2 ] . Add ( 3 ) ; graph [ 2 ] . Add ( 4 ) ; graph [ 1 ] . Add ( 5 ) ; dfs ( 1 , - 1 , 0 ) ; int u = 3 , v = 5 ; Console . Write ( findPrimeOnPath ( u , v ) ) ; } }
Lowest Common Ancestor for a Set of Nodes in a Rooted Tree | C # Program to find the LCA in a rooted tree for a given set of nodes ; Set time 1 initially ; Case for root node ; In - time for node ; Out - time for the node ; level [ i ] - . Level of node i ; t_in [ i ] - . In - time of node i ; t_out [ i ] - . Out - time of node i ; Fill the level , in - time and out - time of all nodes ; To find minimum in - time among all nodes in LCA set ; To find maximum in - time among all nodes in LCA set ; Node with same minimum and maximum out time is LCA for the set ; Take the minimum level as level of LCA ; If i - th node is at a higher level than that of the minimum among the nodes of the given set ; Compare in - time , out - time and level of i - th node to the respective extremes among all nodes of the given set ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static int T = 1 ; static void dfs ( int node , int parent , List < int > [ ] g , int [ ] level , int [ ] t_in , int [ ] t_out ) { if ( parent == - 1 ) { level [ node ] = 1 ; } else { level [ node ] = level [ parent ] + 1 ; } t_in [ node ] = T ; foreach ( int i in g [ node ] ) { if ( i != parent ) { T ++ ; dfs ( i , node , g , level , t_in , t_out ) ; } } T ++ ; t_out [ node ] = T ; } static int findLCA ( int n , List < int > [ ] g , List < int > v ) { int [ ] level = new int [ n + 1 ] ; int [ ] t_in = new int [ n + 1 ] ; int [ ] t_out = new int [ n + 1 ] ; dfs ( 1 , - 1 , g , level , t_in , t_out ) ; int mint = int . MaxValue , maxt = int . MinValue ; int minv = - 1 , maxv = - 1 ; for ( int i = 0 ; i < v . Count ; i ++ ) { if ( t_in [ v [ i ] ] < mint ) { mint = t_in [ v [ i ] ] ; minv = v [ i ] ; } if ( t_out [ v [ i ] ] > maxt ) { maxt = t_out [ v [ i ] ] ; maxv = v [ i ] ; } } if ( minv == maxv ) { return minv ; } int lev = Math . Min ( level [ minv ] , level [ maxv ] ) ; int node = 0 , l = int . MinValue ; for ( int i = 1 ; i <= n ; i ++ ) { if ( level [ i ] > lev ) continue ; if ( t_in [ i ] <= mint && t_out [ i ] >= maxt && level [ i ] > l ) { node = i ; l = level [ i ] ; } } return node ; } public static void Main ( String [ ] args ) { int n = 10 ; List < int > [ ] g = new List < int > [ n + 1 ] ; for ( int i = 0 ; i < g . Length ; i ++ ) g [ i ] = new List < int > ( ) ; g [ 1 ] . Add ( 2 ) ; g [ 2 ] . Add ( 1 ) ; g [ 1 ] . Add ( 3 ) ; g [ 3 ] . Add ( 1 ) ; g [ 1 ] . Add ( 4 ) ; g [ 4 ] . Add ( 1 ) ; g [ 2 ] . Add ( 5 ) ; g [ 5 ] . Add ( 2 ) ; g [ 2 ] . Add ( 6 ) ; g [ 6 ] . Add ( 2 ) ; g [ 3 ] . Add ( 7 ) ; g [ 7 ] . Add ( 3 ) ; g [ 4 ] . Add ( 10 ) ; g [ 10 ] . Add ( 4 ) ; g [ 8 ] . Add ( 7 ) ; g [ 7 ] . Add ( 8 ) ; g [ 9 ] . Add ( 7 ) ; g [ 7 ] . Add ( 9 ) ; List < int > v = new List < int > ( ) ; v . Add ( 7 ) ; v . Add ( 3 ) ; v . Add ( 8 ) ; Console . Write ( findLCA ( n , g , v ) + " STRNEWLINE " ) ; } }
Sum of all odd nodes in the path connecting two given nodes | C # program to find sum of all odd nodes in the path connecting two given nodes ; Binary Tree node ; Utitlity function to create a new Binary Tree node ; Function to check if there is a path from root to the given node . It also populates ' arr ' with the given path ; if root is null there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr ' ; if it is the required node return true ; else check whether the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node Thus , Remove current node ' s ▁ value ▁ from ▁ ▁ ' arr 'and then return false ; Function to get the sum of odd nodes in the path between any two nodes in a binary tree ; List to store the path of first node n1 from root ; List to store the path of second node n2 from root ; Get intersection point ; Keep moving forward until no intersection is found ; calculate sum of ODD nodes from the path ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left ; public Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return node ; } static Boolean getPath ( Node root , List < int > arr , int x ) { if ( root == null ) return false ; arr . Add ( root . data ) ; if ( root . data == x ) return true ; if ( getPath ( root . left , arr , x ) || getPath ( root . right , arr , x ) ) return true ; arr . RemoveAt ( arr . Count - 1 ) ; return false ; } static int sumOddNodes ( Node root , int n1 , int n2 ) { List < int > path1 = new List < int > ( ) ; List < int > path2 = new List < int > ( ) ; getPath ( root , path1 , n1 ) ; getPath ( root , path2 , n2 ) ; int intersection = - 1 ; int i = 0 , j = 0 ; while ( i < path1 . Count < path2 . Count ) { if ( i == j && path1 [ i ] == path2 [ j ] ) { i ++ ; j ++ ; } else { intersection = j - 1 ; break ; } } int sum = 0 ; for ( i = path1 . Count - 1 ; i > intersection ; i -- ) if ( path1 [ i ] % 2 != 0 ) sum += path1 [ i ] ; for ( i = intersection ; i < path2 . Count ; i ++ ) if ( path2 [ i ] % 2 != 0 ) sum += path2 [ i ] ; return sum ; } 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 ) ; int node1 = 5 ; int node2 = 6 ; Console . Write ( sumOddNodes ( root , node1 , node2 ) ) ; } }
Lowest Common Ancestor in Parent Array Representation | C # program to find LCA in a tree represented as parent array . ; Maximum value in a node ; Function to find the Lowest common ancestor ; Create a visited vector and mark all nodes as not visited . ; Moving from n1 node till root and mark every accessed node as visited ; Move to the parent of node n1 ; For second node finding the first node common ; Insert function for Binary tree ; Driver Code ; Maximum capacity of binary tree ; Root marked
using System ; class GFG { static int MAX = 1000 ; static int findLCA ( int n1 , int n2 , int [ ] parent ) { Boolean [ ] visited = new Boolean [ MAX ] ; visited [ n1 ] = true ; while ( parent [ n1 ] != - 1 ) { visited [ n1 ] = true ; n1 = parent [ n1 ] ; } visited [ n1 ] = true ; while ( ! visited [ n2 ] ) n2 = parent [ n2 ] ; return n2 ; } static void insertAdj ( int [ ] parent , int i , int j ) { parent [ i ] = j ; } public static void Main ( String [ ] args ) { int [ ] parent = new int [ MAX ] ; parent [ 20 ] = - 1 ; insertAdj ( parent , 8 , 20 ) ; insertAdj ( parent , 22 , 20 ) ; insertAdj ( parent , 4 , 8 ) ; insertAdj ( parent , 12 , 8 ) ; insertAdj ( parent , 10 , 12 ) ; insertAdj ( parent , 14 , 12 ) ; Console . WriteLine ( findLCA ( 10 , 14 , parent ) ) ; } }
LCA for general or n | C # Program to find LCA of n1 and n2 using one DFS on the Tree ; Maximum number of nodes is 100000 and nodes are numbered from 1 to 100000 ; storing root to node path ; storing the path from root to node ; pushing current node into the path ; node found ; terminating the path ; This Function compares the path from root to ' a ' & root to ' b ' and returns LCA of a and b . Time Complexity : O ( n ) ; trivial case ; setting root to be first element in path ; calculating path from root to a ; calculating path from root to b ; runs till path 1 & path 2 mathches ; returns the last matching node in the paths ; Driver code ; Number of nodes
using System ; using System . Collections . Generic ; class GFG { static readonly int MAXN = 100001 ; static List < int > [ ] tree = new List < int > [ MAXN ] ; static int [ , ] path = new int [ 3 , MAXN ] ; static bool flag ; static void dfs ( int cur , int prev , int pathNumber , int ptr , int node ) { for ( int i = 0 ; i < tree [ cur ] . Count ; i ++ ) { if ( tree [ cur ] [ i ] != prev && ! flag ) { path [ pathNumber , ptr ] = tree [ cur ] [ i ] ; if ( tree [ cur ] [ i ] == node ) { flag = true ; path [ pathNumber , ptr + 1 ] = - 1 ; return ; } dfs ( tree [ cur ] [ i ] , cur , pathNumber , ptr + 1 , node ) ; } } } static int LCA ( int a , int b ) { if ( a == b ) return a ; path [ 1 , 0 ] = path [ 2 , 0 ] = 1 ; flag = false ; dfs ( 1 , 0 , 1 , 1 , a ) ; flag = false ; dfs ( 1 , 0 , 2 , 1 , b ) ; int i = 0 ; while ( i < MAXN && path [ 1 , i ] == path [ 2 , i ] ) i ++ ; return path [ 1 , i - 1 ] ; } static void addEdge ( int a , int b ) { tree [ a ] . Add ( b ) ; tree [ b ] . Add ( a ) ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < MAXN ; i ++ ) tree [ i ] = new List < int > ( ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 3 , 8 ) ; Console . Write ( " LCA ( 4 , ▁ 7 ) ▁ = ▁ " + LCA ( 4 , 7 ) + " STRNEWLINE " ) ; Console . Write ( " LCA ( 4 , ▁ 6 ) ▁ = ▁ " + LCA ( 4 , 6 ) + " STRNEWLINE " ) ; } }
LCA for general or n | Sparse Matrix DP approach to find LCA of two nodes ; pre - compute the depth for each node and their first parent ( 2 ^ 0 th parent ) time complexity : O ( n ) ; Dynamic Programming Sparse Matrix Approach populating 2 ^ i parent for each node Time complexity : O ( nlogn ) ; Returning the LCA of u and v Time complexity : O ( log n ) ; Step 1 of the pseudocode ; now depth [ u ] = = depth [ v ] ; Step 2 of the pseudocode ; Driver function ; running dfs and precalculating depth of each node . ; Precomputing the 2 ^ i th ancestor for evey node ; calling the LCA function
using System ; using System . Collections . Generic ; class GFG { static readonly int MAXN = 100000 ; static readonly int level = 18 ; static List < int > [ ] tree = new List < int > [ MAXN ] ; static int [ ] depth = new int [ MAXN ] ; static int [ , ] parent = new int [ MAXN , level ] ; static void dfs ( int cur , int prev ) { depth [ cur ] = depth [ prev ] + 1 ; parent [ cur , 0 ] = prev ; for ( int i = 0 ; i < tree [ cur ] . Count ; i ++ ) { if ( tree [ cur ] [ i ] != prev ) dfs ( tree [ cur ] [ i ] , cur ) ; } } static void precomputeSparseMatrix ( int n ) { for ( int i = 1 ; i < level ; i ++ ) { for ( int node = 1 ; node <= n ; node ++ ) { if ( parent [ node , i - 1 ] != - 1 ) parent [ node , i ] = parent [ parent [ node , i - 1 ] , i - 1 ] ; } } } static int lca ( int u , int v ) { if ( depth [ v ] < depth [ u ] ) { u = u + v ; v = u - v ; u = u - v ; } int diff = depth [ v ] - depth [ u ] ; for ( int i = 0 ; i < level ; i ++ ) if ( ( ( diff >> i ) & 1 ) == 1 ) v = parent [ v , i ] ; if ( u == v ) return u ; for ( int i = level - 1 ; i >= 0 ; i -- ) if ( parent [ u , i ] != parent [ v , i ] ) { u = parent [ u , i ] ; v = parent [ v , i ] ; } return parent [ u , 0 ] ; } static void addEdge ( int u , int v ) { tree [ u ] . Add ( v ) ; tree [ v ] . Add ( u ) ; } static void memset ( int value ) { for ( int i = 0 ; i < MAXN ; i ++ ) { for ( int j = 0 ; j < level ; j ++ ) { parent [ i , j ] = - 1 ; } } } public static void Main ( String [ ] args ) { memset ( - 1 ) ; for ( int i = 0 ; i < MAXN ; i ++ ) tree [ i ] = new List < int > ( ) ; int n = 8 ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 3 , 8 ) ; depth [ 0 ] = 0 ; dfs ( 1 , 0 ) ; precomputeSparseMatrix ( n ) ; Console . Write ( " LCA ( 4 , ▁ 7 ) ▁ = ▁ " + lca ( 4 , 7 ) + " STRNEWLINE " ) ; Console . Write ( " LCA ( 4 , ▁ 6 ) ▁ = ▁ " + lca ( 4 , 6 ) + " STRNEWLINE " ) ; } }
Longest Common Extension / LCE | Set 1 ( Introduction and Naive Method ) | A C # Program to find the length of longest common extension using Naive Method ; Structure to represent a query of form ( L , R ) ; A utility function to find longest common extension from index - L and index - R ; A function to answer queries of longest common extension ; Driver code ; LCA Queries to answer
using System ; public class GFG { public class Query { public int L , R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static int LCE ( String str , int n , int L , int R ) { int length = 0 ; while ( R + length < n && str [ L + length ] == str [ R + length ] ) length ++ ; return ( length ) ; } static void LCEQueries ( String str , int n , Query [ ] q , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L ; int R = q [ i ] . R ; Console . WriteLine ( " LCE ▁ ( " + L + " , ▁ " + R + " ) ▁ = ▁ " + LCE ( str , n , L , R ) ) ; } return ; } public static void Main ( String [ ] args ) { String str = " abbababba " ; int n = str . Length ; Query [ ] q = new Query [ 3 ] ; q [ 0 ] = new Query ( 1 , 2 ) ; q [ 1 ] = new Query ( 1 , 6 ) ; q [ 2 ] = new Query ( 0 , 5 ) ; int m = q . Length ; LCEQueries ( str , n , q , m ) ; } }
Time Complexity and Space Complexity | C # program for above approach ; Function to find a pair in the given array whose sum is equal to z ; Iterate through all the pairs ; Check if the sum of the pair ( a [ i ] , a [ j ] ) is equal to z ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static bool findPair ( int [ ] a , int n , int z ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( i != j && a [ i ] + a [ j ] == z ) return true ; return false ; } static void Main ( ) { int [ ] a = { 1 , - 2 , 1 , 0 , 5 } ; int z = 0 ; int n = a . Length ; if ( findPair ( a , n , z ) ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }
Analysis of Algorithms | Big | C # program for above approach ; Function to print all possible pairs ; Driver Code ; Given array ; Store the size of the array ; Function Call
using System ; class GFG { static void print ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i != j ) Console . WriteLine ( a [ i ] + " ▁ " + a [ j ] ) ; } } } static void Main ( ) { int [ ] a = { 1 , 2 , 3 } ; int n = a . Length ; print ( a , n ) ; } }
Maximum sum obtained by dividing Array into several subarrays as per given conditions | C # program for the above approach ; Function to find the required answer ; Stores maximum sum ; Adding the difference of elements at ends of increasing subarray to the answer ; Driver Code ; Input ; Functio calling
using System ; using System . Collections . Generic ; class GFG { static int maximumSum ( int [ ] arr , int N ) { int Sum = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) Sum += ( arr [ i ] - arr [ i - 1 ] ) ; } return Sum ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 3 } ; int N = arr . Length ; Console . Write ( maximumSum ( arr , N ) ) ; } }
Maximum count of X that can be inserted with no 3 adjacent characters are X | C # program for the above approach ; Utility function to find maximum count of X that are to be inserted into str such that no three consecutive characters are equal to X ; Stores count of consecutive X ; Stores count of characters which is not equal to X ; Iterate over characters of string , str ; If current character is X ; Update countX ; If countX is less than 3 ; Update countNotX ; Update countX ; If countX is greater than or equal to 3 ; Function to find maximum count of X that are to be inserted into str such that no three consecutive characters are equal to X ; Print the answer ; Driver Code ; Given string ; Function Call
using System ; class GFG { static int maxNumberOfXAddedUtil ( string str , char X ) { int countX = 0 ; int countNotX = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == X ) { countX ++ ; } else if ( countX < 3 ) { countNotX ++ ; countX = 0 ; } } if ( countX >= 3 ) return - 1 ; else return 2 * ( countNotX + 1 ) - ( str . Length - countNotX ) ; } static void maxNumberOfXAdded ( string str , char X ) { int ans = maxNumberOfXAddedUtil ( str , X ) ; Console . Write ( ans ) ; } public static void Main ( ) { string str = " xxyxy " ; char X = ' x ' ; maxNumberOfXAdded ( str , X ) ; } }
Examples of Big | C # program to illustrate time complexity for single for - loop ; Driver Code ; This loop runs for N time ; This loop runs for M time
using System ; class GFG { public static void Main ( string [ ] args ) { int a = 0 , b = 0 ; int N = 4 , M = 4 ; for ( int i = 0 ; i < N ; i ++ ) { a = a + 10 ; } for ( int i = 0 ; i < M ; i ++ ) { b = b + 40 ; } Console . Write ( a + " ▁ " + b ) ; } }
The Slowest Sorting Algorithms | C # program for the stooge sort ; Function to implement stooge sort ; Base Case ; If first element is smaller than last element , swap them ; If there are more than 2 elements in the array ; Recursively sort the first 2 / 3 elements ; Recursively sort the last 2 / 3 elements ; Recursively sort the first 2 / 3 elements again ; Driver Code ; Function Call ; Display the sorted array
using System ; class GFG { static void stoogesort ( int [ ] arr , int l , int h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) { int temp = arr [ l ] ; arr [ l ] = arr [ h ] ; arr [ h ] = temp ; } if ( h - l + 1 > 2 ) { int t = ( h - l + 1 ) / 3 ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t , h ) ; stoogesort ( arr , l , h - t ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 5 , 3 , 1 } ; int N = arr . Length ; stoogesort ( arr , 0 , N - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }
Time | C # program to find Nth Fibonacci number using recursion ; Function to find Nth Fibonacci term ; Base Case ; Recursively computing the term using recurrence relation ; Driver Code ; Function Call
using System ; class GFG { static int Fibonacci ( int N ) { if ( N < 2 ) return N ; return Fibonacci ( N - 1 ) + Fibonacci ( N - 2 ) ; } public static void Main ( string [ ] args ) { int N = 5 ; Console . Write ( Fibonacci ( N ) ) ; } }
Determine winner of the Game by arranging balls in a row | C # program for the above approach ; Function to find the winner of the Game by arranging the balls in a row ; Check if small balls are greater or equal to the large ones ; X can place balls therefore scores n - 1 ; Condition if large balls are greater than small ; X can have m - 1 as a score since greater number of balls can only be adjacent ; Compare the score ; Driver Code ; Given number of small balls ( N ) and number of large balls ( M ) ; Function call
using System ; class GFG { static void findWinner ( int n , int m ) { int X = 0 ; int Y = 0 ; if ( n >= m ) { X = n - 1 ; Y = m ; } else { X = m - 1 ; Y = n ; } if ( X > Y ) Console . Write ( " X " ) ; else if ( Y > X ) Console . Write ( " Y " ) ; else Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int n = 3 , m = 1 ; findWinner ( n , m ) ; } }
Check if Pascal 's Triangle is possible with a complete layer by using numbers upto N | C # program for the above approach ; Function to check if Pascaltriangle can be made by N integers ; Find X ; If x is integer ; Driver Code ; Given number N ; Function call
using System ; class GFG { static void checkPascaltriangle ( int N ) { double x = ( Math . Sqrt ( 8 * N + 1 ) - 1 ) / 2 ; if ( Math . Ceiling ( x ) - x == 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( String [ ] args ) { int N = 10 ; checkPascaltriangle ( N ) ; } }
Count subarrays having sum of elements at even and odd positions equal | C # program for the above approach ; Function to count subarrays in which sum of elements at even and odd positions are equal ; Initialize variables ; Iterate over the array ; Check if position is even then add to sum then add it to sum ; else subtract it to sum ; Increment the count if the sum equals 0 ; Print the count of subarrays ; Driver Code ; Given array [ ] arr ; Size of the array ; Function call
using System ; class GFG { static void countSubarrays ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( ( j - i ) % 2 == 0 ) sum += arr [ j ] ; else sum -= arr [ j ] ; if ( sum == 0 ) count ++ ; } } Console . WriteLine ( count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 6 , 4 , 2 } ; int n = arr . Length ; countSubarrays ( arr , n ) ; } }
Find position of non | C # implementation to find count of placing non - attacking rooks on the N x N chessboard ; Function to find the count of placing non - attacking rooks on the N x N chessboard ; Count of the Non - attacking rooks ; Printing lexographically smallest configuration ; Driver Code ; Function call
using System ; class P2Placerooks { static void findCountRooks ( int [ ] row , int [ ] col , int n , int k ) { int res = n - k ; Console . WriteLine ( res + " ▁ " ) ; int ri = 0 , ci = 0 ; while ( res -- > 0 ) { while ( ri < k && row [ ri ] == 1 ) { ri ++ ; } while ( ci < k && col [ ci ] == 1 ) { ci ++ ; } Console . WriteLine ( ( ri + 1 ) + " ▁ " + ( ci + 1 ) + " ▁ " ) ; ri ++ ; ci ++ ; } } public static void Main ( String [ ] args ) { int n = 4 ; int k = 2 ; int [ ] row = { 1 , 2 } ; int [ ] col = { 4 , 2 } ; findCountRooks ( row , col , n , k ) ; } }