codeparrot/xlcost-text-to-code huggingface dataset:
Count number of ways to cover a distance | A Dynamic Programming based C # program to count number of ways ; Create the array of size 3. ; Initialize the bases cases ; Run a loop from 3 to n Bottom up approach to fill the array ; Driver code
using System ; class GFG { static int printCountDP ( int dist ) { int [ ] ways = new int [ 3 ] ; int n = dist ; ways [ 0 ] = 1 ; ways [ 1 ] = 1 ; ways [ 2 ] = 2 ; for ( int i = 3 ; i <= n ; i ++ ) ways [ i % 3 ] = ways [ ( i - 1 ) % 3 ] + ways [ ( i - 2 ) % 3 ] + ways [ ( i - 3 ) % 3 ] ; return ways [ n % 3 ] ; } public static void Main ( String [ ] arg ) { int dist = 4 ; Console . Write ( printCountDP ( dist ) ) ; } }
Count numbers from 1 to n that have 4 as a digit | C # program to compute sum of digits in numbers from 1 to n ; Returns sum of all digits in numbers from 1 to n ; initialize result ; One by one compute sum of digits in every number from 1 to n ; A utility function to compute sum of digits in a given number x ; Driver Code
using System ; public class GFG { static int countNumbersWith4 ( int n ) { int result = 0 ; for ( int x = 1 ; x <= n ; x ++ ) result += has4 ( x ) ? 1 : 0 ; return result ; } static bool has4 ( int x ) { while ( x != 0 ) { if ( x % 10 == 4 ) return true ; x = x / 10 ; } return false ; } public static void Main ( ) { int n = 328 ; Console . WriteLine ( " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to ▁ " + " ▁ that ▁ have ▁ 4 ▁ as ▁ a ▁ a ▁ digit ▁ is ▁ " + countNumbersWith4 ( n ) ) ; } }
Count numbers from 1 to n that have 4 as a digit | C # program to count numbers having 4 as a digit ; Function to count numbers from 1 to n that have 4 as a digit ; Base case ; d = number of digits minus one in n . For 328 , d is 2 ; computing count of numbers from 1 to 10 ^ d - 1 , d = 0 a [ 0 ] = 0 ; d = 1 a [ 1 ] = count of numbers from 0 to 9 = 1 d = 2 a [ 2 ] = count of numbers from 0 to 99 = a [ 1 ] * 9 + 10 = 19 d = 3 a [ 3 ] = count of numbers from 0 to 999 = a [ 2 ] * 19 + 100 = 171 ; Computing 10 ^ d ; Most significant digit ( msd ) of n , For 328 , msd is 3 which can be obtained using 328 / 100 ; If MSD is 4. For example if n = 428 , then count of numbers is sum of following . 1 ) Count of numbers from 1 to 399 2 ) Count of numbers from 400 to 428 which is 29. ; IF MSD > 4. For example if n is 728 , then count of numbers is sum of following . 1 ) Count of numbers from 1 to 399 and count of numbers from 500 to 699 , i . e . , " a [ 2 ] ▁ * ▁ 6" 2 ) Count of numbers from 400 to 499 , i . e . 100 3 ) Count of numbers from 700 to 728 , recur for 28 ; IF MSD < 4. For example if n is 328 , then count of numbers is sum of following . 1 ) Count of numbers from 1 to 299 a 2 ) Count of numbers from 300 to 328 , recur for 28 ; Driver code
using System ; class GFG { static int countNumbersWith4 ( int n ) { if ( n < 4 ) return 0 ; int d = ( int ) Math . Log10 ( n ) ; int [ ] a = new int [ d + 2 ] ; a [ 0 ] = 0 ; a [ 1 ] = 1 ; for ( int i = 2 ; i <= d ; i ++ ) a [ i ] = a [ i - 1 ] * 9 + ( int ) Math . Ceiling ( Math . Pow ( 10 , i - 1 ) ) ; int p = ( int ) Math . Ceiling ( Math . Pow ( 10 , d ) ) ; int msd = n / p ; if ( msd == 4 ) return ( msd ) * a [ d ] + ( n % p ) + 1 ; if ( msd > 4 ) return ( msd - 1 ) * a [ d ] + p + countNumbersWith4 ( n % p ) ; return ( msd ) * a [ d ] + countNumbersWith4 ( n % p ) ; } static void Main ( ) { int n = 328 ; Console . WriteLine ( " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to ▁ " + n + " ▁ that ▁ have ▁ 4 ▁ as ▁ a ▁ digit ▁ is ▁ " + countNumbersWith4 ( n ) ) ; } }
Remove minimum elements from either side such that 2 * min becomes more than max | A O ( n * n ) solution to find the minimum of elements to be removed ; Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; Initialize starting and ending indexes of the maximum sized subarray with property 2 * min > max ; Choose different elements as starting point ; Initialize min and max for the current start ; Choose different ending points for current start ; Update min and max if necessary ; If the property is violated , then no point to continue for a bigger array ; Update longest_start and longest_end if needed ; If not even a single element follow the property , then return n ; Return the number of elements to be removed ; Driver program to test above functions
using System ; public class GFG { static int minRemovalsDP ( int [ ] arr , int n ) { int longest_start = - 1 , longest_end = 0 ; for ( int start = 0 ; start < n ; start ++ ) { int min = int . MaxValue , max = int . MinValue ; for ( int end = start ; end < n ; end ++ ) { int val = arr [ end ] ; if ( val < min ) { min = val ; } if ( val > max ) { max = val ; } if ( 2 * min <= max ) { break ; } if ( end - start > longest_end - longest_start longest_start == - 1 ) { longest_start = start ; longest_end = end ; } } } if ( longest_start == - 1 ) { return n ; } return ( n - ( longest_end - longest_start + 1 ) ) ; } public static void Main ( ) { int [ ] arr = { 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 } ; int n = arr . Length ; Console . WriteLine ( minRemovalsDP ( arr , n ) ) ; } }
Optimal Strategy for a Game | DP | C # program to find out maximum value from a given sequence of coins ; Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Create a table to store solutions of subproblems ; Fill table using above recursive formula . Note that the tableis filled in diagonal fashion ( similar to http : goo . gl / PQqoS ) , from diagonal elements to table [ 0 ] [ n - 1 ] which is the result . ; Here x is value of F ( i + 2 , j ) , y is F ( i + 1 , j - 1 ) and z is F ( i , j - 2 ) in above recursive formula ; Driver program
using System ; public class GFG { static int optimalStrategyOfGame ( int [ ] arr , int n ) { int [ , ] table = new int [ n , n ] ; int gap , i , j , x , y , z ; for ( gap = 0 ; gap < n ; ++ gap ) { for ( i = 0 , j = gap ; j < n ; ++ i , ++ j ) { x = ( ( i + 2 ) <= j ) ? table [ i + 2 , j ] : 0 ; y = ( ( i + 1 ) <= ( j - 1 ) ) ? table [ i + 1 , j - 1 ] : 0 ; z = ( i <= ( j - 2 ) ) ? table [ i , j - 2 ] : 0 ; table [ i , j ] = Math . Max ( arr [ i ] + Math . Min ( x , y ) , arr [ j ] + Math . Min ( y , z ) ) ; } } return table [ 0 , n - 1 ] ; } static public void Main ( ) { int [ ] arr1 = { 8 , 15 , 3 , 7 } ; int n = arr1 . Length ; Console . WriteLine ( " " + optimalStrategyOfGame ( arr1 , n ) ) ; int [ ] arr2 = { 2 , 2 , 2 , 2 } ; n = arr2 . Length ; Console . WriteLine ( " " + optimalStrategyOfGame ( arr2 , n ) ) ; int [ ] arr3 = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . Length ; Console . WriteLine ( " " + optimalStrategyOfGame ( arr3 , n ) ) ; } }
Maximum Sum Increasing Subsequence | DP | Dynamic Programming C # implementation of Maximum Sum Increasing Subsequence ( MSIS ) problem ; maxSumIS ( ) returns the maximum sum of increasing subsequence in arr [ ] of size n ; Initialize msis values for all indexes ; Compute maximum sum values in bottom up manner ; Pick maximum of all msis values ; Driver Code
using System ; class GFG { static int maxSumIS ( int [ ] arr , int n ) { int i , j , max = 0 ; int [ ] msis = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) msis [ i ] = arr [ i ] ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j ] + arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( max < msis [ i ] ) max = msis [ i ] ; return max ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 101 , 2 , 3 , 100 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( " Sum ▁ of ▁ maximum ▁ sum ▁ increasing ▁ " + " ▁ subsequence ▁ is ▁ " + maxSumIS ( arr , n ) ) ; } }
Find position i to split Array such that prefix sum till i | C # program for the above approach ; Function to check if there is an element forming G . P . series having common ratio k ; If size of array is less than three then return - 1 ; Initialize the variables ; Calculate total sum of array ; Calculate Middle element of G . P . series ; Iterate over the range ; Store the first element of G . P . series in the variable temp ; Return position of middle element of the G . P . series if the first element is in G . P . of common ratio k ; Else return 0 ; if middle element is not found in arr [ ] ; Driver Code ; Given array
using System ; class GFG { static int checkArray ( int [ ] arr , int N , int k ) { if ( N < 3 ) return - 1 ; int i , Sum = 0 , temp = 0 ; for ( i = 0 ; i < N ; i ++ ) Sum += arr [ i ] ; int R = ( k * k + k + 1 ) ; if ( Sum % R != 0 ) return 0 ; int Mid = k * ( Sum / R ) ; for ( i = 1 ; i < N - 1 ; i ++ ) { temp += arr [ i - 1 ] ; if ( arr [ i ] == Mid ) { if ( temp == Mid / k ) return i + 1 ; else return 0 ; } } return 0 ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , 1 , 4 , 20 , 6 , 15 , 9 , 10 } ; int N = arr . Length ; int K = 2 ; Console . WriteLine ( checkArray ( arr , N , K ) ) ; } }
Repeat last occurrence of each alphanumeric character to their position in character family times | C # program for the above approach ; Function to encode the given string ; Variable string to store the result ; Arrays to store the last occuring index of every character in the string ; Length of the string ; Iterate over the range ; If str [ i ] is between 0 and 9 ; If str [ i ] is between a and z ; If str [ i ] is between A and Z ; Iterate over the range ; If str [ i ] is between a and z and i is the last occurence in str ; If str [ i ] is between A and Z and i is the last occurence in str ; If str [ i ] is between 0 and 9 and i is the last occurence in str ; Print the result ; Driver Code
using System ; public class GFG { static void encodeString ( String str ) { String res = " " ; int [ ] small = new int [ 26 ] ; int [ ] capital = new int [ 26 ] ; int [ ] num = new int [ 10 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { small [ i ] = 0 ; capital [ i ] = 0 ; } for ( int i = 0 ; i < 10 ; i ++ ) { num [ i ] = 0 ; } int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] >= '0' && str [ i ] <= '9' ) { num [ str [ i ] - 48 ] = i ; } else if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) { small [ str [ i ] - 97 ] = i ; } else if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { capital [ str [ i ] - 65 ] = i ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) && small [ str [ i ] - 97 ] == i ) { int occ = str [ i ] - 96 ; while ( occ -- > 0 ) { res += str [ i ] ; } } else if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) && capital [ str [ i ] - 65 ] == i ) { int occ = str [ i ] - 64 ; while ( occ -- > 0 ) { res = res + str [ i ] ; } } else if ( ( str [ i ] >= '0' && str [ i ] <= '9' ) && num [ str [ i ] - 48 ] == i ) { int occ = str [ i ] - 48 ; while ( occ -- > 0 ) { res = res + str [ i ] ; } } else { res = res + str [ i ] ; } } Console . Write ( res ) ; } public static void Main ( String [ ] args ) { String str = " Ea2 , ▁ 0 , ▁ E " ; encodeString ( str ) ; } }
Form minimum number of Palindromic Strings from a given string | C # Program to implement the above approach ; Function to return minimum palindromic Strings formed ; Store the length of String ; Iterate over the String and store the frequency of all charactrs A vector to store the frequency ; Store the odd frequency characters ; Iterate through all possible characters and check the parity of frequency ; store answer in a vector ; if there are no odd Frequency characters then print the String with even characters ; store the left part of the palindromic String ; reverse the right part of the String ; store the String in ans vector ; take any character from off frequency element ; repeat the above step to form left and right Strings ; reverse the right part of the String ; store the String in ans vector ; store all other odd frequency Strings ; Print the answer ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static int minimumPalindromicStrings ( String S ) { int N = S . Length ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ S [ i ] - ' a ' ] ++ ; } List < int > oddFreqchars = new List < int > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] % 2 == 1 ) { oddFreqchars . Add ( i ) ; freq [ i ] -- ; } freq [ i ] /= 2 ; } List < String > ans = new List < String > ( ) ; if ( oddFreqchars . Count == 0 ) { String left = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { left += ( char ) ( i + ' a ' ) ; } } String right = left ; right = reverse ( right ) ; ans . Add ( left + right ) ; } else { String middle = " " ; int c = oddFreqchars [ oddFreqchars . Count - 1 ] ; oddFreqchars . RemoveAt ( oddFreqchars . Count - 1 ) ; middle += ( char ) ( c + ' a ' ) ; String left = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { left += ( char ) ( i + ' a ' ) ; } } String right = left ; right = reverse ( right ) ; ans . Add ( left + middle + right ) ; while ( oddFreqchars . Count != 0 ) { c = oddFreqchars [ oddFreqchars . Count - 1 ] ; oddFreqchars . RemoveAt ( oddFreqchars . Count - 1 ) ; middle = " " ; middle += ( char ) ( c + ' a ' ) ; ans . Add ( middle ) ; } } Console . Write ( " [ " ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . Write ( ans [ i ] ) ; if ( i != ans . Count - 1 ) { Console . Write ( " , ▁ " ) ; } } Console . Write ( " ] " ) ; return 0 ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; minimumPalindromicStrings ( S ) ; } }
Make Array elements equal by replacing adjacent elements with their XOR | C # program for the above approach ; Function to check if it is possible to make all the array elements equal using the given operation ; Stores the prefix XOR array ; Calculate prefix [ i ] ; Case 1 , check if the XOR of the input array is 0 ; Case 2 Iterate over all the ways to divide the array into three non empty subarrays ; XOR of Middle Block ; XOR of Left Block ; XOR of Right Block ; Not Possible ; Driver code ; Function Call
using System ; public class GFG { static void possibleEqualArray ( int [ ] A , int N ) { int [ ] pref = new int [ N ] ; pref [ 0 ] = A [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pref [ i ] = pref [ i - 1 ] ^ A [ i ] ; } if ( pref [ N - 1 ] == 0 ) { Console . WriteLine ( " YES " ) ; return ; } int cur_xor = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { cur_xor ^= A [ i ] ; for ( int j = 0 ; j < i ; j ++ ) { if ( j != 0 ) { int middle_xor = pref [ j - 1 ] ^ pref [ i - 1 ] ; int left_xor = pref [ j - 1 ] ; int right_xor = cur_xor ; if ( left_xor == middle_xor && middle_xor == right_xor ) { Console . WriteLine ( " YES " ) ; return ; } } } } Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 0 , 2 , 2 } ; int N = A . Length ; possibleEqualArray ( A , N ) ; } }
Construct an array whose Prefix XOR array starting from X is an N | C # program for above approach ; Function to print the required array ; Iteratie from 1 to N ; Print the i - th element ; Update prev_xor to i ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static void GenerateArray ( int N , int X ) { int prev_xor = X ; for ( int i = 1 ; i <= N ; i ++ ) { Console . Write ( i ^ prev_xor ) ; if ( i != N ) { Console . Write ( " ▁ " ) ; } prev_xor = i ; } } static void Main ( ) { int N = 4 , X = 3 ; Console . Write ( " The ▁ generated ▁ array ▁ is ▁ " ) ; GenerateArray ( N , X ) ; } }
Check if elements of a Binary Matrix can be made alternating | C # program for the above approach ; Function to create the possible grids ; Function to test if any one of them matches with the given 2 - D array ; Function to print the grid , if possible ; Function to check if the grid can be made alternating or not ; Grids to store the possible grids ; Driver code
using System ; class GFG { public static void createGrid ( char [ , ] grid , bool is1 , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( is1 ) { grid [ i , j ] = '0' ; is1 = false ; } else { grid [ i , j ] = '1' ; is1 = true ; } } if ( M % 2 == 0 ) is1 = ! is1 ; } } public static bool testGrid ( char [ , ] testGrid , char [ , ] Grid , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( Grid [ i , j ] != ' * ' ) { if ( Grid [ i , j ] != testGrid [ i , j ] ) { return false ; } } } } return true ; } public static void printGrid ( char [ , ] grid , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { Console . Write ( grid [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void findPossibleGrid ( int N , int M , char [ , ] grid ) { char [ , ] gridTest1 = new char [ N , 1001 ] ; char [ , ] gridTest2 = new char [ N , 1001 ] ; createGrid ( gridTest1 , true , N , M ) ; createGrid ( gridTest2 , false , N , M ) ; if ( testGrid ( gridTest1 , grid , N , M ) ) { Console . WriteLine ( " Yes " ) ; printGrid ( gridTest1 , N , M ) ; } else if ( testGrid ( gridTest2 , grid , N , M ) ) { Console . WriteLine ( " Yes " ) ; printGrid ( gridTest2 , N , M ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( ) { int N = 4 , M = 4 ; char [ , ] grid = { { ' * ' , ' * ' , '1' , '0' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , '0' , '1' } } ; findPossibleGrid ( N , M , grid ) ; } }
Find non | C # program for the above approach ; Function to find the possible output array ; Base case for the recursion ; If ind becomes half of the size then print the array . ; Exit the function . ; Iterate in the range . ; Put the values in the respective indices . ; Call the function to find values for other indices . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int INF64 = ( int ) 1e8 ; static int N = 200 * 1000 + 13 ; static int n ; static int [ ] arr = new int [ N ] ; static int [ ] brr = new int [ N ] ; static void brute ( int ind , int l , int r ) { if ( ind == n / 2 ) { for ( int i = 0 ; i < ( int ) n ; i ++ ) Console . Write ( brr [ i ] + " ▁ " ) ; System . Environment . Exit ( 0 ) ; } for ( int i = l ; i <= arr [ ind ] / 2 ; ++ i ) if ( arr [ ind ] - i <= r ) { brr [ ind ] = i ; brr [ n - ind - 1 ] = arr [ ind ] - i ; brute ( ind + 1 , i , arr [ ind ] - i ) ; } } public static void Main ( ) { n = 2 ; n *= 2 ; arr [ 0 ] = 5 ; arr [ 1 ] = 6 ; brute ( 0 , 0 , INF64 ) ; } }
Count of minimum numbers having K as the last digit required to obtain sum N | C # program for the above approach ; Stores the smallest number that ends with digit i ( 0 , 9 ) ; Stores the minimum number of steps to create a number ending with digit i ; Initialize elements as infinity ; Minimum number ending with digit i ; Minimum steps to create a number ending with digit i ; If N < SmallestNumber then , return - 1 ; Otherwise , return answer ; Driver Code
using System ; public class GFG { static int minCount ( int N , int K ) { int [ ] SmallestNumber = new int [ 10 ] ; int [ ] MinimumSteps = new int [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) { SmallestNumber [ i ] = Int32 . MaxValue ; MinimumSteps [ i ] = Int32 . MaxValue ; } for ( int i = 1 ; i <= 10 ; i ++ ) { int num = K * i ; SmallestNumber [ num % 10 ] = Math . Min ( SmallestNumber [ num % 10 ] , num ) ; MinimumSteps [ num % 10 ] = Math . Min ( MinimumSteps [ num % 10 ] , i ) ; } if ( N < SmallestNumber [ N % 10 ] ) { return - 1 ; } else { return MinimumSteps [ N % 10 ] ; } } static public void Main ( ) { int N = 42 , K = 7 ; Console . Write ( minCount ( N , K ) ) ; } }
Minimum number of operations required to make an array non | C # program for the above approach ; Function to count the minimum number of steps required to make arr non - decreasing ; Stores differences ; Stores the max number ; Traverse the array arr [ ] ; Update mx ; Update val ; Stores the result ; Iterate until 2 ^ res - 1 is less than val ; Return the answer ; Driver Code ; Given input ; Function call
using System ; using System . Collections . Generic ; class GFG { static int countMinSteps ( int [ ] arr , int N ) { int val = 0 ; int mx = Int32 . MinValue ; for ( int i = 0 ; i < N ; i ++ ) { int curr = arr [ i ] ; mx = Math . Max ( mx , curr ) ; val = Math . Max ( val , mx - curr ) ; } int res = 0 ; while ( ( 1 << res ) - 1 < val ) { ++ res ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 7 , 6 , 5 } ; int N = arr . Length ; Console . Write ( countMinSteps ( arr , N ) ) ; } }
Minimum XOR of at most K elements in range [ L , R ] | C # program for the above approach ; Function for K = 2 ; Function for K = 2 ; Function for K = 2 ; Function to calculate the minimum XOR of at most K elements in [ L , R ] ; Driver code ; Input ; Function call
using System ; class GFG { static int func2 ( int L , int R , int K ) { if ( R - L >= 2 ) return 1 ; return Math . Min ( L , L ^ R ) ; } static int func3 ( int L , int R , int K ) { if ( ( R ^ L ) > L && ( R ^ L ) < R ) return 0 ; return func2 ( L , R , K ) ; } static int func4 ( int L , int R , int K ) { if ( R - L >= 4 ) return 0 ; int minval = L ^ ( L + 1 ) ^ ( L + 2 ) ^ ( L + 3 ) ; return Math . Min ( minval , func3 ( L , R , K ) ) ; } static int minimumXor ( int L , int R , int K ) { if ( K > 4 ) return 0 ; else if ( K == 4 ) return func4 ( L , R , K ) ; else if ( K == 3 ) return func3 ( L , R , K ) ; else if ( K == 2 ) return func2 ( L , R , K ) ; else return L ; } static void Main ( ) { int L = 1 , R = 3 , K = 3 ; Console . Write ( minimumXor ( L , R , K ) ) ; } }
Nth term of Ruler Function Series | C # program for the above approach ; Function to count the number of set bits in the number N ; Store the number of setbits ; Update the value of n ; Update the count ; Return the total count ; Function to find the Nth term of the Ruler Function Series ; Store the result ; Print the result ; Driver Code
using System ; class GFG { static int setBits ( long n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static void findNthTerm ( int N ) { int x = setBits ( N ^ ( N - 1 ) ) ; Console . WriteLine ( x ) ; } public static void Main ( string [ ] args ) { int N = 8 ; findNthTerm ( N ) ; } }
Quadratic equation whose roots are reciprocal to the roots of given equation | C # program for the above approach ; Function to find the quadratic equation having reciprocal roots ; Print quadratic equation ; Driver Code ; Given coefficients ; Function call to find the quadratic equation having reciprocal roots
using System ; using System . Collections . Generic ; class GFG { static void findEquation ( int A , int B , int C ) { Console . Write ( " ( " + C + " ) " + " x ^ 2 ▁ + ( " + B + " ) x ▁ + ▁ ( " + A + " ) ▁ = ▁ 0" ) ; } public static void Main ( ) { int A = 1 , B = - 5 , C = 6 ; findEquation ( A , B , C ) ; } }
Minimize subtraction followed by increments of adjacent elements required to make all array elements equal | C # program for the above approach ; Function to find the minimum number of moves required to make all array elements equal ; Store the total sum of the array ; Calculate total sum of the array ; If the sum is not divisible by N , then print " - 1" ; Stores the average ; Stores the count of operations ; Traverse the array arr [ ] ; Update number of moves required to make current element equal to avg ; Update the overall count ; Return the minimum operations required ; Driver Code
using System ; class GFG { static int findMinMoves ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % N != 0 ) return - 1 ; int avg = sum / N ; int total = 0 ; int needCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { needCount += ( arr [ i ] - avg ) ; total = Math . Max ( Math . Max ( Math . Abs ( needCount ) , arr [ i ] - avg ) , total ) ; } return total ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 5 } ; int N = arr . Length ; Console . Write ( findMinMoves ( arr , N ) ) ; } }