codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively | C # program for the above approach ; Function to return the count of possible pairs of A and B whose Bitwise XOR is X and Y respectively ; Stores the count of pairs ; Iterate till any bit are set ; Extract i - th bit of X and Y ; Divide X and Y by 2 ; If Xi = 1 and Yi = 2 , multiply counter by 2 ; Increase required count ; If Xi = 1 and Yi = 1 ; No answer exists ; Return the readonly count ; Driver Code ; Given X and Y ; Function Call
using System ; class GFG { static int countOfPairs ( int x , int y ) { int counter = 1 ; while ( x > 0 y > 0 ) { int bit1 = x % 2 ; int bit2 = y % 2 ; x >>= 1 ; y >>= 1 ; if ( bit1 == 1 && bit2 == 0 ) { counter *= 2 ; continue ; } if ( ( bit1 & bit2 ) > 0 ) { counter = 0 ; break ; } } return counter ; } public static void Main ( String [ ] args ) { int X = 2 , Y = 5 ; Console . Write ( countOfPairs ( X , Y ) ) ; } }
Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum | C # program for the above approach ; Function to return gcd of a and b ; Base Case ; Recursive call ; Function to find gcd of array ; Initialise the result ; Traverse the array arr [ ] ; Update result as gcd of the result and arr [ i ] ; Return the resultant GCD ; Driver Code ; Given array arr [ ] ; Function call
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findGCD ( int [ ] arr , int N ) { int result = 0 ; foreach ( int element in arr ) { result = gcd ( result , element ) ; if ( result == 1 ) { return 1 ; } } return result ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( findGCD ( arr , N ) ) ; } }
Check if any permutation of array contains sum of every adjacent pair not divisible by 3 | C # program for the above approach ; Function to checks if any permutation of the array exists whose sum of adjacent pairs is not divisible by 3 ; Count remainder 0 ; Count remainder 1 ; Count remainder 2 ; Condition for valid arrangements ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static void factorsOf3 ( int [ ] arr , int N ) { int a = 0 , b = 0 , c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 3 == 0 ) a ++ ; else if ( arr [ i ] % 3 == 1 ) b ++ ; else if ( arr [ i ] % 3 == 2 ) c ++ ; } if ( a >= 1 && a <= b + c + 1 ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else if ( a == 0 && b == 0 && c > 0 ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else if ( a == 0 && c == 0 && b > 0 ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else Console . Write ( " No " + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 } ; int N = arr . Length ; factorsOf3 ( arr , N ) ; } }
Check if a number is a perfect square having all its digits as a perfect square | C # program for the above approach ; Function to check if digits of N is a perfect square or not ; Iterate over the digits ; Extract the digit ; Check if digit is a perfect square or not ; Divide N by 10 ; Return true ; Function to check if N is a perfect square or not ; If floor and ceil of n is not same ; Function to check if N satisfies the required conditions or not ; If both the conditions are satisfied ; Driver Code ; Function Call
using System ; class GFG { static bool check_digits ( long N ) { while ( N > 0 ) { int n = ( int ) ( N % 10 ) ; if ( ( n != 0 ) && ( n != 1 ) && ( n != 4 ) && ( n != 9 ) ) { return false ; } N = N / 10 ; } return true ; } static bool is_perfect ( long N ) { double n = Math . Sqrt ( N ) ; if ( Math . Floor ( n ) != Math . Ceiling ( n ) ) { return false ; } return true ; } static void isFullSquare ( long N ) { if ( is_perfect ( N ) && check_digits ( N ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { long N = 144 ; isFullSquare ( N ) ; } }
Count substrings with different first and last characters | C # program for the above approach ; Function to count the subStrings having different first & last character ; Stores frequency of each char ; Loop to store frequency of the characters in a Map ; To store readonly result ; Traversal of String ; Store answer for every iteration ; Map traversal ; Compare current char ; Print the readonly count ; Driver Code ; Given String ; Length of the String ; Function call
using System ; using System . Collections . Generic ; class GFG { static void countSubString ( char [ ] s , int n ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . ContainsKey ( s [ i ] ) ) { mp [ s [ i ] ] = mp [ s [ i ] ] + 1 ; } else { mp . Add ( s [ i ] , 1 ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; if ( mp . ContainsKey ( s [ i ] ) ) { mp [ s [ i ] ] = mp [ s [ i ] ] - 1 ; foreach ( KeyValuePair < char , int > value in mp ) { if ( value . Key == s [ i ] ) { continue ; } else { cnt += value . Value ; } } ans += cnt ; } } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { String S = " abcab " ; int N = 5 ; countSubString ( S . ToCharArray ( ) , N ) ; } }
Maximize count of empty water bottles from N filled bottles | C # program for the above approach ; Function to find the maximum bottles that can be emptied ; Iterate until a is non - zero ; Add the number of bottles that are emptied ; Update a after exchanging empty bottles ; Stores the number of bottles left after the exchange ; Return the answer ; Driver Code ; Function call
using System ; class GFG { static int maxBottles ( int n , int e ) { int s = 0 , b = 0 ; int a = n ; while ( a != 0 ) { s = s + a ; a = ( a + b ) / e ; b = n - ( a * e ) ; n = a + b ; } return s ; } public static void Main ( ) { int n = 9 , e = 3 ; int s = maxBottles ( n , e ) ; Console . Write ( s + " STRNEWLINE " ) ; } }
Count all N digit numbers whose digits are multiple of X | C # program for the above approach ; Function to calculate x ^ n using binary - exponentiation ; Stores the resultant power ; Stores the value of x ^ ( n / 2 ) ; Function to count aint N - digit numbers whose digits are multiples of x ; Count aint digits which are multiples of x ; Check if current number is a multiple of X ; Increase count of multiples ; Check if it 's a 1 digit number ; Count the total numbers ; Return the total numbers ; Driver Code ; Given N and X ; Function call
using System ; class GFG { static int power ( int x , int n ) { int temp ; if ( n == 0 ) return 1 ; temp = power ( x , n / 2 ) ; if ( n % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } static int count_Total_Numbers ( int n , int x ) { int total , multiples = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( i % x == 0 ) multiples ++ ; } if ( n == 1 ) return multiples ; total = ( multiples - 1 ) * power ( multiples , n - 1 ) ; return total ; } public static void Main ( ) { int N = 1 , X = 3 ; Console . Write ( count_Total_Numbers ( N , X ) ) ; } }
Find the vertex diagonally opposite to the vertex M from an N | C # program for the above approach ; Function to return the required vertex ; Case 1 : ; Case 2 : ; Driver Code
using System ; class GFG { static int getPosition ( int N , int M ) { if ( M > ( N / 2 ) ) { return ( M - ( N / 2 ) ) ; } return ( M + ( N / 2 ) ) ; } public static void Main ( String [ ] args ) { int N = 8 , M = 5 ; Console . Write ( getPosition ( N , M ) ) ; } }
Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | C # program for the above approach ; Function that counts the number of strings of length three that can be made with given m 0 s and n 1 s ; Iterate until N & M are positive ; Case 1 : ; Case 2 : ; Print the count of strings ; Driver Code ; Given count of 1 s and 0 s ; Function call
using System ; class GFG { static void number_of_strings ( int N , int M ) { int ans = 0 ; while ( N > 0 && M > 0 ) { if ( N > M ) { if ( N >= 2 ) { N -= 2 ; -- M ; ++ ans ; } else { break ; } } else { if ( M >= 2 ) { M -= 2 ; -- N ; ++ ans ; } else { break ; } } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int N = 4 , M = 19 ; number_of_strings ( N , M ) ; } }
Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum | C # program for the above approach ; Function that returns the minimum length of the array after merging unequal adjacent elements ; Stores the first element and its frequency ; Traverse the array ; If all elements are equal ; No merge - pair operations can be performed ; Otherwise ; Driver Code ; Given array ; Length of the array ; Function Call
using System ; class GFG { static int minLength ( int [ ] A , int N ) { int elem = A [ 0 ] , count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( A [ i ] == elem ) { count ++ ; } else { break ; } } if ( count == N ) return N ; else return 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 3 , 1 } ; int N = arr . Length ; Console . Write ( minLength ( arr , N ) + " STRNEWLINE " ) ; } }
Reduce N to 0 or less by given X and Y operations | C # program to implement the above approach ; Function to check if N can be reduced to <= 0 or not ; Update N to N / 2 + 10 at most X times ; Update N to N - 10 Y times ; Driver Code
using System ; class GFG { public static bool NegEqu ( int N , int X , int Y ) { while ( X > 0 && ( N > N / 2 + 10 ) ) { N = N / 2 + 10 ; X -= 1 ; } while ( Y > 0 ) { N = N - 10 ; Y -= 1 ; } if ( N <= 0 ) return true ; return false ; } public static void Main ( String [ ] args ) { int N = 100 ; int X = 3 ; int Y = 4 ; if ( NegEqu ( N , X , Y ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Split array into minimum number of subarrays having GCD of its first and last element exceeding 1 | C # program for the above approach ; Function to find the minimum number of subarrays ; Right pointer ; Left pointer ; Count of subarrays ; Find GCD ( left , right ) ; Found a valid large subarray between arr [ left , right ] ; Searched the arr [ 0. . right ] and found no subarray having size > 1 and having gcd ( left , right ) > 1 ; Driver Code ; Function call
using System ; class GFG { static int minSubarrays ( int [ ] arr , int n ) { int right = n - 1 ; int left = 0 ; int subarrays = 0 ; while ( right >= 0 ) { for ( left = 0 ; left <= right ; left += 1 ) { if ( __gcd ( arr [ left ] , arr [ right ] ) > 1 ) { subarrays += 1 ; right = left - 1 ; break ; } if ( left == right && __gcd ( arr [ left ] , arr [ right ] ) == 1 ) { return 0 ; } } } return subarrays ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( ) { int N = 6 ; int [ ] arr = { 2 , 3 , 4 , 4 , 4 , 3 } ; Console . Write ( minSubarrays ( arr , N ) ) ; } }
Find the largest possible value of K such that K modulo X is Y | C # program for the above approach ; Function to find the largest positive integer K such that K % x = y ; Stores the minimum solution ; Return the maximum possible value ; Driver Code
using System ; class GFG { static int findMaxSoln ( int n , int x , int y ) { int ans = int . MinValue ; for ( int k = 0 ; k <= n ; k ++ ) { if ( k % x == y ) { ans = Math . Max ( ans , k ) ; } } return ( ( ans >= 0 && ans <= n ) ? ans : - 1 ) ; } public static void Main ( String [ ] args ) { int n = 15 , x = 10 , y = 5 ; Console . Write ( findMaxSoln ( n , x , y ) ) ; } }
Count all numbers up to N having M as the last digit | C # program to implement the above approach ; Function to count the numbers ending with M ; Stores the count of numbers required ; Calculate count upto nearest power of 10 ; Computing the value of x ; Adding the count of numbers ending at M from x to N ; Driver Code ; Function call
using System ; class GFG { static int getCount ( int N , int M ) { int total_count = 0 ; total_count += ( N / 10 ) ; int x = ( N / 10 ) * 10 ; if ( ( N - x ) >= M ) { total_count = total_count + 1 ; } return total_count ; } public static void Main ( String [ ] args ) { int N = 100 , M = 1 ; Console . Write ( getCount ( N , M ) ) ; } }
Count of Right | C # program for the above approach ; Function to find the number of right angled triangle that are formed from given N points whose perpendicular or base is parallel to X or Y axis ; To store the number of points has same x or y coordinates ; Store the total count of triangle ; Iterate to check for total number of possible triangle ; Add the count of triangles formed ; Total possible triangle ; Driver Code ; Given N points ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int RightAngled ( int [ , ] a , int n ) { Dictionary < int , int > xpoints = new Dictionary < int , int > ( ) ; Dictionary < int , int > ypoints = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( xpoints . ContainsKey ( a [ i , 0 ] ) ) { xpoints [ a [ i , 0 ] ] = xpoints [ a [ i , 0 ] ] + 1 ; } else { xpoints . Add ( a [ i , 0 ] , 1 ) ; } if ( ypoints . ContainsKey ( a [ i , 1 ] ) ) { ypoints [ a [ i , 1 ] ] = ypoints [ a [ i , 1 ] ] + 1 ; } else { ypoints . Add ( a [ i , 1 ] , 1 ) ; } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( xpoints [ a [ i , 0 ] ] >= 1 && ypoints [ a [ i , 1 ] ] >= 1 ) { count += ( xpoints [ a [ i , 0 ] ] - 1 ) * ( ypoints [ a [ i , 1 ] ] - 1 ) ; } } return count ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ , ] arr = { { 1 , 2 } , { 2 , 1 } , { 2 , 2 } , { 2 , 3 } , { 3 , 2 } } ; Console . Write ( RightAngled ( arr , N ) ) ; } }
Smallest subarray which upon repetition gives the original array | C # program for the above approach ; Function to print the array ; Function to find the smallest subarray ; Corner Case ; Initialize the auxiliary subarray ; Push the first 2 elements into the subarray brr [ ] ; Iterate over the length of subarray ; If array can be divided into subarray of i equal length ; Check if on repeating the current subarray gives the original array or not ; Subarray found ; Add current element into subarray ; No subarray found ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void printArray ( List < int > brr ) { foreach ( int it in brr ) { Console . Write ( it + " ▁ " ) ; } } static void RepeatingSubarray ( int [ ] arr , int N ) { if ( N < 2 ) { Console . Write ( " - 1" ) ; } List < int > brr = new List < int > ( ) ; brr . Add ( arr [ 0 ] ) ; brr . Add ( arr [ 1 ] ) ; for ( int i = 2 ; i < N / 2 + 1 ; i ++ ) { if ( N % i == 0 ) { bool a = false ; int n = brr . Count ; int j = i ; while ( j < N ) { int K = j % i ; if ( arr [ j ] == brr [ K ] ) { j ++ ; } else { a = true ; break ; } } if ( ! a && j == N ) { printArray ( brr ) ; return ; } } brr . Add ( arr [ i ] ) ; } Console . Write ( " - 1" ) ; return ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 } ; int N = arr . Length ; RepeatingSubarray ( arr , N ) ; } }
Find all missing numbers from a given sorted array | C # program for the above approach ; Function to find the missing elements ; Initialize diff ; Check if diff and arr [ i ] - i both are equal or not ; Loop for consecutive missing elements ; Driver code ; Given array arr [ ] ; Function call
using System ; class GFG { static void printMissingElements ( int [ ] arr , int N ) { int diff = arr [ 0 ] - 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] - i != diff ) { while ( diff < arr [ i ] - i ) { Console . Write ( i + diff + " ▁ " ) ; diff ++ ; } } } } static void Main ( ) { int [ ] arr = { 6 , 7 , 10 , 11 , 13 } ; int N = arr . Length ; printMissingElements ( arr , N ) ; } }
Find all missing numbers from a given sorted array | C # program for the above approach ; Function to find the missing elements ; Initialize an array with zero of size equals to the maximum element in the array ; Make b [ i ] = 1 if i is present in the array ; If the element is present make b [ arr [ i ] ] = 1 ; Print the indices where b [ i ] = 0 ; Driver Code ; Given array [ ] arr ; Function call
using System ; class GFG { static void printMissingElements ( int [ ] arr , int N ) { int [ ] b = new int [ arr [ N - 1 ] + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( int i = arr [ 0 ] ; i <= arr [ N - 1 ] ; i ++ ) { if ( b [ i ] == 0 ) { Console . Write ( i + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 7 , 10 , 11 , 13 } ; int N = arr . Length ; printMissingElements ( arr , N ) ; } }
Least common element in given two Arithmetic sequences | C # program to implement the above approach ; Function to find the smallest element common in both the subsequences ; If a is equal to c ; If a exceeds c ; Check for the satisfying equation ; Least value of possible_y satisfying the given equation will yield true in the below if and break the loop ; If the value of possible_y satisfying the given equation lies in range [ 0 , b ] ; If no value of possible_y satisfies the given equation ; Driver Code
using System ; class GFG { static int smallestCommon ( int a , int b , int c , int d ) { if ( a == c ) return a ; if ( a > c ) { swap ( a , c ) ; swap ( b , d ) ; } int first_term_diff = ( c - a ) ; int possible_y ; for ( possible_y = 0 ; possible_y < b ; possible_y ++ ) { if ( ( first_term_diff % b + possible_y * d ) % b == 0 ) { break ; } } if ( possible_y != b ) { return c + possible_y * d ; } return - 1 ; } static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } public static void Main ( String [ ] args ) { int A = 2 , B = 20 , C = 19 , D = 9 ; Console . Write ( smallestCommon ( A , B , C , D ) ) ; } }
Minimum number of flips with rotation to make binary string alternating | C # program for the above approach ; Function that finds the minimum number of flips to make the binary string alternating if left circular rotation is allowed ; Initialize prefix arrays to store number of changes required to put 1 s at either even or odd position ; If i is odd ; Update the oddone and evenone count ; Else i is even ; Update the oddone and evenone count ; Initialize minimum flips ; Check if substring [ 0 , i ] is appended at end how many changes will be required ; Return minimum flips ; Driver Code ; Given String ; Length of given string ; Function call
using System ; class GFG { static int MinimumFlips ( String s , int n ) { int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( s [ i ] == '1' ? 1 : 0 ) ; } int [ ] oddone = new int [ n + 1 ] ; int [ ] evenone = new int [ n + 1 ] ; oddone [ 0 ] = 0 ; evenone [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; } else { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; } } int minimum = Math . Min ( oddone [ n ] , evenone [ n ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( n % 2 != 0 ) { minimum = Math . Min ( minimum , oddone [ n ] - oddone [ i + 1 ] + evenone [ i + 1 ] ) ; minimum = Math . Min ( minimum , evenone [ n ] - evenone [ i + 1 ] + oddone [ i + 1 ] ) ; } } return minimum ; } public static void Main ( String [ ] args ) { String S = "000001100" ; int n = S . Length ; Console . Write ( MinimumFlips ( S , n ) ) ; } }
Create a Graph by connecting divisors from N to M and find shortest path | C # program for the above approach ; Function to check the number is prime or not ; Base Cases ; Iterate till [ 5 , Math . Sqrt ( N ) ] to detect primarility of numbers ; Function to print the shortest path ; Use vector to store the factor of m and n ; Use map to check if largest common factor previously present or not ; First store m ; Check whether m is prime or not ; Largest common factor of m ; If m is divisible by i ; Store the largest common factor ; For number n ; Check whether n is prime ; Largest common factor of n ; Store the largest common factor ; Print the path Print factors from m ; To astatic void duplicate printing of same element ; Print the factors from n ; Driver Code ; Given N and M ; Function call
using System ; using System . Collections . Generic ; class GFG { static int isprm ( int n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 1 ; if ( n % 2 == 0 n % 3 == 0 ) return 0 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return 0 ; return 1 ; } static void shortestpath ( int m , int n ) { List < int > mfactor = new List < int > ( ) ; List < int > nfactor = new List < int > ( ) ; Dictionary < int , int > fre = new Dictionary < int , int > ( ) ; mfactor . Add ( m ) ; fre . Add ( m , 1 ) ; while ( m != 1 ) { if ( isprm ( m ) != 0 ) { mfactor . Add ( 1 ) ; if ( ! fre . ContainsKey ( 1 ) ) fre . Add ( 1 , 1 ) ; m = 1 ; } else { for ( int i = 2 ; i <= Math . Sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { mfactor . Add ( m / i ) ; if ( ! fre . ContainsKey ( m / i ) ) fre . Add ( m / i , 1 ) ; m = ( m / i ) ; break ; } } } } nfactor . Add ( n ) ; while ( fre . ContainsKey ( n ) && fre [ n ] != 1 ) { if ( isprm ( n ) != 0 ) { nfactor . Add ( 1 ) ; n = 1 ; } else { for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { nfactor . Add ( n / i ) ; n = ( n / i ) ; break ; } } } } for ( int i = 0 ; i < mfactor . Count ; i ++ ) { if ( mfactor [ i ] == n ) break ; Console . Write ( mfactor [ i ] + " ▁ < - - > ▁ " ) ; } for ( int i = nfactor . Count - 1 ; i >= 0 ; i -- ) { if ( i == 0 ) Console . Write ( nfactor [ i ] ) ; else Console . Write ( nfactor [ i ] + " ▁ < - - > ▁ " ) ; } } public static void Main ( String [ ] args ) { int m = 18 , n = 19 ; shortestpath ( m , n ) ; } }
Possible number of Trees having N vertex | C # program for the above approach ; Function to count the total number of trees possible ; Find the max element in the given array ; Level array store the number of nodes on level i initially all values are zero ; In this case tree can not be created ; To store the count of trees ; Iterate until maxElement ; Calculate level [ i ] ^ level [ i + 1 ] ; Update the count of tree ; Return the readonly count of trees ; Driver Code ; Given array [ ] arr ; Function call
using System ; using System . Linq ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int NumberOfTrees ( int [ ] arr , int N ) { int maxElement = arr . Max ( ) ; int [ ] level = new int [ maxElement + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } int ans = 1 ; for ( int i = 0 ; i < maxElement ; i ++ ) { for ( int j = 0 ; j < level [ i + 1 ] ; j ++ ) { ans = ( ans * level [ i ] ) % mod ; } } return ans ; } public static void Main ( String [ ] args ) { int N = 7 ; int [ ] arr = { 0 , 3 , 2 , 1 , 2 , 2 , 1 } ; Console . Write ( NumberOfTrees ( arr , N ) ) ; } }
Possible number of Trees having N vertex | C # program for the above approach ; Function that finds the value of x ^ y using Modular Exponentiation ; Base Case ; If y is odd , multiply x with result ; Return the value ; Function that counts the total number of trees possible ; Find the max element in array ; Level array store the number nodes on level i initially all values are 0 ; In this case tree can not be created ; Calculate level [ i ] ^ level [ i + 1 ] ; Return the readonly count ; Driver Code ; Given array [ ] arr ; Function call
using System ; using System . Linq ; class GFG { static int mod = ( int ) 1e9 + 7 ; static int power ( int x , int y ) { if ( y == 0 ) return 1 ; int p = power ( x , y / 2 ) % mod ; p = ( p * p ) % mod ; if ( ( y & 1 ) != 0 ) p = ( x * p ) % mod ; return p ; } static int NumberOfTrees ( int [ ] arr , int N ) { int maxElement = arr . Max ( ) ; int [ ] level = new int [ maxElement + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } int ans = 1 ; for ( int i = 0 ; i < maxElement ; i ++ ) { ans = ( ans * power ( level [ i ] , level [ i + 1 ] ) ) % mod ; } return ans ; } public static void Main ( String [ ] args ) { int N = 7 ; int [ ] arr = { 0 , 3 , 2 , 1 , 2 , 2 , 1 } ; Console . Write ( NumberOfTrees ( arr , N ) ) ; } }