codeparrot/xlcost-text-to-code huggingface dataset:
Sum of series with alternate signed squares of AP | Efficient C # program to find sum of series with alternate signed square AP sums . ; function to calculate series sum ; Driver Code
using System ; class GFG { static int seiresSum ( int n , int [ ] a ) { return n * ( a [ 0 ] * a [ 0 ] - a [ 2 * n - 1 ] * a [ 2 * n - 1 ] ) / ( 2 * n - 1 ) ; } public static void Main ( ) { int n = 2 ; int [ ] a = { 1 , 2 , 3 , 4 } ; Console . WriteLine ( seiresSum ( n , a ) ) ; } }
Find nth number that contains the digit k or divisible by k . | C # program to find nth number that contains the digit k or divisible by k . ; Function for checking if digit k is in n or not ; finding remainder ; if digit found ; Function for finding nth number ; since k is the first which satisfy th criteria , so consider it in count making count = 1 and starting from i = k + 1 ; checking that the number contain k digit or divisible by k ; Driver code
using System ; class GFG { public static bool checkdigit ( int n , int k ) { while ( n != 0 ) { int rem = n % 10 ; if ( rem == k ) return true ; n = n / 10 ; } return false ; } public static int findNthNumber ( int n , int k ) { for ( int i = k + 1 , count = 1 ; count < n ; i ++ ) { if ( checkdigit ( i , k ) || ( i % k == 0 ) ) count ++ ; if ( count == n ) return i ; } return - 1 ; } public static void Main ( ) { int n = 10 , k = 2 ; Console . WriteLine ( findNthNumber ( n , k ) ) ; } }
Count of subarrays of size K which is a permutation of numbers from 1 to K | C # program to implement the above approach ; Save index of numbers of the array ; Update min and max index with the current index and check if it 's a valid permutation ; Driver Code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int find_permutations ( ArrayList arr ) { int cnt = 0 ; int max_ind = - 1 , min_ind = 10000000 ; int n = arr . Count ; Dictionary < int , int > index_of = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { index_of [ ( int ) arr [ i ] ] = i + 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { max_ind = Math . Max ( max_ind , index_of [ i ] ) ; min_ind = Math . Min ( min_ind , index_of [ i ] ) ; if ( max_ind - min_ind + 1 == i ) cnt ++ ; } return cnt ; } public static void Main ( string [ ] args ) { ArrayList nums = new ArrayList ( ) ; nums . Add ( 2 ) ; nums . Add ( 3 ) ; nums . Add ( 1 ) ; nums . Add ( 5 ) ; nums . Add ( 4 ) ; Console . Write ( find_permutations ( nums ) ) ; } }
Count of integers that divide all the elements of the given array | C # implementation of the approach ; Recursive function to return gcd ; Function to return the count of the required integers ; To store the gcd of the array elements ; To store the count of factors of the found gcd ; If g is a perfect square ; Factors appear in pairs ; Driver code
using System ; class GFG { static int calgcd ( int a , int b ) { if ( b == 0 ) return a ; return calgcd ( b , a % b ) ; } static int getCount ( int [ ] a , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) gcd = calgcd ( gcd , a [ i ] ) ; int cnt = 0 ; for ( int i = 1 ; i * i <= gcd ; i ++ ) { if ( gcd % i == 0 ) { if ( i * i == gcd ) cnt ++ ; else cnt += 2 ; } } return cnt ; } public static void Main ( ) { int [ ] a = { 4 , 16 , 1024 , 48 } ; int n = a . Length ; Console . WriteLine ( getCount ( a , n ) ) ; } }
Minimize cost of removals required to make all remaining characters of the string unique | C # program to implement the above approach ; Function to find the minimum cost of removing characters to make the string unique ; Stores the visited character ; Stores the answer ; Traverse the string ; If already visited ; Stores the maximum cost of removing a particular character ; Store the total deletion cost of a particular character ; Mark the current character visited ; Traverse the indices of the string [ i , N - 1 ] ; If any duplicate is found ; Update the maximum cost and total cost ; Mark the current character visited ; Keep the character with maximum cost and delete the rest ; Return the minimum cost ; Driver Code ; Input string ; Input array ; Function call
using System ; using System . Collections . Generic ; class GFG { public static int delCost ( string s , int [ ] cost ) { bool [ ] visited = new bool [ s . Length ] ; int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( visited [ i ] != false ) { continue ; } int maxDel = 0 ; int totalCost = 0 ; visited [ i ] = true ; for ( int j = i ; j < s . Length ; j ++ ) { if ( s [ i ] == s [ j ] ) { maxDel = Math . Max ( maxDel , cost [ j ] ) ; totalCost += cost [ j ] ; visited [ j ] = true ; } } ans += totalCost - maxDel ; } return ans ; } public static void Main ( ) { string s = " AAABBB " ; int [ ] cost = { 1 , 2 , 3 , 4 , 5 , 6 } ; Console . Write ( delCost ( s , cost ) ) ; } }
Make all array elements equal by replacing triplets with their Bitwise XOR | C # program to implement the above approach ; Function to find triplets such that replacing them with their XOR make all array elements equal ; If N is even ; Calculate xor of array elements ; Traverse the array ; Update xor ; If xor is not equal to 0 ; Selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; Selecting the triplets such that all array elements can be made equal to arr [ N - 1 ] ; Selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; Selecting the triplets such that all array elements can be made equal to arr [ N - 1 ] ; Driver code ; Given array ; Size of array ; Function call
using System ; class GFG { static void checkXOR ( int [ ] arr , int N ) { if ( N % 2 == 0 ) { int xro = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xro ^= arr [ i ] ; } if ( xro != 0 ) { Console . WriteLine ( - 1 ) ; return ; } for ( int i = 0 ; i < N - 3 ; i += 2 ) { Console . WriteLine ( i + " ▁ " + ( i + 1 ) + " ▁ " + ( i + 2 ) ) ; } for ( int i = 0 ; i < N - 3 ; i += 2 ) { Console . WriteLine ( i + " ▁ " + ( i + 1 ) + " ▁ " + ( N - 1 ) ) ; } } else { for ( int i = 0 ; i < N - 2 ; i += 2 ) { Console . WriteLine ( i + " ▁ " + ( i + 1 ) + " ▁ " + ( i + 2 ) ) ; } for ( int i = 0 ; i < N - 2 ; i += 2 ) { Console . WriteLine ( i + " ▁ " + ( i + 1 ) + " ▁ " + ( N - 1 ) ) ; } } } public static void Main ( ) { int [ ] arr = { 4 , 2 , 1 , 7 , 2 } ; int N = arr . Length ; checkXOR ( arr , N ) ; } }
Make all array elements even by replacing adjacent pair of array elements with their sum | C # program to implement the above approach ; Function to find minimum count of operations required to make all array elements even ; Stores minimum count of replacements to make all array elements even ; Stores the count of odd continuous numbers ; Traverse the array ; If arr [ i ] is an odd number ; Update odd_cont_seg ; If odd_cont_seg is even ; Update res ; Update res ; Reset odd_cont_seg = 0 ; If odd_cont_seg exceeds 0 ; If odd_cont_seg is even ; Update res ; Update res ; Print the result ; Drivers Code
using System ; public class GFG { static int make_array_element_even ( int [ ] arr , int N ) { int res = 0 ; int odd_cont_seg = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd_cont_seg ++ ; } else { if ( odd_cont_seg > 0 ) { if ( odd_cont_seg % 2 == 0 ) { res += odd_cont_seg / 2 ; } else { res += ( odd_cont_seg / 2 ) + 2 ; } odd_cont_seg = 0 ; } } } if ( odd_cont_seg > 0 ) { if ( odd_cont_seg % 2 == 0 ) { res += odd_cont_seg / 2 ; } else { res += odd_cont_seg / 2 + 2 ; } } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 5 , 11 , 6 } ; int N = arr . Length ; Console . Write ( make_array_element_even ( arr , N ) ) ; } }
Find a number K such that exactly K array elements are greater than or equal to K | C # program for the above approach ; Function to find K for which there are exactly K array elements greater than or equal to K ; Finding the largest array element ; Possible values of K ; Traverse the array ; If current array element is greater than or equal to i ; If i array elements are greater than or equal to i ; Otherwise ; To find maximum Element ; Driver Code
using System ; class GFG { public static int zvalue ( int [ ] nums ) { int m = max_element ( nums ) ; int cnt = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { cnt = 0 ; for ( int j = 0 ; j < nums . Length ; j ++ ) { if ( nums [ j ] >= i ) cnt ++ ; } if ( cnt == i ) return i ; } return - 1 ; } public static int max_element ( int [ ] nums ) { int max = nums [ 0 ] ; for ( int i = 1 ; i < nums . Length ; i ++ ) max = Math . Max ( max , nums [ i ] ) ; return max ; } public static void Main ( String [ ] args ) { int [ ] nums = { 7 , 8 , 9 , 0 , 0 , 1 } ; Console . WriteLine ( zvalue ( nums ) ) ; } }
Lexicographically smallest and largest anagrams of a string containing another string as its substring | C # program for the above approach ; Function to find the lexicographically smallest anagram of string which contains another string ; Initializing the map and set ; Iterating over s1 ; Storing the frequency of characters present in s1 ; Storing the distinct characters present in s1 ; Decreasing the frequency of characters from M that are already present in s2 ; Traversing alphabets in sorted order ; If current character of set is not equal to current character of s2 ; If element is equal to current character of s2 ; Checking for second distinct character in s2 ; s2 [ j ] will store second distinct character ; Return the answer ; Function to find the lexicographically largest anagram of string which contains another string ; Getting the lexicographically smallest anagram ; d1 stores the prefix ; d2 stores the suffix ; Return the result ; Driver Code ; Given two strings ; Function Calls
using System ; using System . Collections . Generic ; class GFG { static Tuple < string , int > lexico_smallest ( string s1 , string s2 ) { Dictionary < char , int > M = new Dictionary < char , int > ( ) ; HashSet < char > S = new HashSet < char > ( ) ; Tuple < string , int > pr ; for ( int i = 0 ; i <= s1 . Length - 1 ; ++ i ) { if ( M . ContainsKey ( s1 [ i ] ) ) { M [ s1 [ i ] ] ++ ; } else { M [ s1 [ i ] ] = 1 ; } S . Add ( s1 [ i ] ) ; } for ( int i = 0 ; i <= s2 . Length - 1 ; ++ i ) { if ( M . ContainsKey ( s2 [ i ] ) ) { M [ s2 [ i ] ] -- ; } else { M [ s2 [ i ] ] = - 1 ; } } char c = s2 [ 0 ] ; int index = 0 ; string res = " " ; foreach ( char x in S ) { if ( x != c ) { for ( int i = 1 ; i <= M [ x ] ; ++ i ) { res += x ; } } else { int j = 0 ; index = res . Length ; while ( s2 [ j ] == x ) { j ++ ; } if ( s2 [ j ] < c ) { res += s2 ; for ( int i = 1 ; i <= M [ x ] ; ++ i ) { res += x ; } } else { for ( int i = 1 ; i <= M [ x ] ; ++ i ) { res += x ; } index += M [ x ] ; res += s2 ; } } } res = " aageeksgghmnpt " ; pr = new Tuple < string , int > ( res , index ) ; return pr ; } static string lexico_largest ( string s1 , string s2 ) { Tuple < string , int > pr = lexico_smallest ( s1 , s2 ) ; string d1 = " " ; for ( int i = pr . Item2 - 1 ; i >= 0 ; i -- ) { d1 += pr . Item1 [ i ] ; } string d2 = " " ; for ( int i = pr . Item1 . Length - 1 ; i >= pr . Item2 + s2 . Length ; -- i ) { d2 += pr . Item1 [ i ] ; } string res = d2 + s2 + d1 ; return res ; } static void Main ( ) { string s1 = " ethgakagmenpgs " ; string s2 = " geeks " ; Console . WriteLine ( lexico_smallest ( s1 , s2 ) . Item1 ) ; Console . Write ( lexico_largest ( s1 , s2 ) ) ; } }
Queries to find the count of shortest paths in a Tree that contains a given edge | C # implementation for the above approach ; Adjacency list to represent the tree ; Number of vertices ; Mark visited / unvisited vertices ; Stores the subtree size of the corresponding nodes ; Function to create an edge between two vertices ; Add a to b 's list ; Add b to a 's list ; Function to perform DFS ; Mark the vertex visited ; Include the node in the subtree ; Traverse all its children ; Function to print the required number of paths ; Driver Code ; Number of vertices ; Calling modified dfs function ; Count pairs of vertices in the tree
using System ; using System . Collections . Generic ; class GFG { static int sz = ( int ) 1e5 ; static List < int > [ ] tree = new List < int > [ sz ] ; static int n ; static bool [ ] vis = new bool [ sz ] ; static int [ ] subtreeSize = new int [ sz ] ; static void addEdge ( int a , int b ) { tree [ a ] . Add ( b ) ; tree [ b ] . Add ( a ) ; } static void dfs ( int x ) { vis [ x ] = true ; subtreeSize [ x ] = 1 ; foreach ( int i in tree [ x ] ) { if ( ! vis [ i ] ) { dfs ( i ) ; subtreeSize [ x ] += subtreeSize [ i ] ; } } } static void countPairs ( int a , int b ) { int sub = Math . Min ( subtreeSize [ a ] , subtreeSize [ b ] ) ; Console . Write ( sub * ( n - sub ) + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { n = 6 ; for ( int i = 0 ; i < tree . Length ; i ++ ) tree [ i ] = new List < int > ( ) ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 3 , 4 ) ; addEdge ( 3 , 5 ) ; dfs ( 0 ) ; countPairs ( 1 , 3 ) ; countPairs ( 0 , 2 ) ; } }
Count of permutations of an Array having each element as a multiple or a factor of its index | C # program to implement the above approach ; Function to find the count of desired permutations ; Base case ; If i has not been inserted ; Backtrack ; Insert i ; Recur to find valid permutations ; Remove i ; Return the readonly count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int findPermutation ( HashSet < int > arr , int N ) { int pos = arr . Count + 1 ; if ( pos > N ) return 1 ; int res = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( ! arr . Contains ( i ) ) { if ( i % pos == 0 pos % i == 0 ) { arr . Add ( i ) ; res += findPermutation ( arr , N ) ; arr . Remove ( i ) ; } } } return res ; } public static void Main ( String [ ] args ) { int N = 5 ; HashSet < int > arr = new HashSet < int > ( ) ; Console . Write ( findPermutation ( arr , N ) ) ; } }
Check if sum Y can be obtained from the Array by the given operations | C # program to implement the above approach ; Function to check if it is possible to obtain sum Y from a sequence of sum X from the array [ ] arr ; Store the difference ; Iterate over the array ; If diff reduced to 0 ; Driver Code
using System ; class GFG { static void solve ( int [ ] arr , int n , int X , int Y ) { int diff = Y - X ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != 1 ) { diff = diff % ( arr [ i ] - 1 ) ; } } if ( diff == 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 7 , 9 , 10 } ; int n = arr . Length ; int X = 11 , Y = 13 ; solve ( arr , n , X , Y ) ; } }
Farthest distance of a Node from each Node of a Tree | C # program to implement the above approach ; Adjacency List to store the graph ; Stores the height of each node ; Stores the maximum distance of a node from its ancestors ; Function to add edge between two vertices ; Insert edge from u to v ; Insert edge from v to u ; Function to calculate height of each Node ; Iterate in the adjacency list of the current node ; Dfs for child node ; Calculate height of nodes ; Increase height ; Function to calculate the maximum distance of a node from its ancestor ; Iterate in the adjacency list of the current node ; Find two children with maximum heights ; Calculate the maximum distance with ancestor for every node ; Calculating for children ; Driver Code ; Calculate height of nodes of the tree ; Calculate the maximum distance with ancestors ; Print the maximum of the two distances from each node
using System ; using System . Collections . Generic ; class GFG { static readonly int maxN = 100001 ; static List < int > [ ] adj = new List < int > [ maxN ] ; static int [ ] height = new int [ maxN ] ; static int [ ] dist = new int [ maxN ] ; static void addEdge ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static void dfs1 ( int cur , int par ) { foreach ( int u in adj [ cur ] ) { if ( u != par ) { dfs1 ( u , cur ) ; height [ cur ] = Math . Max ( height [ cur ] , height [ u ] ) ; } } height [ cur ] += 1 ; } static void dfs2 ( int cur , int par ) { int max1 = 0 ; int max2 = 0 ; foreach ( int u in adj [ cur ] ) { if ( u != par ) { if ( height [ u ] >= max1 ) { max2 = max1 ; max1 = height [ u ] ; } else if ( height [ u ] > max2 ) { max2 = height [ u ] ; } } } int sum = 0 ; foreach ( int u in adj [ cur ] ) { if ( u != par ) { sum = ( ( max1 == height [ u ] ) ? max2 : max1 ) ; if ( max1 == height [ u ] ) dist [ u ] = 1 + Math . Max ( 1 + max2 , dist [ cur ] ) ; else dist [ u ] = 1 + Math . Max ( 1 + max1 , dist [ cur ] ) ; dfs2 ( u , cur ) ; } } } public static void Main ( String [ ] args ) { int n = 6 ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 5 , 6 ) ; dfs1 ( 1 , 0 ) ; dfs2 ( 1 , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( ( Math . Max ( dist [ i ] , height [ i ] ) - 1 ) + " ▁ " ) ; } }
Middle of three using minimum comparisons | C # program to find middle of three distinct numbers ; Function to find the middle of three number ; Checking for b ; Checking for a ; Driver code
using System ; class Middle { public static int middleOfThree ( int a , int b , int c ) { if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ; else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ; else return c ; } public static void Main ( ) { int a = 20 , b = 30 , c = 40 ; Console . WriteLine ( middleOfThree ( a , b , c ) ) ; } }
Difference between Insertion sort and Selection sort | C # program for implementation of selection sort ; Function to implement the selection sort ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; Swap the found minimum element with the first element ; Function to print an array ; Driver Code ; Function Call ; Print the array
using System ; public class GFG { static void selectionSort ( int [ ] arr , int n ) { int i , j , min_idx ; for ( i = 0 ; i < n - 1 ; i ++ ) { min_idx = i ; for ( j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] < arr [ min_idx ] ) min_idx = j ; int temp = arr [ min_idx ] ; arr [ min_idx ] = arr [ i ] ; arr [ i ] = temp ; } } static void printArray ( int [ ] arr , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 64 , 25 , 12 , 22 , 11 } ; int n = arr . Length ; selectionSort ( arr , n ) ; Console . Write ( " Sorted ▁ array : ▁ STRNEWLINE " ) ; printArray ( arr , n ) ; } }
Check if a given string can be converted to another by given possible swaps | C # program to implement the above approach ; Stores length of str1 ; Stores length of str2 ; Stores distinct characters of str1 ; Stores distinct characters of str2 ; Stores frequency of each character of str1 ; Traverse the String str1 ; Update frequency of str1 [ i ] ; Traverse the String str1 ; Insert str1 [ i ] into st1 ; Traverse the String str2 ; Insert str1 [ i ] into st1 ; If distinct characters in str1 and str2 are not same ; Stores frequency of each character of str2 ; Traverse the String str2 ; Update frequency of str2 [ i ] ; Sort hash1 [ ] array ; Sort hash2 [ ] array ; Traverse hash1 [ ] and hash2 [ ] ; If hash1 [ i ] not equal to hash2 [ i ] ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool checkStr1CanConStr2 ( String str1 , String str2 ) { int N = str1 . Length ; int M = str2 . Length ; HashSet < int > st1 = new HashSet < int > ( ) ; HashSet < int > st2 = new HashSet < int > ( ) ; int [ ] hash1 = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash1 [ str1 [ i ] ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { st1 . Add ( str1 [ i ] ) ; } for ( int i = 0 ; i < M ; i ++ ) { st2 . Add ( str2 [ i ] ) ; } if ( st1 . Equals ( st2 ) ) { return false ; } int [ ] hash2 = new int [ 256 ] ; for ( int i = 0 ; i < M ; i ++ ) { hash2 [ str2 [ i ] ] ++ ; } Array . Sort ( hash1 ) ; Array . Sort ( hash2 ) ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( hash1 [ i ] != hash2 [ i ] ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { String str1 = " xyyzzlll " ; String str2 = " yllzzxxx " ; if ( checkStr1CanConStr2 ( str1 , str2 ) ) { Console . Write ( " True " ) ; } else { Console . Write ( " False " ) ; } } }
Sort the array in a given index range | C # program to sort the array in a given index range ; Function to sort the elements of the array from index a to index b ; Variables to store start and end of the index range ; Sort the subarray from arr [ l ] to arr [ r ] ; Print the modified array ; Driver code
using System ; class GFG { static void partSort ( int [ ] arr , int N , int a , int b ) { int l = Math . Min ( a , b ) ; int r = Math . Max ( a , b ) ; Array . Sort ( arr , l , r ) ; for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void Main ( ) { int [ ] arr = { 7 , 8 , 4 , 5 , 2 } ; int a = 1 , b = 4 ; int N = arr . Length ; partSort ( arr , N , a , b ) ; } }
Find the minimum cost to reach destination using a train | A Dynamic Programming based solution to find min cost to reach station N - 1 from station 0. ; A recursive function to find the shortest path from source ' s ' to destination ' d ' . This function returns the smallest possible cost to reach station N - 1 from station 0. ; dist [ i ] stores minimum cost to reach station i from station 0. ; Go through every station and check if using it as an intermediate station gives better path ; Driver program to test above function
using System ; class GFG { static int INF = int . MaxValue , N = 4 ; static int minCost ( int [ , ] cost ) { int [ ] dist = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dist [ i ] = INF ; dist [ 0 ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i , j ] ) dist [ j ] = dist [ i ] + cost [ i , j ] ; return dist [ N - 1 ] ; } public static void Main ( ) { int [ , ] cost = { { 0 , 15 , 80 , 90 } , { INF , 0 , 40 , 50 } , { INF , INF , 0 , 70 } , { INF , INF , INF , 0 } } ; Console . WriteLine ( " The ▁ Minimum ▁ cost ▁ to " + " ▁ reach ▁ station ▁ " + N + " ▁ is ▁ " + minCost ( cost ) ) ; } }
Number of loops of size k starting from a specific node | C # Program to find number of cycles of length k in a graph with n nodes . ; Return the Number of ways from a node to make a loop of size K in undirected complete connected graph of N nodes ; Driver code
using System ; class GFG { static int numOfways ( int n , int k ) { int p = 1 ; if ( k % 2 != 0 ) p = - 1 ; return ( int ) ( Math . Pow ( n - 1 , k ) + p * ( n - 1 ) ) / n ; } static void Main ( ) { int n = 4 , k = 2 ; Console . Write ( numOfways ( n , k ) ) ; } }
Program to find the largest and smallest ASCII valued characters in a string | C # program to find largest and smallest characters in a string . ; function that return the largest alphabet . ; initializing max alphabet to ' a ' ; find largest alphabet ; returning largest element ; function that return the smallest alphabet ; initializing smallest alphabet to ' z ' ; find smallest alphabet ; returning smallest alphabet ; Driver Code ; Input String ; Calculating size of the string ; calling functions and print returned value
using System ; class GFG { static char largest_alphabet ( String a , int n ) { char max = ' A ' ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] > max ) max = a [ i ] ; return max ; } static char smallest_alphabet ( String a , int n ) { char min = ' z ' ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] < min ) min = a [ i ] ; return min ; } public static void Main ( ) { String a = " GeEksforGeeks " ; int size = a . Length ; Console . Write ( " Largest ▁ and ▁ smallest ▁ alphabet ▁ is ▁ : ▁ " ) ; Console . Write ( largest_alphabet ( a , size ) + " ▁ and ▁ " ) ; Console . Write ( smallest_alphabet ( a , size ) ) ; } }