codeparrot/xlcost-text-to-code huggingface dataset:
Count subarrays for every array element in which they are the minimum | C # implementation of the above approach ; Function to required count subarrays ; For storing count of subarrays ; For finding next smaller element left to a element if there is no next smaller element left to it than taking - 1. ; For finding next smaller element right to a element if there is no next smaller element right to it than taking n . ; Taking exact boundaries in which arr [ i ] is minimum ; Similarly for right side ; Driver code ; Given array arr [ ] ; Function call
using System ; using System . Collections ; class GFG { static int [ ] countingSubarray ( int [ ] arr , int n ) { int [ ] a = new int [ n ] ; int [ ] nsml = new int [ n ] ; Array . Fill ( nsml , - 1 ) ; int [ ] nsmr = new int [ n ] ; Array . Fill ( nsmr , n ) ; Stack st = new Stack ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( st . Count > 0 && arr [ ( int ) st . Peek ( ) ] >= arr [ i ] ) st . Pop ( ) ; nsmr [ i ] = ( st . Count > 0 ) ? ( int ) st . Peek ( ) : n ; st . Push ( i ) ; } while ( st . Count > 0 ) st . Pop ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( st . Count > 0 && arr [ ( int ) st . Peek ( ) ] >= arr [ i ] ) st . Pop ( ) ; nsml [ i ] = ( st . Count > 0 ) ? ( int ) st . Peek ( ) : - 1 ; st . Push ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { nsml [ i ] ++ ; nsmr [ i ] -- ; int r = nsmr [ i ] - i + 1 ; int l = i - nsml [ i ] + 1 ; a [ i ] = r * l ; } return a ; } static void Main ( ) { int N = 5 ; int [ ] arr = { 3 , 2 , 4 , 1 , 5 } ; int [ ] a = countingSubarray ( arr , N ) ; Console . Write ( " [ " ) ; int n = a . Length - 1 ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " , ▁ " ) ; Console . Write ( a [ n ] + " ] " ) ; } }
Lexicographically smallest permutation of a string that contains all substrings of another string | C # program for the above approach ; Function to reorder the String B to contain all the subStrings of A ; Find length of Strings ; Initialize array to count the frequencies of the character ; Counting frequencies of character in B ; Find remaining character in B ; Declare the reordered String ; Loop until freq [ j ] > 0 ; Decrement the value from freq array ; Check if A [ j ] > A [ 0 ] ; Check if A [ j ] < A [ 0 ] ; Append the remaining characters to the end of the result ; Push all the values from frequency array in the answer ; Return the answer ; Driver Code ; Given Strings A and B ; Function Call
using System ; class GFG { static String reorderString ( char [ ] A , char [ ] B ) { int size_a = A . Length ; int size_b = B . Length ; int [ ] freq = new int [ 300 ] ; for ( int i = 0 ; i < size_b ; i ++ ) freq [ B [ i ] ] ++ ; for ( int i = 0 ; i < size_a ; i ++ ) freq [ A [ i ] ] -- ; for ( int j = ' a ' ; j <= ' z ' ; j ++ ) { if ( freq [ j ] < 0 ) return " - 1" ; } String answer = " " ; for ( int j = ' a ' ; j < A [ 0 ] ; j ++ ) while ( freq [ j ] > 0 ) { answer += j ; freq [ j ] -- ; } int first = A [ 0 ] ; for ( int j = 0 ; j < size_a ; j ++ ) { if ( A [ j ] > A [ 0 ] ) break ; if ( A [ j ] < A [ 0 ] ) { answer += String . Join ( " " , A ) ; A = new char [ A . Length ] ; break ; } } while ( freq [ first ] > 0 ) { answer += String . Join ( " " , ( char ) first ) ; -- freq [ first ] ; } answer += String . Join ( " " , A ) ; for ( int j = ' a ' ; j <= ' z ' ; j ++ ) while ( freq [ j ] -- > 0 ) answer += ( ( char ) j ) ; return answer ; } public static void Main ( String [ ] args ) { String A = " aa " ; String B = " ababab " ; Console . Write ( reorderString ( A . ToCharArray ( ) , B . ToCharArray ( ) ) ) ; } }
Number from a range [ L , R ] having Kth minimum cost of conversion to 1 by given operations | C # implementation of the above approach ; Function to calculate the cost ; Base case ; Even condition ; Odd condition ; Return cost ; Function to find Kth element ; Array to store the costs ; Sort the array based on cost ; Driver code ; Given range and6 K ; Function call
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int func ( int n ) { int count = 0 ; if ( n == 2 n == 1 ) return 1 ; if ( n % 2 == 0 ) count = 1 + func ( n / 2 ) ; if ( n % 2 != 0 ) count = 1 + func ( n * 3 + 1 ) ; return count ; } static void findKthElement ( int l , int r , int k ) { List < int > arr = new List < int > ( ) ; for ( int i = l ; i <= r ; i ++ ) arr . Add ( i ) ; Dictionary < int , int > result = new Dictionary < int , int > ( ) ; foreach ( int i in arr ) { result . Add ( i , func ( i ) ) ; } var myList = result . ToList ( ) ; myList . Sort ( ( pair1 , pair2 ) => pair1 . Value . CompareTo ( pair2 . Value ) ) ; Console . WriteLine ( myList [ 1 ] . Key ) ; } public static void Main ( String [ ] args ) { int l = 12 ; int r = 15 ; int k = 2 ; findKthElement ( l , r , k ) ; } }
Check if a string represents a hexadecimal number or not | C # implementation of the above approach ; Function to check if the string represents a hexadecimal number ; Size of string ; Iterate over string ; Check if the character is invalid ; Print true if all characters are valid ; Driver Code ; Given string ; Function Call
using System ; class GFG { public static void checkHex ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s [ i ] ; if ( ( ch < '0' ch > '9' ) && ( ch < ' A ' ch > ' F ' ) ) { Console . WriteLine ( " No " ) ; return ; } } Console . WriteLine ( " Yes " ) ; } public static void Main ( String [ ] args ) { String s = " BF57C " ; checkHex ( s ) ; } }
Longest subsequence forming an Arithmetic Progression ( AP ) | C # program for the above approach ; Function that finds the longest arithmetic subsequence having the same absolute difference ; Stores the length of sequences having same difference ; Stores the resultant length ; Iterate over the array ; Update length of subsequence ; Return res ; Driver Code ; Given array arr [ ] ; Function call
using System ; using System . Collections . Generic ; class GFG { static int lenghtOfLongestAP ( int [ ] A , int n ) { Dictionary < int , Dictionary < int , int > > dp = new Dictionary < int , Dictionary < int , int > > ( ) ; int res = 2 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int d = A [ j ] - A [ i ] ; if ( dp . ContainsKey ( d ) ) { if ( dp [ d ] . ContainsKey ( i ) ) { dp [ d ] [ j ] = dp [ d ] [ i ] + 1 ; } else { dp [ d ] [ j ] = 2 ; } } else { dp [ d ] = new Dictionary < int , int > ( ) ; dp [ d ] [ j ] = 2 ; } res = Math . Max ( res , dp [ d ] [ j ] ) ; } } return res ; } public static void Main ( string [ ] args ) { int [ ] arr = { 20 , 1 , 15 , 3 , 10 , 5 , 8 } ; int N = arr . Length ; Console . Write ( lenghtOfLongestAP ( arr , N ) ) ; } }
Length of longest increasing prime subsequence from a given array | C # program for the above approach ; Function to find the prime numbers till 10 ^ 5 using Sieve of Eratosthenes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function which computes the length of the LIS of Prime Numbers ; Create an array of size n ; Create bool array to mark prime numbers ; Initialize all values to true ; Precompute N primes ; Compute optimized LIS having prime numbers in bottom up manner ; Check for LIS and prime ; Return maximum value in lis [ ] ; Driver Code ; Given array ; Size of array ; Function call
using System ; using System . Linq ; class GFG { static readonly int N = 100005 ; static void SieveOfEratosthenes ( bool [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int LISPrime ( int [ ] arr , int n ) { int [ ] lisp = new int [ n ] ; bool [ ] prime = new bool [ N + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , N ) ; lisp [ 0 ] = prime [ arr [ 0 ] ] ? 1 : 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { lisp [ i ] = 0 ; continue ; } lisp [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( prime [ arr [ j ] ] && arr [ i ] > arr [ j ] && lisp [ i ] < lisp [ j ] + 1 ) { lisp [ i ] = lisp [ j ] + 1 ; } } } return lisp . Max ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 2 , 5 , 1 , 7 } ; int M = arr . Length ; Console . Write ( LISPrime ( arr , M ) ) ; } }
Check if a given number can be expressed as pair | C # program of the above approach ; Function to check if the number is pair - sum of sum of first X natural numbers ; Check if the given number is sum of pair of special numbers ; X is the sum of first i natural numbers ; t = 2 * Y ; Condition to check if Y is a special number ; Driver Code ; Function call
using System ; class GFG { static void checkSumOfNatural ( int n ) { int i = 1 ; bool flag = false ; while ( i * ( i + 1 ) < n * 2 ) { int X = i * ( i + 1 ) ; int t = n * 2 - X ; int k = ( int ) Math . Sqrt ( t ) ; if ( k * ( k + 1 ) == t ) { flag = true ; break ; } i += 1 ; } if ( flag ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { int n = 25 ; checkSumOfNatural ( n ) ; } }
Count distinct regular bracket sequences which are not N periodic | C # program for above approach ; Function that finds the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Return the C ( n , k ) ; Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; Return C ( 2 n , n ) / ( n + 1 ) ; Function to find possible ways to put balanced parenthesis in an expression of length n ; If n is odd , not possible to create any valid parentheses ; Otherwise return n / 2 th Catalan Number ; Difference between counting ways of 2 * N and N is the result ; Driver Code ; Given value of N ; Function call
using System ; using System . Collections . Generic ; class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } static long findWays ( int n ) { if ( ( n & 1 ) == 1 ) return 0 ; return catalan ( n / 2 ) ; } static void countNonNPeriodic ( int N ) { Console . Write ( findWays ( 2 * N ) - findWays ( N ) ) ; } public static void Main ( string [ ] args ) { int N = 4 ; countNonNPeriodic ( N ) ; } }
Check if two elements of a matrix are on the same diagonal or not | C # implementation of the above approach ; Function to check if two integers are on the same diagonal of the matrix ; Storing indexes of x in I , J ; Storing Indexes of y in P , Q ; Condition to check if the both the elements are in same diagonal of a matrix ; Driver Code ; Dimensions of Matrix ; Given Matrix ; Elements to be checked ; Function call
using System ; class GFG { static void checkSameDiag ( int [ , ] li , int x , int y , int m , int n ) { int I = 0 , J = 0 ; int P = 0 , Q = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( li [ i , j ] == x ) { I = i ; J = j ; } if ( li [ i , j ] == y ) { P = i ; Q = j ; } } } if ( P - Q == I - J P + Q == I + J ) { Console . WriteLine ( " YES " ) ; } else Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { int m = 6 ; int n = 5 ; int [ , ] li = { { 32 , 94 , 99 , 26 , 82 } , { 51 , 69 , 52 , 63 , 17 } , { 90 , 36 , 88 , 55 , 33 } , { 93 , 42 , 73 , 39 , 28 } , { 81 , 31 , 83 , 53 , 10 } , { 12 , 29 , 85 , 80 , 87 } } ; int x = 42 ; int y = 80 ; checkSameDiag ( li , x , y , m , n ) ; } }
Count of decrement operations required to obtain K in N steps | C # program for the above approach ; Function to check whether m number of steps of type 1 are valid or not ; If m and n are the count of operations of type 1 and type 2 respectively , then n - m operations are performed ; Find the value of S after step 2 ; If m steps of type 1 is valid ; Function to find the number of operations of type 1 required ; Iterate over the range ; Find the value of mid ; Check if m steps of type 1 are valid or not ; If mid is the valid number of steps ; If no valid number of steps exist ; Driver Code ; Given and N , K ; Function call
using System ; class GFG { static int isValid ( int n , int m , int k ) { int step2 = n - m ; int cnt = ( step2 * ( step2 + 1 ) ) / 2 ; if ( cnt - m == k ) return 0 ; if ( cnt - m > k ) return 1 ; return - 1 ; } static void countOfOperations ( int n , int k ) { int start = 0 , end = n ; bool ok = true ; while ( start <= end ) { int mid = ( start + end ) / 2 ; int temp = isValid ( n , mid , k ) ; if ( temp == 0 ) { ok = false ; Console . Write ( mid ) ; break ; } else if ( temp == 1 ) { start = mid + 1 ; } else { end = mid - 1 ; } } if ( ok ) Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int N = 5 , K = 4 ; countOfOperations ( N , K ) ; } }
Print all Possible Decodings of a given Digit Sequence | C # program for the above approach ; Function to check if all the characters are lowercase or not ; Traverse the string ; If any character is not found to be in lowerCase ; Function to print the decodings ; If all characters are not in lowercase ; Function to return the character corresponding to given integer ; Function to return the decodings ; Base case ; Recursive call ; Stores the characters of two digit numbers ; Extract first digit and first two digits ; Check if it lies in the range of alphabets ; Next recursive call ; Combine both the output in a single readonly output array ; Index of readonly output array ; Store the elements of output1 in readonly output array ; Store the elements of output2 in readonly output array ; Result the result ; Driver Code ; Function call ; Print function call
using System ; class GFG { public static bool nonLower ( String s ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! char . IsLower ( s [ i ] ) ) { return true ; } } return false ; } public static void printCodes ( String [ ] output ) { for ( int i = 0 ; i < output . Length ; i ++ ) { if ( nonLower ( output [ i ] ) ) continue ; Console . WriteLine ( output [ i ] ) ; } } public static char getChar ( int n ) { return ( char ) ( n + 96 ) ; } public static String [ ] getCode ( String str ) { if ( str . Length == 0 ) { String [ ] ans = { " " } ; return ans ; } String [ ] output1 = getCode ( str . Substring ( 1 ) ) ; String [ ] output2 = new String [ 0 ] ; int firstDigit = ( str [ 0 ] - '0' ) ; int firstTwoDigit = 0 ; if ( str . Length >= 2 ) { firstTwoDigit = ( str [ 0 ] - '0' ) * 10 + ( str [ 1 ] - '0' ) ; if ( firstTwoDigit >= 10 && firstTwoDigit <= 26 ) { output2 = getCode ( str . Substring ( 2 ) ) ; } } String [ ] output = new String [ output1 . Length + output2 . Length ] ; int k = 0 ; for ( int i = 0 ; i < output1 . Length ; i ++ ) { char ch = getChar ( firstDigit ) ; output [ i ] = ch + output1 [ i ] ; k ++ ; } for ( int i = 0 ; i < output2 . Length ; i ++ ) { char ch = getChar ( firstTwoDigit ) ; output [ k ] = ch + output2 [ i ] ; k ++ ; } return output ; } public static void Main ( String [ ] args ) { String input = "101" ; String [ ] output = getCode ( input ) ; printCodes ( output ) ; } }
Count prime numbers that can be expressed as sum of consecutive prime numbers | C # program for the above approach ; Function to check if a number is prime or not ; Base Case ; Iterate till [ 5 , Math . Sqrt ( N ) ] to detect primality of numbers ; If N is divisible by i or i + 2 ; Return 1 if N is prime ; Function to count the prime numbers which can be expressed as sum of consecutive prime numbers ; Initialize count as 0 ; Stores prime numbers ; If i is prime ; Initialize the sum ; Find all required primes upto N ; Add it to the sum ; Return the readonly count ; Driver Code ; Given number N ; 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 int countprime ( int n ) { int count = 0 ; List < int > primevector = new List < int > ( ) ; for ( int i = 2 ; i <= n ; i ++ ) { if ( isprm ( i ) == 1 ) { primevector . Add ( i ) ; } } int sum = primevector [ 0 ] ; for ( int i = 1 ; i < primevector . Count ; i ++ ) { sum += primevector [ i ] ; if ( sum > n ) break ; if ( isprm ( sum ) == 1 ) { count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int N = 45 ; Console . Write ( countprime ( N ) ) ; } }
Find a subarray of size K whose sum is a perfect square | C # program for the above approach ; Function to check if a given number is a perfect square or not ; Find square root of n ; Check if the square root is an integer or not ; Function to print the subarray whose sum is a perfect square ; Sum of first K elements ; If the first k elements have a sum as perfect square ; Iterate through the array ; If sum is perfect square ; If subarray not found ; Driver Code ; Given array ; Given subarray size K ; Function Call
using System ; class GFG { class pair { public int first , second ; } static bool isPerfectSquare ( int n ) { double sr = Math . Sqrt ( n ) ; return ( ( sr - Math . Floor ( sr ) ) == 0 ) ; } static void SubarrayHavingPerfectSquare ( int [ ] arr , int k ) { pair ans = new pair ( ) ; int sum = 0 , i ; for ( i = 0 ; i < k ; i ++ ) { sum += arr [ i ] ; } bool found = false ; if ( isPerfectSquare ( sum ) ) { ans . first = 0 ; ans . second = i - 1 ; } else { for ( int j = i ; j < arr . Length ; j ++ ) { sum = sum + arr [ j ] - arr [ j - k ] ; if ( isPerfectSquare ( sum ) ) { found = true ; ans . first = j - k + 1 ; ans . second = j ; } } for ( int k1 = ans . first ; k1 <= ans . second ; k1 ++ ) { Console . Write ( arr [ k1 ] + " ▁ " ) ; } } if ( found == false ) { Console . Write ( " - 1" ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 20 , 34 , 51 , 10 , 99 , 87 , 23 , 45 } ; int K = 3 ; SubarrayHavingPerfectSquare ( arr , K ) ; } }
Check if any permutation of a number without any leading zeros is a power of 2 or not | C # program for the above approach ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i to n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagrams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Function to check if any permutation of a number is a power of 2 or not ; Iterate over all possible perfect power of 2 ; Print that number ; Driver Code ; Given number N ; Function call
using System ; class GFG { static int TEN = 10 ; static void updateFreq ( int n , int [ ] freq ) { while ( n > 0 ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } static bool areAnagrams ( int a , int b ) { int [ ] freqA = new int [ TEN ] ; int [ ] freqB = new int [ TEN ] ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } static bool isPowerOf2 ( int N ) { for ( int i = 0 ; i < 32 ; i ++ ) { if ( areAnagrams ( 1 << i , N ) ) { Console . Write ( ( 1 << i ) ) ; return true ; } } return false ; } public static void Main ( String [ ] args ) { int N = 46 ; if ( ! isPowerOf2 ( N ) ) { Console . Write ( " No " ) ; } } }
Count number of triangles possible with length of sides not exceeding N | C # implementation of the above approach ; Function to count total number of right angled triangle ; Initialise count with 0 ; Run three nested loops and check all combinations of sides ; Condition for right angled triangle ; Increment count ; Driver Code ; Given N ; Function call
using System ; class GFG { static int right_angled ( int n ) { int count = 0 ; for ( int z = 1 ; z <= n ; z ++ ) { for ( int y = 1 ; y <= z ; y ++ ) { for ( int x = 1 ; x <= y ; x ++ ) { if ( ( x * x ) + ( y * y ) == ( z * z ) ) { count ++ ; } } } } return count ; } public static void Main ( string [ ] args ) { int n = 5 ; Console . Write ( right_angled ( n ) ) ; } }
Check if the given two matrices are mirror images of one another | C # implementation of the above approach ; Function to check whether the two matrices are mirror of each other ; Initialising row and column of second matrix ; Iterating over the matrices ; Check row of first matrix with reversed row of second matrix ; If the element is not equal ; Increment column ; Reset column to 0 for new row ; Increment row ; Driver code ; Given 2 matrices ; Function Call
using System ; class GFG { static void mirrorMatrix ( int [ , ] mat1 , int [ , ] mat2 , int N ) { int row = 0 ; int col = 0 ; bool isMirrorImage = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( mat2 [ row , col ] != mat1 [ i , j ] ) { isMirrorImage = false ; } col ++ ; } col = 0 ; row ++ ; } if ( isMirrorImage ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( String [ ] args ) { int N = 4 ; int [ , ] mat1 = { { 1 , 2 , 3 , 4 } , { 0 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int [ , ] mat2 = { { 4 , 3 , 2 , 1 } , { 8 , 7 , 6 , 0 } , { 12 , 11 , 10 , 9 } , { 16 , 15 , 14 , 13 } } ; mirrorMatrix ( mat1 , mat2 , N ) ; } }
Calculate number of nodes between two vertices in an acyclic Graph by DFS method | C # program for the above approach ; Function to return the count of nodes in the path from source to destination ; Mark the node visited ; If dest is reached ; Traverse all adjacent nodes ; If not already visited ; If there is path , then include the current node ; Return 0 if there is no path between src and dest through the current node ; Function to return the count of nodes between two given vertices of the acyclic Graph ; Initialize an adjacency list ; Populate the edges in the list ; Mark all the nodes as not visited ; Count nodes in the path from src to dest ; Return the nodes between src and dest ; Driver Code ; Given number of vertices and edges ; Given source and destination vertices ; Given edges
using System ; using System . Collections . Generic ; class GFG { static int dfs ( int src , int dest , int [ ] vis , List < int > [ ] adj ) { vis [ src ] = 1 ; if ( src == dest ) { return 1 ; } foreach ( int u in adj [ src ] ) { if ( vis [ u ] == 0 ) { int temp = dfs ( u , dest , vis , adj ) ; if ( temp != 0 ) { return temp + 1 ; } } } return 0 ; } static int countNodes ( int V , int E , int src , int dest , int [ , ] edges ) { List < int > [ ] adj = new List < int > [ V + 1 ] ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < E ; i ++ ) { adj [ edges [ i , 0 ] ] . Add ( edges [ i , 1 ] ) ; adj [ edges [ i , 1 ] ] . Add ( edges [ i , 0 ] ) ; } int [ ] vis = new int [ V + 1 ] ; int count = dfs ( src , dest , vis , adj ) ; return count - 2 ; } public static void Main ( String [ ] args ) { int V = 8 , E = 7 ; int src = 5 , dest = 2 ; int [ , ] edges = { { 1 , 4 } , { 4 , 5 } , { 4 , 2 } , { 2 , 6 } , { 6 , 3 } , { 2 , 7 } , { 3 , 8 } } ; Console . Write ( countNodes ( V , E , src , dest , edges ) ) ; } }