codeparrot/xlcost-text-to-code huggingface dataset:
Construct a graph from given degrees of all vertices | C # program to generate a graph for a given fixed degrees ; A function to print the adjacency matrix . ; n is number of vertices ; For each pair of vertex decrement the degree of both vertex . ; Print the result in specified format ; Driver Code
using System ; class GFG { static void printMat ( int [ ] degseq , int n ) { int [ , ] mat = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( degseq [ i ] > 0 && degseq [ j ] > 0 ) { degseq [ i ] -- ; degseq [ j ] -- ; mat [ i , j ] = 1 ; mat [ j , i ] = 1 ; } } } Console . Write ( " STRNEWLINE " + setw ( 3 ) + " ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( setw ( 3 ) + " ( " + i + " ) " ) ; Console . Write ( " STRNEWLINE STRNEWLINE " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( setw ( 4 ) + " ( " + i + " ) " ) ; for ( int j = 0 ; j < n ; j ++ ) Console . Write ( setw ( 5 ) + mat [ i , j ] ) ; Console . Write ( " STRNEWLINE " ) ; } } static String setw ( int n ) { String space = " " ; while ( n -- > 0 ) space += " ▁ " ; return space ; } public static void Main ( String [ ] args ) { int [ ] degseq = { 2 , 2 , 1 , 1 , 1 } ; int n = degseq . Length ; printMat ( degseq , n ) ; } }
Decimal equivalent of concatenation of absolute difference of floor and rounded | C # program for the above approach ; Function to find the decimal equivalent of the new binary array constructed from absolute decimal of floor and the round - off values ; Traverse the givenarray from the end ; Stores the absolute difference between floor and round - off each array element ; If bit / difference is 1 , then calculate the bit by proper power of 2 and add it to result ; Increment the value of power ; Print the result ; Driver Code
using System ; class GFG { static void findDecimal ( double [ ] arr , int N ) { int bit , power = 0 , result = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { bit = Math . Abs ( ( int ) Math . Floor ( arr [ i ] ) - ( int ) Math . Round ( arr [ i ] ) ) ; if ( bit != 0 ) result += ( int ) Math . Pow ( 2 , power ) ; power ++ ; } Console . WriteLine ( result ) ; } public static void Main ( ) { double [ ] arr = { 1.2 , 2.6 , 4.2 , 6.9 , 3.1 , 21.6 , 91.2 } ; int N = arr . Length ; findDecimal ( arr , N ) ; } }
Calculate money placed in boxes after N days based on given conditions | C # program for the above approach ; Function to find the total money placed in boxes after N days ; Stores the total money ; Iterate for N days ; Adding the Week number ; Adding previous amount + 1 ; Return the total amount ; Driver code ; Input ; Function call to find total money placed
using System ; class GFG { public static int totalMoney ( int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += i / 7 ; ans += ( i % 7 + 1 ) ; } return ans ; } static public void Main ( ) { int N = 15 ; Console . WriteLine ( totalMoney ( N ) ) ; } }
Calculate money placed in boxes after N days based on given conditions | C # program for the above approach ; Function to find total money placed in the box ; Number of complete weeks ; Remaining days in the last week ; Driver Code ; Input ; Function call to find the total money placed
using System ; class GFG { static void totalMoney ( int N ) { int CompWeeks = N / 7 ; int RemDays = N % 7 ; int X = 28 * CompWeeks + 7 * ( CompWeeks * ( CompWeeks - 1 ) / 2 ) ; int Y = RemDays * ( RemDays + 1 ) / 2 + CompWeeks * RemDays ; int cost = X + Y ; Console . WriteLine ( cost ) ; } public static void Main ( ) { int N = 15 ; totalMoney ( N ) ; } }
Convert a number to another by dividing by its factor or removing first occurrence of a digit from an array | C # program for the above approach ; Function to check if a digit x is present in the number N or not ; Convert N to string ; Traverse the string num ; Return first occurrence of the digit x ; Function to remove the character at a given index from the number ; Convert N to string ; Store the resultant string ; Traverse the string num ; If the number becomes empty after deletion , then return - 1 ; Return the number ; Function to check if A can be reduced to B by performing the operations any number of times ; Create a queue ; Push A into the queue ; Hashmap to check if the element is present in the Queue or not ; Set A as visited ; Iterate while the queue is not empty ; Store the front value of the queue and pop it from it ; If top is equal to B , then return true ; Traverse the array , D [ ] ; Divide top by D [ i ] if it is possible and push the result in q ; If D [ i ] is present at the top ; Remove the first occurrence of D [ i ] from the top and store the new number ; Push newElement into the queue q ; Return false if A can not be reduced to B ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int isPresent ( int n , int x ) { string num = n . ToString ( ) ; for ( int i = 0 ; i < num . Length ; i ++ ) { if ( ( ( int ) num [ i ] - 97 ) == x ) return i ; } return - 1 ; } static int removeDigit ( int n , int index ) { string num = n . ToString ( ) ; string ans = " " ; for ( int i = 0 ; i < num . Length ; i ++ ) { if ( i != index ) ans += num [ i ] ; } if ( ans == " " || ( ans . Length == 1 && ans [ 0 ] == '0' ) ) return - 1 ; int x = Int32 . Parse ( ans ) ; ; return x ; } static bool reduceNtoX ( int a , int b , int [ ] d , int n ) { Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( a ) ; Dictionary < int , bool > visited = new Dictionary < int , bool > ( ) ; visited [ a ] = true ; while ( q . Count > 0 ) { int top = q . Peek ( ) ; q . Dequeue ( ) ; if ( top < 0 ) continue ; if ( top == b ) return true ; for ( int i = 0 ; i < n ; i ++ ) { if ( d [ i ] != 0 && top % d [ i ] == 0 && visited . ContainsKey ( top / d [ i ] ) && visited [ top / d [ i ] ] == false ) { q . Enqueue ( top / d [ i ] ) ; visited [ top / d [ i ] ] = true ; } int index = isPresent ( top , d [ i ] ) ; if ( index != - 1 ) { int newElement = removeDigit ( top , index ) ; if ( newElement != - 1 && ( visited . ContainsKey ( newElement ) && visited [ newElement ] == false ) ) { q . Enqueue ( newElement ) ; visited [ newElement ] = true ; } } } } return true ; } public static void Main ( ) { int A = 5643 , B = 81 ; int [ ] D = { 3 , 8 , 1 } ; int N = D . Length ; if ( reduceNtoX ( A , B , D , N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Prime triplets consisting of values up to N having difference between two elements equal to the third | C # program for the above approach ; Stores 1 and 0 at indices which are prime and non - prime respectively ; Function to find all prime numbers from the range [ 0 , N ] ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all tultiples of p as false ; Function to find all prime triplets satisfying the given conditions ; Generate all primes up to N ; Stores the triplets ; Iterate over the range [ 3 , N ] ; Check for the condition ; Store the triplets ; Print all the stored triplets ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool [ ] prime = new bool [ 100000 ] ; static void initialize ( ) { for ( int i = 0 ; i < 100000 ; i ++ ) prime [ i ] = true ; } static void SieveOfEratosthenes ( int n ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } static void findTriplets ( int N ) { SieveOfEratosthenes ( N ) ; List < List < int > > V = new List < List < int > > ( ) ; for ( int i = 3 ; i <= N ; i ++ ) { if ( 2 + i <= N && prime [ i ] == true && prime [ 2 + i ] ) { List < int > a1 = new List < int > ( ) ; a1 . Add ( 2 ) ; a1 . Add ( i ) ; a1 . Add ( i + 2 ) ; V . Add ( a1 ) ; } } for ( int i = 0 ; i < V . Count ; i ++ ) { Console . WriteLine ( V [ i ] [ 0 ] + " ▁ " + V [ i ] [ 1 ] + " ▁ " + V [ i ] [ 2 ] ) ; } } public static void Main ( ) { initialize ( ) ; int N = 8 ; findTriplets ( N ) ; } }
Smallest number which is not coprime with any element of an array | C # program for the above approach ; Function check if a number is prime or not ; Corner cases ; Check if n is divisible by 2 or 3 ; Check for every 6 th number . The above checking allows to skip middle 5 numbers ; Function to store primes in an array ; Function to calculate GCD of two numbers ; Function to find the smallest number which is not coprime with any element of the array arr [ ] ; Function call to fill the prime numbers ; Stores the answer ; Generate all non - empty subsets of the primes [ ] array ; Stores product of the primes ; Checks if temp is coprime with the array or not ; Check if the product temp is not coprime with the whole array ; If the product is not co - prime with the array ; Prlong the answer ; Driver Code ; Given array ; Stores the size of the array
using System ; using System . Collections . Generic ; class GFG { static long MAX = 50 ; static bool isPrime ( long n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( long i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void findPrime ( List < long > primes ) { for ( long i = 2 ; i <= MAX ; i ++ ) { if ( isPrime ( i ) ) primes . Add ( i ) ; } } static long gcd ( long a , long b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static void findMinimumNumber ( long [ ] arr , long N ) { List < long > primes = new List < long > ( ) ; findPrime ( primes ) ; long ans = 2147483647 ; int n = primes . Count ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { long temp = 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) { temp *= primes [ j ] ; } } bool check = true ; for ( long k = 0 ; k < N ; k ++ ) { if ( gcd ( temp , arr [ k ] ) == 1 ) { check = false ; break ; } } if ( check == true ) ans = Math . Min ( ans , temp ) ; } Console . Write ( ans ) ; } public static void Main ( ) { long [ ] arr = { 3 , 4 , 6 , 7 , 8 , 9 , 10 } ; long N = arr . Length ; findMinimumNumber ( arr , N ) ; } }
Count numbers from a given range that can be expressed as sum of digits raised to the power of count of digits | C # program for the above approach ; Function to check if a number N can be expressed as sum of its digits raised to the power of the count of digits ; Stores the number of digits ; Stores the resultant number ; Return true if both the numbers are same ; Function to precompute and store for all numbers whether they can be expressed ; Mark all the index which are plus perfect number ; If true , then update the value at this index ; Compute prefix sum of the array ; Function to count array elements that can be expressed as the sum of digits raised to the power of count of digits ; Precompute the results ; Traverse the queries ; Print the resultant count ; Driver Code ; Function call
using System ; class GFG { static int R = 100005 ; static int [ ] arr = new int [ R ] ; public static bool canExpress ( int N ) { int temp = N ; int n = 0 ; while ( N != 0 ) { N /= 10 ; n ++ ; } N = temp ; int sum = 0 ; while ( N != 0 ) { sum += ( ( int ) Math . Pow ( N % 10 , n ) ) ; N /= 10 ; } if ( sum == temp ) return true ; return false ; } public static void precompute ( ) { for ( int i = 1 ; i < R ; i ++ ) { if ( canExpress ( i ) ) { arr [ i ] = 1 ; } } for ( int i = 1 ; i < R ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; } } public static void countNumbers ( int [ , ] queries , int N ) { precompute ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int L1 = queries [ i , 0 ] ; int R1 = queries [ i , 1 ] ; Console . Write ( ( arr [ R1 ] - arr [ L1 - 1 ] ) + " ▁ " ) ; } } static public void Main ( ) { int [ , ] queries = { { 1 , 400 } , { 1 , 9 } } ; int N = queries . GetLength ( 0 ) ; countNumbers ( queries , N ) ; } }
Queries to calculate sum of the path from root to a given node in given Binary Tree | C # program for the above approach ; Function to find the sum of the path from root to the current node ; Sum of nodes in the path ; Iterate until root is reached ; Update the node value ; Print the resultant sum ; Function to print the path sum for each query ; Traverse the queries ; Driver Code ; arraylist to store integers
using System ; using System . Collections . Generic ; class GFG { public static void sumOfNodeInAPath ( int node_value ) { int sum_of_node = 0 ; while ( node_value > 0 ) { sum_of_node += node_value ; node_value /= 2 ; } Console . Write ( sum_of_node ) ; } public static void findSum ( List < int > Q ) { for ( int i = 0 ; i < Q . Count ; i ++ ) { int node_value = Q [ i ] ; sumOfNodeInAPath ( node_value ) ; Console . Write ( " ▁ " ) ; } } static public void Main ( ) { List < int > arr = new List < int > ( ) ; arr . Add ( 1 ) ; arr . Add ( 5 ) ; arr . Add ( 20 ) ; arr . Add ( 100 ) ; findSum ( arr ) ; } }
Minimum removals required to make frequency of all remaining array elements equal | C # program for the above approach ; Function to count the minimum removals required to make frequency of all array elements equal ; Stores frequency of all array elements ; Traverse the array ; Stores all the frequencies ; Traverse the map ; Sort the frequencies ; Count of frequencies ; Stores the final count ; Traverse the vector ; Count the number of removals for each frequency and update the minimum removals required ; Print the final count ; Driver Code ; Given array ; Size of the array ; Function call to print the minimum number of removals required
using System ; using System . Collections . Generic ; class GFG { static void minDeletions ( int [ ] arr , int N ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) freq [ arr [ i ] ] ++ ; else freq [ arr [ i ] ] = 1 ; } List < int > v = new List < int > ( ) ; foreach ( var z in freq ) { v . Add ( z . Value ) ; } int sz = v . Count ; int [ ] temp = new int [ sz ] ; for ( int i = 0 ; i < v . Count ; i ++ ) temp [ i ] = v [ i ] ; Array . Sort ( temp ) ; for ( int i = 0 ; i < v . Count ; i ++ ) v [ i ] = temp [ i ] ; int size = v . Count ; int ans = N - ( v [ 0 ] * size ) ; for ( int i = 1 ; i < v . Count ; i ++ ) { if ( v [ i ] != v [ i - 1 ] ) { int safe = v [ i ] * ( size - i ) ; ans = Math . Min ( ans , N - safe ) ; } } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 , 2 , 5 , 3 } ; int N = arr . Length ; minDeletions ( arr , N ) ; } }
Determine whether a universal sink exists in a directed graph | C # program to find whether a universal sink exists in a directed graph ; constructor to initialize number of vertices and size of adjacency matrix ; make adjacency_matrix [ i , j ] = 1 if there is an edge from i to j ; if any element in the row i is 1 , it means that there is an edge emanating from the vertex , which means it cannot be a sink ; if any element other than i in the column i is 0 , it means that there is no edge from that vertex to the vertex we are testing and hence it cannot be a sink ; if none of the checks fails , return true ; we will eliminate n - 1 non sink vertices so that we have to check for only one vertex instead of all n vertices ; If the index is 1 , increment the row we are checking by 1 else increment the column ; If i exceeds the number of vertices , it means that there is no valid vertex in the given vertices that can be a sink ; Driver Code ; input set 1 g . insert ( 1 , 6 ) ; g . insert ( 2 , 6 ) ; g . insert ( 3 , 6 ) ; g . insert ( 4 , 6 ) ; g . insert ( 5 , 6 ) ; input set 2 ; returns 0 based indexing of vertex . returns - 1 if no sink exits . returns the vertex number - 1 if sink is found
using System ; using System . Collections . Generic ; class graph { int vertices , itr ; int [ , ] adjacency_matrix ; public graph ( int vertices ) { this . vertices = vertices ; adjacency_matrix = new int [ vertices , vertices ] ; } public void insert ( int source , int destination ) { adjacency_matrix = 1 ; } public bool issink ( int i ) { for ( int j = 0 ; j < vertices ; j ++ ) { if ( adjacency_matrix [ i , j ] == 1 ) return false ; if ( adjacency_matrix [ j , i ] == 0 && j != i ) return false ; } return true ; } public int eliminate ( ) { int i = 0 , j = 0 ; while ( i < vertices && j < vertices ) { if ( adjacency_matrix [ i , j ] == 1 ) i = i + 1 ; else j = j + 1 ; } if ( i > vertices ) return - 1 ; else if ( ! issink ( i ) ) return - 1 ; else return i ; } } public class Sink { public static void Main ( String [ ] args ) { int number_of_vertices = 6 ; graph g = new graph ( number_of_vertices ) ; g . insert ( 1 , 6 ) ; g . insert ( 2 , 3 ) ; g . insert ( 2 , 4 ) ; g . insert ( 4 , 3 ) ; g . insert ( 5 , 3 ) ; int vertex = g . eliminate ( ) ; if ( vertex >= 0 ) Console . WriteLine ( " Sink ▁ found ▁ at ▁ vertex ▁ " + ( vertex + 1 ) ) ; else Console . WriteLine ( " No ▁ Sink " ) ; } }
Reverse alternate levels of a perfect binary tree | C # program to reverse alternate levels of a tree ; Base cases ; Swap subtrees if level is even ; Recur for left and right subtrees ( Note : left of root1 is passed and right of root2 in first call and opposite in second call . ; This function calls preorder ( ) for left and right children of root ; Inorder traversal ( used to print initial and modified trees ) ; A utility function to create a new node ; Driver code
using System ; class GFG { public class Node { public char key ; public Node left , right ; } ; static void preorder ( Node root1 , Node root2 , int lvl ) { if ( root1 == null root2 == null ) return ; if ( lvl % 2 == 0 ) { char t = root1 . key ; root1 . key = root2 . key ; root2 . key = t ; } preorder ( root1 . left , root2 . right , lvl + 1 ) ; preorder ( root1 . right , root2 . left , lvl + 1 ) ; } static void reverseAlternate ( Node root ) { preorder ( root . left , root . right , 0 ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; Console . Write ( root . key + " ▁ " ) ; printInorder ( root . right ) ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . left = temp . right = null ; temp . key = ( char ) key ; return temp ; } public static void Main ( String [ ] args ) { Node root = newNode ( ' a ' ) ; root . left = newNode ( ' b ' ) ; root . right = newNode ( ' c ' ) ; root . left . left = newNode ( ' d ' ) ; root . left . right = newNode ( ' e ' ) ; root . right . left = newNode ( ' f ' ) ; root . right . right = newNode ( ' g ' ) ; root . left . left . left = newNode ( ' h ' ) ; root . left . left . right = newNode ( ' i ' ) ; root . left . right . left = newNode ( ' j ' ) ; root . left . right . right = newNode ( ' k ' ) ; root . right . left . left = newNode ( ' l ' ) ; root . right . left . right = newNode ( ' m ' ) ; root . right . right . left = newNode ( ' n ' ) ; root . right . right . right = newNode ( ' o ' ) ; Console . Write ( " Inorder ▁ Traversal ▁ of ▁ given ▁ tree STRNEWLINE " ) ; printInorder ( root ) ; reverseAlternate ( root ) ; Console . Write ( " STRNEWLINE STRNEWLINE Inorder ▁ Traversal ▁ of ▁ modified ▁ tree STRNEWLINE " ) ; printInorder ( root ) ; } }
Number of sink nodes in a graph | C # program to count number if sink nodes ; Return the number of Sink NOdes . ; Array for marking the non - sink node . ; Marking the non - sink node . ; Counting the sink nodes . ; Driver Code
using System ; class GFG { static int countSink ( int n , int m , int [ ] edgeFrom , int [ ] edgeTo ) { int [ ] mark = new int [ n + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( mark [ i ] == 0 ) count ++ ; return count ; } public static void Main ( String [ ] args ) { int n = 4 , m = 2 ; int [ ] edgeFrom = { 2 , 4 } ; int [ ] edgeTo = { 3 , 3 } ; Console . WriteLine ( countSink ( n , m , edgeFrom , edgeTo ) ) ; } }
Largest subset of Graph vertices with edges of 2 or more colors | C # program to find size of subset of graph vertex such that each vertex has more than 1 color edges ; Number of vertices ; function to calculate max subset size ; set for number of vertices ; loop for deletion of vertex from set ; if subset has only 1 vertex return 0 ; for each vertex iterate and keep removing a vertix while we find a vertex with all edges of same color . ; note down different color values for each vertex ; if only one color is found erase that vertex ( bad vertex ) ; If no vertex was removed in the above loop . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int N = 6 ; static int subsetGraph ( int [ , ] C ) { HashSet < int > vertices = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { vertices . Add ( i ) ; } while ( vertices . Count != 0 ) { if ( vertices . Count == 1 ) { return 1 ; } Boolean someone_removed = false ; foreach ( int x in vertices ) { HashSet < int > values = new HashSet < int > ( ) ; foreach ( int y in vertices ) { if ( y != x ) { values . Add ( C [ x , y ] ) ; } } if ( values . Count == 1 ) { vertices . Remove ( x ) ; someone_removed = true ; break ; } } if ( ! someone_removed ) { break ; } } return ( vertices . Count ) ; } public static void Main ( String [ ] args ) { int [ , ] C = { { 0 , 9 , 2 , 4 , 7 , 8 } , { 9 , 0 , 9 , 9 , 7 , 9 } , { 2 , 9 , 0 , 3 , 7 , 6 } , { 4 , 9 , 3 , 0 , 7 , 1 } , { 7 , 7 , 7 , 7 , 0 , 7 } , { 8 , 9 , 6 , 1 , 7 , 0 } } ; Console . WriteLine ( subsetGraph ( C ) ) ; } }
Count number of edges in an undirected graph | C # program to count number of edge in undirected graph ; Adjacency list representation of graph ; add edge to graph ; Returns count of edge in undirected graph ; traverse all vertex ; add all edge that are linked to the current vertex ; The count of edge is always even because in undirected graph every edge is connected twice between two vertices ; Driver Code ; making above uhown graph
using System ; using System . Collections . Generic ; class Graph { public int V ; public List < int > [ ] adj ; public Graph ( int V ) { this . V = V ; this . adj = new List < int > [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new List < int > ( ) ; } public void addEdge ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } public int countEdges ( ) { int sum = 0 ; for ( int i = 0 ; i < V ; i ++ ) sum += adj [ i ] . Count ; return sum / 2 ; } } class GFG { public static void Main ( String [ ] args ) { int V = 9 ; Graph g = new Graph ( V ) ; g . addEdge ( 0 , 1 ) ; g . addEdge ( 0 , 7 ) ; g . addEdge ( 1 , 2 ) ; g . addEdge ( 1 , 7 ) ; g . addEdge ( 2 , 3 ) ; g . addEdge ( 2 , 8 ) ; g . addEdge ( 2 , 5 ) ; g . addEdge ( 3 , 4 ) ; g . addEdge ( 3 , 5 ) ; g . addEdge ( 4 , 5 ) ; g . addEdge ( 5 , 6 ) ; g . addEdge ( 6 , 7 ) ; g . addEdge ( 6 , 8 ) ; g . addEdge ( 7 , 8 ) ; Console . WriteLine ( g . countEdges ( ) ) ; } }
Check whether given degrees of vertices represent a Graph or Tree | C # program to check whether input degree sequence is graph or tree ; Function returns true for tree false for graph ; Find sum of all degrees ; Graph is tree if sum is equal to 2 ( n - 1 ) ; Driver code
using System ; class GFG { static bool check ( int [ ] degree , int n ) { int deg_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { deg_sum += degree [ i ] ; } return ( 2 * ( n - 1 ) == deg_sum ) ; } public static void Main ( ) { int n = 5 ; int [ ] degree = { 2 , 3 , 1 , 1 , 1 } ; if ( check ( degree , n ) ) { Console . WriteLine ( " Tree " ) ; } else { Console . WriteLine ( " Graph " ) ; } } }