codeparrot/xlcost-text-to-code huggingface dataset:
Find the largest number smaller than integer N with maximum number of set bits | C # implementation to Find the largest number smaller than integer N with maximum number of set bits ; Function to return the largest number less than N ; Iterate through all possible values ; Multiply the number by 2 i times ; Return the final result ; Driver code
using System ; class GFG { static int largestNum ( int n ) { int num = 0 ; for ( int i = 0 ; i <= 32 ; i ++ ) { int x = ( 1 << i ) ; if ( ( x - 1 ) <= n ) num = ( 1 << i ) - 1 ; else break ; } return num ; } public static void Main ( ) { int N = 345 ; Console . Write ( largestNum ( N ) ) ; } }
Find the String having each substring with exactly K distinct characters | C # program to find the string having each substring with exactly K distinct characters ; Function to find the required output string ; Each element at index i is modulus of K ; Driver code ; Initialise integers N and K
using System ; class GFG { static void findString ( int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( ( char ) ( ' A ' + i % K ) ) ; } } public static void Main ( String [ ] args ) { int N = 10 ; int K = 3 ; findString ( N , K ) ; } }
Find total no of collisions taking place between the balls in which initial direction of each ball is given | C # implementation to Find total no of collisions taking place between the balls in which initial direction of each ball is given ; Function to count no of collision ; length of the string ; Driver code
using System ; class GFG { static int count ( String s ) { int N , i , cnt = 0 , ans = 0 ; N = s . Length ; for ( i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' R ' ) cnt ++ ; if ( s [ i ] == ' L ' ) ans += cnt ; } return ans ; } public static void Main ( String [ ] args ) { String s = " RRLL " ; Console . Write ( count ( s ) ) ; } }
Maximum number on 7 | C # implementation to find the maximum number that can be using the N segments in N segments display ; Function to find the maximum number that can be displayed using the N segments ; Condition to check base case ; Condition to check if the number is even ; Condition to check if the number is odd ; Driver Code
using System ; class GFG { static void segments ( int n ) { if ( n == 1 n == 0 ) { return ; } if ( n % 2 == 0 ) { Console . Write ( "1" ) ; segments ( n - 2 ) ; } else if ( n % 2 == 1 ) { Console . Write ( "7" ) ; segments ( n - 3 ) ; } } public static void Main ( ) { int n ; n = 11 ; segments ( n ) ; } }
Minimum number of subsequences required to convert one string to another using Greedy Algorithm | C # implementation for minimum number of subsequences required to convert one String to another ; Function to find the minimum number of subsequences required to convert one String to another S2 == A and S1 == B ; At least 1 subsequence is required Even in best case , when A is same as B ; size of B ; size of A ; Create an 2D array next [ , ] of size 26 * sizeOfB to store the next occurrence of a character ( ' a ' to ' z ' ) as an index [ 0 , sizeOfA - 1 ] ; Array Initialization with infinite ; Loop to Store the values of index ; If the value of next [ i , j ] is infinite then update it with next [ i , j + 1 ] ; Greedy algorithm to obtain the maximum possible subsequence of B to cover the remaining String of A using next subsequence ; Loop to iterate over the String A ; Condition to check if the character is not present in the String B ; Condition to check if there is an element in B matching with character A [ i ] on or next to B [ pos ] given by next [ A [ i ] - ' a ' , pos ] ; Condition to check if reached at the end of B or no such element exists on or next to A [ pos ] , thus increment number by one and reinitialise pos to zero ; Driver Code
using System ; class GFG { static int findMinimumSubsequences ( String A , String B ) { int numberOfSubsequences = 1 ; int sizeOfB = B . Length ; int sizeOfA = A . Length ; int inf = 1000000 ; int [ , ] next = new int [ 26 , sizeOfB ] ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < sizeOfB ; j ++ ) { next [ i , j ] = inf ; } } for ( int i = 0 ; i < sizeOfB ; i ++ ) { next [ B [ i ] - ' a ' , i ] = i ; } for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = sizeOfB - 2 ; j >= 0 ; j -- ) { if ( next [ i , j ] == inf ) { next [ i , j ] = next [ i , j + 1 ] ; } } } int pos = 0 ; int I = 0 ; while ( I < sizeOfA ) { if ( pos == 0 && next [ A [ I ] - ' a ' , pos ] == inf ) { numberOfSubsequences = - 1 ; break ; } else if ( pos < sizeOfB && next [ A [ I ] - ' a ' , pos ] < inf ) { int nextIndex = next [ A [ I ] - ' a ' , pos ] + 1 ; pos = nextIndex ; I ++ ; } else { numberOfSubsequences ++ ; pos = 0 ; } } return numberOfSubsequences ; } public static void Main ( String [ ] args ) { String A = " aacbe " ; String B = " aceab " ; Console . Write ( findMinimumSubsequences ( A , B ) ) ; } }
Vertical and Horizontal retrieval ( MRT ) on Tapes | C # program to print Horizontal filling ; It is used for checking whether tape is full or not ; It is used for calculating total retrieval time ; It is used for calculating mean retrieval time ; vector is used because n number of records can insert in one tape with size constraint ; Null vector obtained to use fresh vector ' v ' ; initialize variables to 0 for each iteration ; sum is used for checking whether i 'th tape is full or not ; check sum less than size of tape ; increment in j for next record ; calculating total retrieval time ; MRT formula ; calculating mean retrieval time using formula ; v . Count is function of vector is used to get size of vector ; Driver Code ; store the size of records [ ] ; store the size of tapes [ ] ; sorting of an array is required to attain greedy approach of algorithm
using System ; using System . Collections . Generic ; class GFG { static void horizontalFill ( int [ ] records , int [ ] tape , int nt ) { int sum = 0 ; int Retrieval_Time = 0 ; double Mrt ; int current = 0 ; List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < nt ; i ++ ) { v . Clear ( ) ; Retrieval_Time = 0 ; sum = 0 ; Console . Write ( " tape ▁ " + ( i + 1 ) + " ▁ : ▁ [ ▁ " ) ; sum += records [ current ] ; while ( sum <= tape [ i ] ) { Console . Write ( records [ current ] + " ▁ " ) ; v . Add ( records [ current ] ) ; current ++ ; sum += records [ current ] ; } Console . Write ( " ] " ) ; for ( int j = 0 ; j < v . Count ; j ++ ) { Retrieval_Time += v [ j ] * ( v . Count - j ) ; } Mrt = ( double ) Retrieval_Time / v . Count ; Console . Write ( " TABSYMBOL MRT ▁ : ▁ " + Mrt + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int [ ] records = { 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 } ; int [ ] tape = { 25 , 80 , 160 } ; int n = records . Length ; int m = tape . Length ; Array . Sort ( records ) ; horizontalFill ( records , tape , m ) ; } }
Circular Convolution using Matrix Method | C # program to compute circular convolution of two arrays ; Function to find circular convolution ; Finding the maximum size between the two input sequence sizes ; Copying elements of x to row_vec and padding zeros if size of x < maxSize ; Copying elements of h to col_vec and padding zeros if size of h is less than maxSize ; Generating 2D matrix of circularly shifted elements ; Computing result by matrix multiplication and printing results ; Driver program
using System ; class GFG { readonly static int MAX_SIZE = 10 ; static void convolution ( int [ ] x , int [ ] h , int n , int m ) { int [ ] row_vec = new int [ MAX_SIZE ] ; int [ ] col_vec = new int [ MAX_SIZE ] ; int [ ] out_ = new int [ MAX_SIZE ] ; int [ , ] circular_shift_mat = new int [ MAX_SIZE , MAX_SIZE ] ; int maxSize = n > m ? n : m ; for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= n ) { row_vec [ i ] = 0 ; } else { row_vec [ i ] = x [ i ] ; } } for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= m ) { col_vec [ i ] = 0 ; } else { col_vec [ i ] = h [ i ] ; } } int k = 0 , d = 0 ; for ( int i = 0 ; i < maxSize ; i ++ ) { int curIndex = k - d ; for ( int j = 0 ; j < maxSize ; j ++ ) { circular_shift_mat [ j , i ] = row_vec [ curIndex % maxSize ] ; curIndex ++ ; } k = maxSize ; d ++ ; } for ( int i = 0 ; i < maxSize ; i ++ ) { for ( int j = 0 ; j < maxSize ; j ++ ) { out_ [ i ] += circular_shift_mat [ i , j ] * col_vec [ j ] ; } Console . Write ( out_ [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] x = { 5 , 7 , 3 , 2 } ; int n = x . Length ; int [ ] h = { 1 , 5 } ; int m = h . Length ; convolution ( x , h , n , m ) ; } }
Maximize the number of palindromic Strings | C # program for the above approach ; To check if there is any string of odd length ; If there is at least 1 string of odd length . ; If all the strings are of even length . ; Count of 0 's in all  the strings ; Count of 1 's in  all the strings ; If z is even and o is even then ans will be N . ; Otherwise ans will be N - 1. ; Driver code
using System ; class GFG { static int max_palindrome ( string [ ] s , int n ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] . Length % 2 != 0 ) { flag = 1 ; } } if ( flag == 1 ) { return n ; } int z = 0 ; int o = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < s [ i ] . Length ; j ++ ) { if ( s [ i ] [ j ] == '0' ) z += 1 ; else o += 1 ; } } if ( o % 2 == 0 && z % 2 == 0 ) { return n ; } else { return n - 1 ; } } public static void Main ( ) { int n = 3 ; string [ ] s = { "1110" , "100110" , "010101" } ; Console . WriteLine ( max_palindrome ( s , n ) ) ; } }
Minimum possible travel cost among N cities | C # implementation of the approach ; Function to return the minimum cost to travel from the first city to the last ; To store the total cost ; Start from the first city ; If found any city with cost less than that of the previous boarded bus then change the bus ; Calculate the cost to travel from the currently boarded bus till the current city ; Update the currently boarded bus ; Finally calculate the cost for the last boarding bus till the ( N + 1 ) th city ; Driver code
using System ; class GFG { static int minCost ( int [ ] cost , int n ) { int totalCost = 0 ; int boardingBus = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( cost [ boardingBus ] > cost [ i ] ) { totalCost += ( ( i - boardingBus ) * cost [ boardingBus ] ) ; boardingBus = i ; } } totalCost += ( ( n - boardingBus ) * cost [ boardingBus ] ) ; return totalCost ; } public static void Main ( String [ ] args ) { int [ ] cost = { 4 , 7 , 8 , 3 , 4 } ; int n = cost . Length ; Console . Write ( minCost ( cost , n ) ) ; } }
Minimum cells to be flipped to get a 2 * 2 submatrix with equal elements | C # implementation of the approach ; Function to return the minimum flips required such that the submatrix from mat [ i , j ] to mat [ i + 1 , j + 1 ] contains all equal elements ; Function to return the minimum number of slips required such that the matrix contains at least a single submatrix of size 2 * 2 with all equal elements ; To store the result ; For every submatrix of size 2 * 2 ; Update the count of flips required for the current submatrix ; Driver code
using System ; class GFG { static int minFlipsSub ( String [ ] mat , int i , int j ) { int cnt0 = 0 , cnt1 = 0 ; if ( mat [ i ] [ j ] == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i ] [ j + 1 ] == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] [ j ] == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] [ j + 1 ] == '1' ) cnt1 ++ ; else cnt0 ++ ; return Math . Min ( cnt0 , cnt1 ) ; } static int minFlips ( String [ ] mat , int r , int c ) { int res = int . MaxValue ; for ( int i = 0 ; i < r - 1 ; i ++ ) { for ( int j = 0 ; j < c - 1 ; j ++ ) { res = Math . Min ( res , minFlipsSub ( mat , i , j ) ) ; } } return res ; } public static void Main ( String [ ] args ) { String [ ] mat = { "0101" , "0101" , "0101" } ; int r = mat . Length ; int c = mat . GetLength ( 0 ) ; Console . Write ( minFlips ( mat , r , c ) ) ; } }
Count set bits in the Kth number after segregating even and odd from N natural numbers | C # implementation of the above approach ; Function to return the kth element of the Odd - Even sequence of length n ; Finding the index from where the even numbers will be stored ; Return the kth element ; Function to return the count of set bits in the kth number of the odd even sequence of length n ; Required kth number ; Return the count of set bits ; Driver code
using System ; class GFG { static int findK ( int n , int k ) { int pos ; if ( n % 2 == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } static int countSetBits ( int n , int k ) { int kth = findK ( n , k ) ; int count = 0 ; while ( kth > 0 ) { count += kth & 1 ; kth >>= 1 ; } return count ; } public static void Main ( String [ ] args ) { int n = 18 , k = 12 ; Console . WriteLine ( countSetBits ( n , k ) ) ; } }
Minimum cost to convert str1 to str2 with the given operations | C # implementation of the approach ; Function to return the minimum cost to convert str1 to sr2 ; For every character of str1 ; If current character is not equal in both the strings ; If the next character is also different in both the strings then these characters can be swapped ; Change the current character ; Driver code
using System ; class GFG { static int minCost ( string str1 , string str2 , int n ) { int cost = 0 ; char [ ] array = str1 . ToCharArray ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str1 [ i ] != str2 [ i ] ) { if ( i < n - 1 && str1 [ i + 1 ] != str2 [ i + 1 ] ) { char temp = array [ i ] ; array [ i ] = array [ i + 1 ] ; array [ i + 1 ] = temp ; cost ++ ; } else { cost ++ ; } } } return cost ; } static public void Main ( ) { string str1 = " abb " , str2 = " bba " ; int n = str1 . Length ; Console . WriteLine ( minCost ( str1 , str2 , n ) ) ; } }
Partition first N natural number into two sets such that their sum is not coprime | C # implementation of the approach ; Function to find the required sets ; Impossible case ; Sum of first n - 1 natural numbers ; Driver code
using System ; class GFG { static void find_set ( int n ) { if ( n <= 2 ) { Console . WriteLine ( " - 1" ) ; return ; } int sum1 = ( n * ( n - 1 ) ) / 2 ; int sum2 = n ; Console . WriteLine ( sum1 + " ▁ " + sum2 ) ; } public static void Main ( ) { int n = 8 ; find_set ( n ) ; } }
Count common elements in two arrays containing multiples of N and M | C # implementation of the above approach ; Recursive function to find gcd using euclidean algorithm ; Function to find lcm of two numbers using gcd ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int n , int m ) { return ( n * m ) / gcd ( n , m ) ; } public static void Main ( String [ ] args ) { int n = 2 , m = 3 , k = 5 ; Console . WriteLine ( k / lcm ( n , m ) ) ; } }
Check whether a subsequence exists with sum equal to k if arr [ i ] > 2 * arr [ i | C # implementation of above approach ; Function to check whether sum of any set of the array element is equal to k or not ; Traverse the array from end to start ; if k is greater than arr [ i ] then subtract it from k ; If there is any subsequence whose sum is equal to k ; Driver code
using System ; class GFG { static bool CheckForSequence ( int [ ] arr , int n , int k ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( k >= arr [ i ] ) k -= arr [ i ] ; } if ( k != 0 ) return false ; else return true ; } public static void Main ( ) { int [ ] A = { 1 , 3 , 7 , 15 , 31 } ; int n = A . Length ; Console . WriteLine ( CheckForSequence ( A , n , 18 ) ? " True " : " False " ) ; } }
Maximum possible sub | C # implementation of the approach ; Function to return the maximum sub - array sum after at most x swaps ; To store the required answer ; For all possible intervals ; Keep current ans as zero ; To store the integers which are not part of the sub - array currently under consideration ; To store elements which are part of the sub - array currently under consideration ; Create two sets ; Swap at most X elements ; Remove the minimum of the taken elements ; Add maximum of the discarded elements ; Update the answer ; Return the maximized sub - array sum ; Driver code .
using System ; using System . Collections . Generic ; class GFG { static int SubarraySum ( int [ ] a , int n , int x ) { int ans = - 10000 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int curans = 0 ; List < int > pq = new List < int > ( ) ; List < int > pq2 = new List < int > ( ) ; for ( int k = 0 ; k < n ; k ++ ) { if ( k >= i && k <= j ) { curans += a [ k ] ; pq2 . Add ( a [ k ] ) ; } else pq . Add ( a [ k ] ) ; } pq . Sort ( ) ; pq . Reverse ( ) ; pq2 . Sort ( ) ; ans = Math . Max ( ans , curans ) ; for ( int k = 1 ; k <= x ; k ++ ) { if ( pq . Count == 0 pq2 . Count == 0 pq2 [ 0 ] >= pq [ 0 ] ) break ; curans -= pq2 [ 0 ] ; pq2 . RemoveAt ( 0 ) ; curans += pq [ 0 ] ; pq . RemoveAt ( 0 ) ; ans = Math . Max ( ans , curans ) ; } } } return ans ; } static void Main ( ) { int [ ] a = { 5 , - 1 , 2 , 3 , 4 , - 2 , 5 } ; int x = 2 ; int n = a . Length ; Console . WriteLine ( SubarraySum ( a , n , x ) ) ; } }
Generate an array of size K which satisfies the given conditions | C # implementation of the approach ; Function to generate and print the required array ; Initializing the array ; Finding r ( from above approach ) ; If r < 0 ; Finding ceiling and floor values ; Fill the array with ceiling values ; Fill the array with floor values ; Add 1 , 2 , 3 , ... with corresponding values ; There is no solution for below cases ; Modify A [ 1 ] and A [ k - 1 ] to get the required array ; Driver Code
using System ; class GFG { static void generateArray ( int n , int k ) { int [ ] array = new int [ k ] ; int remaining = n - ( k * ( k + 1 ) / 2 ) ; if ( remaining < 0 ) Console . Write ( " NO " ) ; int right_most = remaining % k ; int high = ( int ) Math . Ceiling ( remaining / ( k * 1.0 ) ) ; int low = ( int ) Math . Floor ( remaining / ( k * 1.0 ) ) ; for ( int i = k - right_most ; i < k ; i ++ ) array [ i ] = high ; for ( int i = 0 ; i < ( k - right_most ) ; i ++ ) array [ i ] = low ; for ( int i = 0 ; i < k ; i ++ ) array [ i ] += i + 1 ; if ( k - 1 != remaining k == 1 ) { foreach ( int u in array ) Console . Write ( u + " ▁ " ) ; } else if ( k == 2 k == 3 ) Console . Write ( " - 1 STRNEWLINE " ) ; else { array [ 1 ] -= 1 ; array [ k - 1 ] += 1 ; foreach ( int u in array ) Console . Write ( u + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int n = 26 , k = 6 ; generateArray ( n , k ) ; } }
Maximize the given number by replacing a segment of digits with the alternate digits given | C # implementation of the approach ; Function to return the maximized number ; Iterate till the end of the string ; Check if it is greater or not ; Replace with the alternate till smaller ; Return original s in case no change took place ; Driver Code
using System ; class GFG { static String get_maximum ( char [ ] s , int [ ] a ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] - '0' < a [ s [ i ] - '0' ] ) { int j = i ; while ( j < n && ( s [ j ] - '0' <= a [ s [ j ] - '0' ] ) ) { s [ j ] = ( char ) ( '0' + a [ s [ j ] - '0' ] ) ; j ++ ; } return String . Join ( " " , s ) ; } } return String . Join ( " " , s ) ; } public static void Main ( String [ ] args ) { String s = "1337" ; int [ ] a = { 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 } ; Console . WriteLine ( get_maximum ( s . ToCharArray ( ) , a ) ) ; } }
Number of times the largest perfect square number can be subtracted from N | C # implementation of the approach ; Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect square and subtract it from N ; Increment steps ; Return the required count ; Driver code
using System ; class GfG { static int countSteps ( int n ) { int steps = 0 ; while ( n > 0 ) { int largest = ( int ) Math . Sqrt ( n ) ; n -= ( largest * largest ) ; steps ++ ; } return steps ; } public static void Main ( ) { int n = 85 ; Console . WriteLine ( countSteps ( n ) ) ; } }
Maximum array sum that can be obtained after exactly k changes | C # implementation of the above approach ; Utility function to return the sum of the array elements ; Function to return the maximized sum of the array after performing the given operation exactly k times ; Sort the array elements ; Change signs of the negative elements starting from the smallest ; If a single operation has to be performed then it must be performed on the smallest positive element ; To store the index of the minimum element ; Update the minimum index ; Perform remaining operation on the smallest element ; Return the sum of the updated array ; Driver code
using System ; using System . Linq ; class GFG { static int sumArr ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int maxSum ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int i = 0 ; while ( i < n && k > 0 && arr [ i ] < 0 ) { arr [ i ] *= - 1 ; k -- ; i ++ ; } if ( k % 2 == 1 ) { int min = 0 ; for ( i = 1 ; i < n ; i ++ ) if ( arr [ min ] > arr [ i ] ) min = i ; arr [ min ] *= - 1 ; } return sumArr ( arr , n ) ; } static void Main ( ) { int [ ] arr = { - 5 , 4 , 1 , 3 , 2 } ; int n = arr . Length ; int k = 4 ; Console . WriteLine ( maxSum ( arr , n , k ) ) ; } }
Spanning Tree With Maximum Degree ( Using Kruskal 's Algorithm) | C # implementation of the approach ; par and rank will store the parent and rank of particular node in the Union Find Algorithm ; Find function of Union Find Algorithm ; Union function of Union Find Algorithm ; Function to find the required spanning tree ; Initialising parent of a node by itself ; Variable to store the node with maximum degree ; Finding the node with maximum degree ; Union of all edges incident on vertex with maximum degree ; Carrying out normal Kruskal Algorithm ; Driver code ; Number of nodes ; Number of edges ; ArrayList to store the graph ; Array to store the degree of each node in the graph ; Add edges and update degrees
using System ; using System . Collections . Generic ; class GFG { static int [ ] par ; static int [ ] rank ; static int find ( int x ) { if ( par [ x ] != x ) par [ x ] = find ( par [ x ] ) ; return par [ x ] ; } static void union ( int u , int v ) { int x = find ( u ) ; int y = find ( v ) ; if ( x == y ) return ; if ( rank [ x ] > rank [ y ] ) par [ y ] = x ; else if ( rank [ x ] < rank [ y ] ) par [ x ] = y ; else { par [ x ] = y ; rank [ y ] ++ ; } } static void findSpanningTree ( int [ ] deg , int n , int m , List < int > [ ] g ) { par = new int [ n + 1 ] ; rank = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) par [ i ] = i ; int max = 1 ; for ( int i = 2 ; i <= n ; i ++ ) if ( deg [ i ] > deg [ max ] ) max = i ; foreach ( int v in g [ max ] ) { Console . WriteLine ( max + " ▁ " + v ) ; union ( max , v ) ; } for ( int u = 1 ; u <= n ; u ++ ) { foreach ( int v in g [ u ] ) { int x = find ( u ) ; int y = find ( v ) ; if ( x == y ) continue ; union ( x , y ) ; Console . WriteLine ( u + " ▁ " + v ) ; } } } public static void Main ( String [ ] args ) { int n = 5 ; int m = 5 ; List < int > [ ] g = new List < int > [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) g [ i ] = new List < int > ( ) ; int [ ] deg = new int [ n + 1 ] ; g [ 1 ] . Add ( 2 ) ; g [ 2 ] . Add ( 1 ) ; deg [ 1 ] ++ ; deg [ 2 ] ++ ; g [ 1 ] . Add ( 5 ) ; g [ 5 ] . Add ( 1 ) ; deg [ 1 ] ++ ; deg [ 5 ] ++ ; g [ 2 ] . Add ( 3 ) ; g [ 3 ] . Add ( 2 ) ; deg [ 2 ] ++ ; deg [ 3 ] ++ ; g [ 5 ] . Add ( 3 ) ; g [ 3 ] . Add ( 5 ) ; deg [ 3 ] ++ ; deg [ 5 ] ++ ; g [ 3 ] . Add ( 4 ) ; g [ 4 ] . Add ( 3 ) ; deg [ 3 ] ++ ; deg [ 4 ] ++ ; findSpanningTree ( deg , n , m , g ) ; } }
Given count of digits 1 , 2 , 3 , 4 , find the maximum sum possible | C # program to maximum possible sum ; Function to find the maximum possible sum ; To store required sum ; Number of 234 's can be formed ; Sum obtained with 234 s ; Remaining 2 's ; Sum obtained with 12 s ; Return the required sum ; Driver code
using System ; class GFG { static int Maxsum ( int c1 , int c2 , int c3 , int c4 ) { int sum = 0 ; int two34 = Math . Min ( c2 , Math . Min ( c3 , c4 ) ) ; sum = two34 * 234 ; c2 -= two34 ; sum += Math . Min ( c2 , c1 ) * 12 ; return sum ; } public static void Main ( ) { int c1 = 5 , c2 = 2 , c3 = 3 , c4 = 4 ; Console . WriteLine ( Maxsum ( c1 , c2 , c3 , c4 ) ) ; } }