codeparrot/xlcost-text-to-code huggingface dataset:
Program to count digits in an integer ( 4 Different Methods ) | C # Code to count number of digits in an integer ; Driver Code
using System ; class GFG { static int countDigit ( long n ) { if ( n / 10 == 0 ) return 1 ; return 1 + countDigit ( n / 10 ) ; } public static void Main ( ) { long n = 345289467 ; Console . WriteLine ( " Number ▁ of ▁ " + " digits ▁ : ▁ " + countDigit ( n ) ) ; } }
Tribonacci Numbers | A DP based C # program to print first n Tribonacci numbers . ; Driver code
using System ; class GFG { static void printTrib ( int n ) { int [ ] dp = new int [ n ] ; dp [ 0 ] = dp [ 1 ] = 0 ; dp [ 2 ] = 1 ; for ( int i = 3 ; i < n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( dp [ i ] + " ▁ " ) ; } public static void Main ( ) { int n = 10 ; printTrib ( n ) ; } }
Tribonacci Numbers | C # Program to print first n tribonacci numbers Matrix Multiplication function for 3 * 3 matrix ; Recursive function to raise the matrix T to the power n ; base condition . ; recursively call to square the matrix ; calculating square of the matrix T ; if n is odd multiply it one time with M ; base condition ; T [ 0 ] [ 0 ] contains the tribonacci number so return it ; Driver Code
using System ; class GFG { static void multiply ( int [ , ] T , int [ , ] M ) { int a , b , c , d , e , f , g , h , i ; a = T [ 0 , 0 ] * M [ 0 , 0 ] + T [ 0 , 1 ] * M [ 1 , 0 ] + T [ 0 , 2 ] * M [ 2 , 0 ] ; b = T [ 0 , 0 ] * M [ 0 , 1 ] + T [ 0 , 1 ] * M [ 1 , 1 ] + T [ 0 , 2 ] * M [ 2 , 1 ] ; c = T [ 0 , 0 ] * M [ 0 , 2 ] + T [ 0 , 1 ] * M [ 1 , 2 ] + T [ 0 , 2 ] * M [ 2 , 2 ] ; d = T [ 1 , 0 ] * M [ 0 , 0 ] + T [ 1 , 1 ] * M [ 1 , 0 ] + T [ 1 , 2 ] * M [ 2 , 0 ] ; e = T [ 1 , 0 ] * M [ 0 , 1 ] + T [ 1 , 1 ] * M [ 1 , 1 ] + T [ 1 , 2 ] * M [ 2 , 1 ] ; f = T [ 1 , 0 ] * M [ 0 , 2 ] + T [ 1 , 1 ] * M [ 1 , 2 ] + T [ 1 , 2 ] * M [ 2 , 2 ] ; g = T [ 2 , 0 ] * M [ 0 , 0 ] + T [ 2 , 1 ] * M [ 1 , 0 ] + T [ 2 , 2 ] * M [ 2 , 0 ] ; h = T [ 2 , 0 ] * M [ 0 , 1 ] + T [ 2 , 1 ] * M [ 1 , 1 ] + T [ 2 , 2 ] * M [ 2 , 1 ] ; i = T [ 2 , 0 ] * M [ 0 , 2 ] + T [ 2 , 1 ] * M [ 1 , 2 ] + T [ 2 , 2 ] * M [ 2 , 2 ] ; T [ 0 , 0 ] = a ; T [ 0 , 1 ] = b ; T [ 0 , 2 ] = c ; T [ 1 , 0 ] = d ; T [ 1 , 1 ] = e ; T [ 1 , 2 ] = f ; T [ 2 , 0 ] = g ; T [ 2 , 1 ] = h ; T [ 2 , 2 ] = i ; } static void power ( int [ , ] T , int n ) { if ( n == 0 n == 1 ) return ; int [ , ] M = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; power ( T , n / 2 ) ; multiply ( T , T ) ; if ( n % 2 != 0 ) multiply ( T , M ) ; } static int tribonacci ( int n ) { int [ , ] T = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; if ( n == 0 n == 1 ) return 0 ; else power ( T , n - 2 ) ; return T [ 0 , 0 ] ; } public static void Main ( ) { int n = 10 ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( tribonacci ( i ) + " ▁ " ) ; Console . WriteLine ( ) ; } }
Geometric mean ( Two Methods ) | Program to calculate the geometric mean of the given array elements . ; function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . . + log ( n ) ) / n ) and return the value to main function . ; Driver function ; function call
using System ; class GFG { static float geometricMean ( int [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( float ) Math . Log ( arr [ i ] ) ; sum = sum / n ; return ( float ) Math . Exp ( sum ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = arr . Length ; Console . WriteLine ( geometricMean ( arr , n ) ) ; } }
Smallest number k such that the product of digits of k is equal to n | C # implementation to find smallest number k such that the product of digits of k is equal to n ; function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to ' k ' ; required smallest number ; Driver program to test above
using System ; using System . Collections . Generic ; public class GFG { static long smallestNumber ( int n ) { if ( n >= 0 && n <= 9 ) { return n ; } Stack < int > digits = new Stack < int > ( ) ; for ( int i = 9 ; i >= 2 && n > 1 ; i -- ) { while ( n % i == 0 ) { digits . Push ( i ) ; n = n / i ; } } if ( n != 1 ) { return - 1 ; } long k = 0 ; while ( digits . Count != 0 ) { k = k * 10 + digits . Peek ( ) ; digits . Pop ( ) ; } return k ; } static public void Main ( ) { int n = 100 ; Console . Write ( smallestNumber ( n ) ) ; } }
Check if a number is magic ( Recursive sum of digits is 1 ) | C # program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code
using System ; class GFG { public static bool isMagic ( int n ) { int sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum == 1 ) ; } public static void Main ( ) { int n = 1234 ; if ( isMagic ( n ) ) Console . WriteLine ( " Magic ▁ Number " ) ; else Console . WriteLine ( " Not ▁ a ▁ magic ▁ Number " ) ; } }
Check if a number is magic ( Recursive sum of digits is 1 ) | C # program to check Whether the number is Magic or not . ; Accepting sample input ; Condition to check Magic number
using System ; using System . Collections . Generic ; class GFG { public static void Main ( String [ ] args ) { int x = 1234 ; if ( x % 9 == 1 ) Console . Write ( " Magic ▁ Number " ) ; else Console . Write ( " Not ▁ a ▁ Magic ▁ Number " ) ; } }
Sylvester 's sequence | C # Code for Sylvester sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driver program
using System ; class GFG { public static void printSequence ( int n ) { int a = 1 ; int ans = 2 ; int N = 1000000007 ; for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( ans + " ▁ " ) ; ans = ( ( a % N ) * ( ans % N ) ) % N ; a = ans ; ans = ( ans + 1 ) % N ; } } public static void Main ( ) { int n = 6 ; printSequence ( n ) ; } }
Program to find sum of first n natural numbers | C # program to find sum of first n natural numbers . ; Returns sum of first n natural numbers ; Driver code
using System ; class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int x = 1 ; x <= n ; x ++ ) sum = sum + x ; return sum ; } public static void Main ( ) { int n = 5 ; Console . Write ( findSum ( n ) ) ; } }
Hailstone Numbers | C # program to generate hailstone numbers and calculate steps required to reduce them to 1 ; function to print hailstone numbers and to calculate the number of steps required ; N is initially 1. ; N is reduced to 1. ; If N is Even . ; N is Odd . ; Driver code ; Function to generate Hailstone Numbers ; Output : Number of Steps
using System ; class GFG { static int c ; static int HailstoneNumbers ( int N ) { Console . Write ( N + " ▁ " ) ; if ( N == 1 && c == 0 ) { return c ; } else if ( N == 1 && c != 0 ) { c ++ ; return c ; } else if ( N % 2 == 0 ) { c ++ ; HailstoneNumbers ( N / 2 ) ; } else if ( N % 2 != 0 ) { c ++ ; HailstoneNumbers ( 3 * N + 1 ) ; } return c ; } public static void Main ( ) { int N = 7 ; int x ; x = HailstoneNumbers ( N ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Number ▁ of ▁ Steps : ▁ " + x ) ; } }
Find m | C # program to find m - th summation . ; Function to return mth summation ; base case ; Driver Code
using System ; class GFG { static int SUM ( int n , int m ) { if ( m == 1 ) return ( n * ( n + 1 ) / 2 ) ; int sum = SUM ( n , m - 1 ) ; return ( sum * ( sum + 1 ) / 2 ) ; } public static void Main ( ) { int n = 5 ; int m = 3 ; Console . Write ( " SUM ( " + n + " , ▁ " + m + " ) : ▁ " + SUM ( n , m ) ) ; } }
Count number of digits after decimal on dividing a number | C # program to count digits after dot when a number is divided by another . ; int ans = 0 ; Initialize result ; calculating remainder ; if this remainder appeared before then the numbers are irrational and would not converge to a solution the digits after decimal will be infinite ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int count ( int x , int y ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; while ( x % y != 0 ) { x = x % y ; ans ++ ; if ( m . ContainsKey ( x ) ) return - 1 ; m . Add ( x , 1 ) ; x = x * 10 ; } return ans ; } public static void Main ( String [ ] args ) { int res = count ( 1 , 2 ) ; if ( ( res == - 1 ) ) Console . WriteLine ( " INF " ) ; else Console . WriteLine ( res ) ; res = count ( 5 , 3 ) ; if ( ( res == - 1 ) ) Console . WriteLine ( " INF " ) ; else Console . WriteLine ( res ) ; res = count ( 3 , 5 ) ; if ( ( res == - 1 ) ) Console . WriteLine ( " INF " ) ; else Console . WriteLine ( res ) ; } }
Find smallest number n such that n XOR n + 1 equals to given k . | C # to find n such that XOR of n and n + 1 is equals to given n ; function to return the required n ; if k is of form 2 ^ i - 1 ; Driver code
using System ; class GFG { static int xorCalc ( int k ) { if ( k == 1 ) return 2 ; if ( ( ( k + 1 ) & k ) == 0 ) return k / 2 ; return 1 ; } public static void Main ( ) { int k = 31 ; Console . WriteLine ( xorCalc ( k ) ) ; } }
Find n | C # program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append 7 and move to parent ; Reverse res and return . ; Driver Code
using System ; class GFG { static string findNthNo ( int n ) { string res = " " ; while ( n >= 1 ) { if ( ( n & 1 ) == 1 ) { res = res + "4" ; n = ( n - 1 ) / 2 ; } else { res = res + "7" ; n = ( n - 2 ) / 2 ; } } char [ ] arr = res . ToCharArray ( ) ; Array . Reverse ( arr ) ; return new string ( arr ) ; } public static void Main ( ) { int n = 13 ; Console . Write ( findNthNo ( n ) ) ; } }
Total number of divisors for a given number | C # program for finding number of divisor ; program for finding no . of divisors ; sieve method for prime calculation ; Traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; Driver Code
using System ; class GFG { static int divCount ( int n ) { bool [ ] hash = new bool [ n + 1 ] ; for ( int p = 2 ; p * p < n ; p ++ ) if ( hash [ p ] == false ) for ( int i = p * 2 ; i < n ; i += p ) hash [ i ] = true ; int total = 1 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( hash [ p ] == false ) { int count = 0 ; if ( n % p == 0 ) { while ( n % p == 0 ) { n = n / p ; count ++ ; } total = total * ( count + 1 ) ; } } } return total ; } public static void Main ( ) { int n = 24 ; Console . WriteLine ( divCount ( n ) ) ; } }
Maximum number of unique prime factors | C # program to find maximum number of prime factors for a number in range [ 1 , N ] ; Return smallest number having maximum prime factors . ; Sieve of eratosthenes method to count number of unique prime factors . ; Return maximum element in arr [ ] ; Driver Code
using System ; class GFG { static int getMax ( int [ ] Arr ) { int max = Arr [ 0 ] ; for ( int i = 1 ; i < Arr . Length ; i ++ ) if ( Arr [ i ] > max ) max = Arr [ i ] ; return max ; } static int maxPrimefactorNum ( int N ) { int [ ] arr = new int [ N + 1 ] ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( arr [ i ] == 0 ) for ( int j = 2 * i ; j <= N ; j += i ) arr [ j ] ++ ; arr [ i ] = 1 ; } return getMax ( arr ) ; } public static void Main ( ) { int N = 40 ; Console . WriteLine ( maxPrimefactorNum ( N ) ) ; } }
Decimal to binary conversion without using arithmetic operators | C # implementation of decimal to binary conversion without using arithmetic operators ; function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Driver program to test above
using System ; class GFG { static String decToBin ( int n ) { if ( n == 0 ) return "0" ; String bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } public static void Main ( ) { int n = 38 ; Console . WriteLine ( decToBin ( n ) ) ; } }
Sum of array elements that is first continuously increasing then decreasing | C # Code for Sum of array elements that is first continuously increasing then decreasing ; Driver program to test above function
using System ; class GFG { public static int arraySum ( int [ ] arr , int n ) { int x = ( n + 1 ) / 2 ; return ( arr [ 0 ] - 1 ) * n + x * x ; } public static void Main ( ) { int [ ] arr = { 10 , 11 , 12 , 13 , 12 , 11 , 10 } ; int n = arr . Length ; Console . WriteLine ( arraySum ( arr , n ) ) ; } }
Balance pans using given weights that are powers of a number | C # code to check whether scale can be balanced or not ; method returns true if balancing of scale is possible ; baseForm vector will store T 's  representation on base a  in reverse order ; convert T to representation on base a ; make first digit of representation as 0 ; loop over base representation of T ; if any digit is not 0 , 1 , ( a - 1 ) or a then balancing is not possible ; if digit is a or ( a - 1 ) then increase left index ' s ▁ count / ▁ ( case , ▁ when ▁ this ▁ weight ▁ is ▁ transferred ▁ to ▁ T ' s side ) ; if representation is processed then balancing is possible ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isBalancePossible ( int T , int a ) { List < int > baseForm = new List < int > ( ) ; int s = 0 ; while ( T > 0 ) { baseForm . Add ( T % a ) ; T /= a ; s ++ ; } baseForm . Add ( 0 ) ; for ( int i = 0 ; i < s ; i ++ ) { if ( baseForm [ i ] != 0 && baseForm [ i ] != 1 && baseForm [ i ] != ( a - 1 ) && baseForm [ i ] != a ) { return false ; } if ( baseForm [ i ] == a || baseForm [ i ] == ( a - 1 ) ) { baseForm . Insert ( i + 1 , baseForm [ i + 1 ] + 1 ) ; } } return true ; } public static void Main ( ) { int T = 11 ; int a = 4 ; bool balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) { Console . WriteLine ( " Balance ▁ is ▁ possible " ) ; } else { Console . WriteLine ( " Balance ▁ is ▁ not ▁ possible " ) ; } } }
Number of digits in the product of two numbers | C # Code for Number of digits in the product of two numbers ; function to count number of digits in the product of two numbers ; if either of the number is 0 , then product will be 0 ; required count of digits ; Driver code
using System ; class GFG { public static int countDigits ( int a , int b ) { if ( a == 0 b == 0 ) return 1 ; return ( int ) Math . Floor ( Math . Log10 ( Math . Abs ( a ) ) + Math . Log10 ( Math . Abs ( b ) ) ) + 1 ; } static void Main ( ) { int a = 33 ; int b = - 24 ; Console . Write ( countDigits ( a , b ) ) ; } }
Distributing M items in a circle of size N starting from K | C # program to find the position where last item is delivered . ; n == > Size of circle m == > Number of items k == > Initial position ; n - k + 1 is number of positions before we reach beginning of circle If m is less than this value , then we can simply return ( m - 1 ) th position ; Let us compute remaining items before we reach beginning . ; We compute m % n to skip all complete rounds . If we reach end , we return n else we return m % n ; Driver Program to test above function
using System ; class GFG { static int lastPosition ( int n , int m , int k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; } public static void Main ( ) { int n = 5 ; int m = 8 ; int k = 2 ; Console . WriteLine ( lastPosition ( n , m , k ) ) ; } }
An interesting solution to get all prime numbers smaller than n | C # program prints prime numbers smaller than n ; Compute factorials and apply Wilson 's  theorem. ; Driver code
class GFG { static void primesInRange ( int n ) { int fact = 1 ; for ( int k = 2 ; k < n ; k ++ ) { fact = fact * ( k - 1 ) ; if ( ( fact + 1 ) % k == 0 ) System . Console . WriteLine ( k ) ; } } static void Main ( ) { int n = 15 ; primesInRange ( n ) ; } }
A product array puzzle | Set 2 ( O ( 1 ) Space ) | C # program for product array puzzle with O ( n ) time and O ( 1 ) space . ; epsilon value to maintain precision ; to hold sum of all values ; output product for each index anti log to find original product value ; Driver code
using System ; class GFG { static double EPS = 1e-9 ; static void productPuzzle ( int [ ] a , int n ) { double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += Math . Log10 ( a [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( ( int ) ( EPS + Math . Pow ( 10.00 , sum - Math . Log10 ( a [ i ] ) ) ) + " ▁ " ) ; } public static void Main ( ) { int [ ] a = { 10 , 3 , 5 , 6 , 2 } ; int n = a . Length ; Console . WriteLine ( " The ▁ product ▁ array ▁ is : ▁ " ) ; productPuzzle ( a , n ) ; } }
Change all even bits in a number to 0 | C # program to change even bits to 0. ; Returns modified number with all even bits 0. ; To store sum of bits at even positions . ; To store bits to shift ; One by one put all even bits to end ; If current last bit is set , add it to ans ; Next shift position ; Driver code
using System ; class GFG { static int changeEvenBits ( int n ) { int to_subtract = 0 ; int m = 0 ; for ( int x = n ; x > 0 ; x >>= 2 ) { if ( ( x & 1 ) > 0 ) to_subtract += ( 1 << m ) ; m += 2 ; } return n - to_subtract ; } public static void Main ( ) { int n = 30 ; Console . Write ( changeEvenBits ( n ) ) ; } }
Find the number closest to n and divisible by m | C # implementation to find the number closest to n and divisible by m ; function to find the number closest to n and divisible by m ; find the quotient ; 1 st possible closest number ; 2 nd possible closest number ; if true , then n1 is the required closest number ; else n2 is the required closest number ; Driver program to test above
using System ; class GFG { static int closestNumber ( int n , int m ) { int q = n / m ; int n1 = m * q ; int n2 = ( n * m ) > 0 ? ( m * ( q + 1 ) ) : ( m * ( q - 1 ) ) ; if ( Math . Abs ( n - n1 ) < Math . Abs ( n - n2 ) ) return n1 ; return n2 ; } public static void Main ( ) { int n = 13 , m = 4 ; Console . WriteLine ( closestNumber ( n , m ) ) ; n = - 15 ; m = 6 ; Console . WriteLine ( closestNumber ( n , m ) ) ; n = 0 ; m = 8 ; Console . WriteLine ( closestNumber ( n , m ) ) ; n = 18 ; m = - 7 ; Console . WriteLine ( closestNumber ( n , m ) ) ; } }
Check if a given number is Pronic | Java program to check and Print Pronic Number upto 200 ; function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code ; Printing Pronic Numbers upto 200
using System ; class GFG { static bool checkPronic ( int x ) { for ( int i = 0 ; i <= ( int ) ( Math . Sqrt ( x ) ) ; i ++ ) if ( x == i * ( i + 1 ) ) return true ; return false ; } public static void Main ( ) { for ( int i = 0 ; i <= 200 ; i ++ ) if ( checkPronic ( i ) ) Console . Write ( i + " ▁ " ) ; } }
Find minimum sum of factors of number | C # program to find minimum sum of product of number ; To find minimum sum of product of number ; Find factors of number and add to the sum ; Return sum of numbers having minimum product ; Driver Code
using System ; public class GFG { static int findMinSum ( int num ) { int sum = 0 ; for ( int i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { sum += i ; num /= i ; } } sum += num ; return sum ; } public static void Main ( ) { int num = 12 ; Console . Write ( findMinSum ( num ) ) ; } }
Minimum number with digits as 4 and 7 only and given sum | C # program to find smallest number with given sum of digits . ; Prints minimum number with given digit sum and only allowed digits as 4 and 7. ; Cases where all remaining digits are 4 or 7 ( Remaining sum of digits should be multiple of 4 or 7 ) ; If both 4 s and 7 s are there in digit sum , we subtract a 4. ; Driver code
using System ; class GFG { static void findMin ( int sum ) { int a = 0 , b = 0 ; while ( sum > 0 ) { if ( sum % 7 == 0 ) { b ++ ; sum -= 7 ; } else if ( sum % 4 == 0 ) { a ++ ; sum -= 4 ; } else { a ++ ; sum -= 4 ; } } if ( sum < 0 ) { Console . Write ( " - 1n " ) ; return ; } for ( int i = 0 ; i < a ; i ++ ) Console . Write ( "4" ) ; for ( int i = 0 ; i < b ; i ++ ) Console . Write ( "7" ) ; Console . WriteLine ( ) ; } public static void Main ( ) { findMin ( 15 ) ; } }
Add minimum number to an array so that the sum becomes even | C # program to add minimum number so that the sum of array becomes even ; Function to find out minimum number ; Count odd number of terms in array ; Driver Code
using System ; class GFG { static int minNum ( int [ ] arr , int n ) { int odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 != 0 ) odd += 1 ; return ( ( odd % 2 ) != 0 ) ? 1 : 2 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . Length ; Console . Write ( minNum ( arr , n ) ) ; } }
Find maximum number that can be formed using digits of a given number | C # program to print the maximum number from the set of digits of a given number ; Function to print the maximum number ; hashed array to store count of digits ; Converting given number to string ; Updating the count array ; result is to store the final number ; Traversing the count array to calculate the maximum number ; return the result ; Driver Code
using System ; class GFG { static int printMaxNum ( int num ) { int [ ] count = new int [ 10 ] ; String str = num . ToString ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) count [ str [ i ] - '0' ] ++ ; int result = 0 , multiplier = 1 ; for ( int i = 0 ; i <= 9 ; i ++ ) { while ( count [ i ] > 0 ) { result = result + ( i * multiplier ) ; count [ i ] -- ; multiplier = multiplier * 10 ; } } return result ; } public static void Main ( ) { int num = 38293367 ; Console . Write ( printMaxNum ( num ) ) ; } }
Form the largest number using at most one swap operation | C # implementation to form the largest number by applying atmost one swap operation ; function to form the largest number by applying atmost one swap operation ; for the rightmost digit , there will be no greater right digit ; index of the greatest right digit till the current index from the right direction ; traverse the array from second right element up to the left element ; if ' num . charAt ( i ) ' is less than the greatest digit encountered so far ; there is no greater right digit for ' num . charAt ( i ) ' ; update ' right ' index ; traverse the ' rightMax [ ] ' array from left to right ; if for the current digit , greater right digit exists then swap it with its greater right digit and break ; performing the required swap operation ; required largest number ; Utility method to swap two characters in a String ; Driver Function to test above Function
using System ; using System . Text ; public class GFG { static String largestNumber ( String num ) { int n = num . Length ; int right ; int [ ] rightMax = new int [ n ] ; rightMax [ n - 1 ] = - 1 ; right = n - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( num [ i ] < num [ right ] ) rightMax [ i ] = right ; else { rightMax [ i ] = - 1 ; right = i ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( rightMax [ i ] != - 1 ) { num = swap ( num , i , rightMax [ i ] ) ; break ; } } return num ; } static String swap ( String num , int i , int j ) { StringBuilder sb = new StringBuilder ( num ) ; sb [ i ] = num [ j ] ; sb [ j ] = num [ i ] ; return sb . ToString ( ) ; } public static void Main ( ) { String num = "8725634" ; Console . WriteLine ( " Largest ▁ Number ▁ : ▁ " + largestNumber ( num ) ) ; } }