codeparrot/xlcost-text-to-code huggingface dataset:
Maximum amount of money that can be collected by a player in a game of coins | C # Program to implement the above approach ; Function to calculate the maximum amount collected by A ; Stores the money obtained by A ; Stores mid elements of odd sized rows ; Size of current row ; Increase money collected by A ; Add coins at even indices to the amount collected by A ; Print the amount ; Driver code ; Function call to calculate the amount of coins collected by A
using System ; using System . Collections . Generic ; class GFG { static void find ( int N , List < List < int > > Arr ) { int amount = 0 ; List < int > mid_odd = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int siz = Arr [ i ] . Count ; for ( int j = 0 ; j < siz / 2 ; j ++ ) amount = amount + Arr [ i ] [ j ] ; if ( siz % 2 == 1 ) mid_odd . Add ( Arr [ i ] [ siz / 2 ] ) ; } mid_odd . Sort ( ) ; for ( int i = 0 ; i < mid_odd . Count ; i ++ ) if ( i % 2 == 0 ) amount = amount + mid_odd [ i ] ; Console . WriteLine ( amount ) ; } static void Main ( ) { int N = 2 ; List < List < int > > Arr = new List < List < int > > ( ) ; Arr . Add ( new List < int > ( ) ) ; Arr [ 0 ] . Add ( 5 ) ; Arr [ 0 ] . Add ( 2 ) ; Arr [ 0 ] . Add ( 3 ) ; Arr [ 0 ] . Add ( 4 ) ; Arr . Add ( new List < int > ( ) ) ; Arr [ 1 ] . Add ( 1 ) ; Arr [ 1 ] . Add ( 6 ) ; find ( N , Arr ) ; } }
Check if an array can be split into K consecutive non | C # program for the above approach ; Function to check if array can be split into K consecutive and non - overlapping subarrays of length M consisting of a single distinct element ; Traverse over the range [ 0 , N - M - 1 ] ; Check if arr [ i ] is the same as arr [ i + m ] ; Increment current length t of pattern matched by 1 ; Check if t is equal to m , increment count of total repeated pattern ; Return true if length of total repeated pattern is k ; Update length of the current pattern ; Update count to 1 ; Finally return false if no pattern found ; Driver Code
using System ; public class GFG { static String checkPattern ( int [ ] arr , int m , int k , int n ) { int count = 1 , t = 0 ; for ( int i = 0 ; i < n - m ; i ++ ) { if ( arr [ i ] == arr [ i + m ] ) { t ++ ; if ( t == m ) { t = 0 ; count ++ ; if ( count == k ) { return " Yes " ; } } } else { t = 0 ; count = 1 ; } } return " No " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 1 , 3 , 3 , 3 , 3 } ; int M = 1 , K = 3 ; int N = arr . Length ; Console . Write ( checkPattern ( arr , M , K , N ) ) ; } }
Move all digits to the beginning of a given string | C # program for the above approach ; Function to move all the digit at the beginning of the string ; Replace all the non - numeric characters with " ▁ " and replace all digits with " ▁ " ; Print the string ; Driver Code ; Given String str
using System ; using System . Text . RegularExpressions ; public class GFG { static void moveAllDigitAtBeginning ( String str ) { String moveAllDigit = Regex . Replace ( str , " \\ D + " , " " ) + Regex . Replace ( str , " \\ d " , " " ) ; Console . WriteLine ( moveAllDigit ) ; } public static void Main ( String [ ] args ) { String str = " GeeksforGeeks1234" ; moveAllDigitAtBeginning ( str ) ; } }
Remove all occurrences of a word from a given string using Z | C # program for the above approach ; Function to fill the Z - array for str ; L Stores start index of window which matches with prefix of str ; R Stores end index of window which matches with prefix of str ; Iterate over the characters of str ; If i is greater thn R ; Update L and R ; If subString match with prefix ; Update R ; Update Z [ i ] ; Update R ; Update k ; if Z [ k ] is less than remaining interval ; Update Z [ i ] ; Start from R and check manually ; Update R ; Update Z [ i ] ; Update R ; Function to remove all the occurrences of word from str ; Create concatenated String " P $ T " ; Store Z array of concat ; Stores String , str by removing all the occurrences of word from str ; Stores length of word ; Traverse the array , Z [ ] ; if Z [ i + pSize + 1 ] equal to length of word ; Update i ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static void getZarr ( string str , int [ ] Z ) { int n = str . Length ; int k ; int L = 0 ; int R = 0 ; for ( int i = 1 ; i < n ; ++ i ) { if ( i > R ) { L = R = i ; while ( R < n && str [ R - L ] == str [ R ] ) { R ++ ; } Z [ i ] = R - L ; R -- ; } else { k = i - L ; if ( Z [ k ] < R - i + 1 ) { Z [ i ] = Z [ k ] ; } else { L = i ; while ( R < n && str [ R - L ] == str [ R ] ) { R ++ ; } Z [ i ] = R - L ; R -- ; } } } } static string goodStr ( string str , string word ) { string concat = word + " $ " + str ; int l = concat . Length ; int [ ] Z = new int [ l ] ; getZarr ( concat , Z ) ; string res = " " ; int pSize = word . Length ; for ( int i = 0 ; i < l ; ++ i ) { if ( i + pSize < l - 1 && Z [ i + pSize + 1 ] == pSize ) { i += pSize - 1 ; } else if ( i < str . Length ) { res += str [ i ] ; } } return res ; } static public void Main ( ) { string str = " Z - kmalgorithmkmiskmkmkmhelpfulkminkmsearching " ; string word = " km " ; Console . WriteLine ( goodStr ( str , word ) ) ; } }
Count of substrings of a string containing another given string as a substring | Set 2 | C # program for the above approach ; Function to count the substrings of string containing another given string as a substring ; Store length of string S ; Store length of string T ; Store the required count of substrings ; Store the starting index of last occurence of T in S ; Iterate in range [ 0 , n1 - n2 ] ; Check if substring from i to i + n2 is equal to T ; Check if substring from i to i + n2 is equal to T ; Mark chk as false and break the loop ; If chk is true ; Add ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) to answer ; Update the last to i + 1 ; Print the answer ; Driver code ; Function Call
using System ; class GFG { static void findOccurrences ( String S , String T ) { int n1 = S . Length ; int n2 = T . Length ; int ans = 0 ; int last = 0 ; for ( int i = 0 ; i <= n1 - n2 ; i ++ ) { bool chk = true ; for ( int j = 0 ; j < n2 ; j ++ ) { if ( T [ j ] != S [ i + j ] ) { chk = false ; break ; } } if ( chk ) { ans += ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) ; last = i + 1 ; } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { String S = " dabc " , T = " ab " ; findOccurrences ( S , T ) ; } }
Lexicographically largest N | C # program for the above approach ; Function to find the lexicographically largest bitonic sequence of size N elements lies in the range [ low , high ] ; Store index of highest element ; If high_index > ( N - 1 ) / 2 , then remaining N / 2 elements cannot be placed in bitonic order ; If high_index <= 0 , then set high_index as 1 ; Stores the resultant sequence ; Store the high value ; Maintain strictly decreasing sequence from index high_index to 0 starting with temp ; Store the value and decrement the temp variable by 1 ; Maintain the strictly decreasing sequence from index high_index + 1 to N - 1 starting with high - 1 ; Store the value and decrement high by 1 ; Print the resultant sequence ; Driver Code ; Function Call
using System ; class GFG { static void LargestArray ( int N , int low , int high ) { int high_index = N - ( high - low + 1 ) ; if ( high_index > ( N - 1 ) / 2 ) { Console . Write ( " Not ▁ Possible " ) ; return ; } if ( high_index <= 0 ) high_index = 1 ; int [ ] A = new int [ N ] ; int temp = high ; for ( int i = high_index ; i >= 0 ; i -- ) { A [ i ] = temp -- ; } high -= 1 ; for ( int i = high_index + 1 ; i < N ; i ++ ) A [ i ] = high -- ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 5 , low = 2 , high = 6 ; LargestArray ( N , low , high ) ; } }
Print matrix elements from top | C # program for the above approach ; Function to traverse the matrix diagonally upwards ; Stores the maximum size of vector from all row of matrix nums [ ] [ ] ; Store elements in desired order ; Store every element on the basis of sum of index ( i + j ) ; Print the stored result ; print in reverse order ; Driver Code ; Given vector of vectors arr ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printDiagonalTraversal ( int [ , ] nums ) { int max_size = nums . GetLength ( 0 ) ; List < List < int > > v = new List < List < int > > ( ) ; for ( int i = 0 ; i < 2 * max_size - 1 ; i ++ ) { v . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < nums . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < nums . GetLength ( 0 ) ; j ++ ) { v [ i + j ] . Add ( nums [ i , j ] ) ; } } for ( int i = 0 ; i < v . Count ; i ++ ) { for ( int j = v [ i ] . Count - 1 ; j >= 0 ; j -- ) { Console . Write ( v [ i ] [ j ] + " ▁ " ) ; } } } static void Main ( ) { int [ , ] arr = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printDiagonalTraversal ( arr ) ; } }
Check if given string satisfies the following conditions | C # program for the above approach ; Function to check if given string satisfies the given conditions ; Dimensions ; Left diagonal ; Right diagonal ; Conditions not satisfied ; Print Yes ; Driver code ; Given String ; Function call
using System ; class GFG { static void isValid ( string s ) { int n = ( int ) Math . Sqrt ( s . Length ) ; char check = s [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = i , y = i ; while ( x >= 0 && y < n ) { if ( s [ ( n * x ) + y ] != check || s [ ( n * y ) + x ] != check ) { Console . Write ( " No " ) ; return ; } x -- ; y ++ ; } } Console . Write ( " Yes " ) ; } public static void Main ( ) { string str = " abacdaeaafaghaia " ; isValid ( str ) ; } }
Sum of all possible strings obtained by removal of non | C # program for the above approach ; Function to convert a character to its equivalent digit ; Function to precompute powers of 10 ; Function to precompute prefix sum of numerical Strings ; Function to return the i - th term of Triangular Number ; Function to return the sum of all resulting Strings ; Precompute powers of 10 ; Precompute prefix sum ; Initialize result ; Apply the above general formula for every i ; Return the answer ; Driver Code ; Function Call
using System ; class GFG { static int N = 10 ; static int [ ] pref = new int [ N ] ; static int [ ] power = new int [ N ] ; static int toDigit ( char ch ) { return ( ch - '0' ) ; } static void powerOf10 ( ) { power [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) power [ i ] = power [ i - 1 ] * 10 ; } static void precomputePrefix ( char [ ] str , int n ) { pref [ 0 ] = str [ 0 ] - '0' ; for ( int i = 1 ; i < n ; i ++ ) pref [ i ] = pref [ i - 1 ] + toDigit ( str [ i ] ) ; } static int triangularNumber ( int i ) { int res = i * ( i + 1 ) / 2 ; return res ; } static int sumOfSubStrings ( String str ) { int n = str . Length ; powerOf10 ( ) ; precomputePrefix ( str . ToCharArray ( ) , n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += ( pref [ n - i - 2 ] * ( i + 1 ) + toDigit ( str [ n - i - 1 ] ) * triangularNumber ( n - i - 1 ) ) * power [ i ] ; } return ans ; } public static void Main ( String [ ] args ) { String str = "1234" ; Console . Write ( sumOfSubStrings ( str ) ) ; } }
Sum of products of all possible Subarrays | C # program for the above approach ; Function that finds the sum of products of all subarray of arr [ ] ; Stores sum of all subarrays ; Iterate array from behind ; Update the ans ; Update the res ; Print the final sum ; Driver Code ; Given array arr [ ] ; Size of array ; Function call
using System ; class solution { static void sumOfSubarrayProd ( int [ ] arr , int n ) { int ans = 0 ; int res = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int incr = arr [ i ] * ( 1 + res ) ; ans += incr ; res = incr ; } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . Length ; sumOfSubarrayProd ( arr , N ) ; } }
Sum of all odd length subarrays | C # program for the above approach ; Function to calculate the sum of all odd length subarrays ; Stores the sum ; Size of array ; Traverse the array ; Generate all subarray of odd length ; Add the element to sum ; Return the final sum ; Driver Code ; Given array arr [ ] ; Function call
using System ; class GFG { static int OddLengthSum ( int [ ] arr ) { int sum = 0 ; int l = arr . Length ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = i ; j < l ; j += 2 ) { for ( int k = i ; k <= j ; k ++ ) { sum += arr [ k ] ; } } } return sum ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 3 , 1 , 2 } ; Console . Write ( OddLengthSum ( arr ) ) ; } }
Sum of all odd length subarrays | C # program for the above approach ; Function that finds the sum of all the element of subarrays of odd length ; Stores the sum ; Size of array ; Traverse the given array [ ] arr ; Add to the sum for each contribution of the arr [ i ] ; Return the readonly sum ; Driver Code ; Given array [ ] arr ; Function call
using System ; class GFG { static int OddLengthSum ( int [ ] arr ) { int sum = 0 ; int l = arr . Length ; for ( int i = 0 ; i < l ; i ++ ) { sum += ( ( ( i + 1 ) * ( l - i ) + 1 ) / 2 ) * arr [ i ] ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 1 , 2 } ; Console . Write ( OddLengthSum ( arr ) ) ; } }
Check if Euler Totient Function is same for a given number and twice of that number | C # program of the above approach ; Function to find the Euler 's Totient Function ; Initialize result as N ; Consider all prime factors of n and subtract their multiples from result ; Return the count ; Function to check if phi ( n ) is equals phi ( 2 * n ) ; Driver Code
using System ; class GFG { static int phi ( int n ) { int result = 1 ; for ( int p = 2 ; p < n ; p ++ ) { if ( __gcd ( p , n ) == 1 ) { result ++ ; } } return result ; } static bool sameEulerTotient ( int n ) { return phi ( n ) == phi ( 2 * n ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int N = 13 ; if ( sameEulerTotient ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if Euler Totient Function is same for a given number and twice of that number | C # program of the above approach ; Function to check if phi ( n ) is equals phi ( 2 * n ) ; Return if N is odd ; Driver code ; Function call
using System ; class GFG { static int sameEulerTotient ( int N ) { return ( N & 1 ) ; } public static void Main ( String [ ] args ) { int N = 13 ; if ( sameEulerTotient ( N ) == 1 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Smallest occurring element in each subsequence | C # program for the above approach ; Function that count the subsequence such that each element as the minimum element in the subsequence ; Store index in a map ; Sort the array ; To store count of subsequence ; Traverse the array ; Store count of subsequence ; Print the count of subsequence ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void solve ( int [ ] arr , int N ) { Dictionary < int , int > M = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { M . Add ( i , arr [ i ] ) ; } Array . Sort ( arr ) ; Dictionary < int , int > Count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Count . Add ( arr [ i ] , ( int ) Math . Pow ( 2 , N - i - 1 ) ) ; } foreach ( KeyValuePair < int , int > m in M ) { Console . Write ( Count [ m . Value ] ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 1 } ; int N = arr . Length ; solve ( arr , N ) ; } }
Program to insert dashes between two adjacent odd digits in given Number | C # program to implement the above approach ; Function to check if char ch is odd or not ; Function to insert dash - between any 2 consecutive digit in string str ; Traverse the string character by character ; Compare every consecutive character with the odd value ; Print the resultant string ; Driver Code ; Given number in form of string ; Function call
using System ; using System . Text ; class GFG { static bool checkOdd ( char ch ) { return ( ( ch - '0' ) & 1 ) != 0 ? true : false ; } static String Insert_dash ( String num_str ) { StringBuilder result_str = new StringBuilder ( num_str ) ; for ( int x = 0 ; x < num_str . Length - 1 ; x ++ ) { if ( checkOdd ( num_str [ x ] ) && checkOdd ( num_str [ x + 1 ] ) ) { result_str . Insert ( x + 1 , " - " ) ; num_str = result_str . ToString ( ) ; x ++ ; } } return result_str . ToString ( ) ; } public static void Main ( String [ ] args ) { String str = "1745389" ; Console . WriteLine ( Insert_dash ( str ) ) ; } }
Check if a Matrix is Reverse Bitonic or Not | C # Program to check if a matrix is Reverse Bitonic or not ; Function to check if an array is Reverse Bitonic or not ; Check for decreasing sequence ; Check for increasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code
using System ; class GFG { static int N = 3 , M = 3 ; static bool checkReverseBitonic ( int [ ] arr , int n ) { int i , j , f = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { f = 1 ; break ; } } if ( i == n ) return true ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ j - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { if ( f == 1 ) return false ; } } return true ; } static void check ( int [ , ] arr ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ! checkReverseBitonic ( GetRow ( arr , i ) , M ) ) { Console . Write ( " No " + " STRNEWLINE " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { int [ ] temp = new int [ N ] ; for ( int j = 0 ; j < N ; j ++ ) { temp [ j ] = arr [ j , i ] ; } if ( ! checkReverseBitonic ( temp , N ) ) { Console . Write ( " No " + " STRNEWLINE " ) ; return ; } } Console . Write ( " Yes " ) ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int [ , ] m = { { 2 , 3 , 4 } , { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; check ( m ) ; } }
Find the element at the specified index of a Spirally Filled Matrix | C # program to find the element at given position in spirally filled matrix ; Function to return the element at ( x , y ) ; If y is greater ; If y is odd ; If y is even ; If x is even ; If x is odd ; Driver code
using System ; class GFG { static int SpiralElement ( int x , int y ) { int r ; if ( x < y ) { if ( y % 2 == 1 ) { r = y * y ; return ( r - x + 1 ) ; } else { r = ( y - 1 ) * ( y - 1 ) ; return ( r + x ) ; } } else { if ( x % 2 == 0 ) { r = x * x ; return ( r - y + 1 ) ; } else { r = ( x - 1 ) * ( x - 1 ) ; return ( r + y ) ; } } } static public void Main ( ) { int x = 2 , y = 3 ; Console . WriteLine ( SpiralElement ( x , y ) ) ; } }
Make the string in AP by changing a character | C # program for the above problem ; Function to modify the given String and find the index where modification is needed ; Array to store the ASCII values of alphabets ; loop to compute the ASCII values of characters a - z ; Set to store all the possible differences between consecutive elements ; Loop to find out the differences between consecutive elements and storing them in the set ; Checks if any character of the String disobeys the pattern ; Constructing the Strings with all possible values of consecutive difference and comparing them with staring String S . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void string_modify ( char [ ] s ) { char [ ] alphabets = new char [ 26 ] ; int flag = 0 , hold_i = 0 ; char hold_l = ( char ) 0 ; int i ; for ( i = 0 ; i < 26 ; i ++ ) { alphabets [ i ] = ( char ) ( i + ' a ' ) ; } HashSet < int > difference = new HashSet < int > ( ) ; String reconstruct = " " ; for ( i = 1 ; i < s . Length ; i ++ ) { difference . Add ( s [ i ] - s [ i - 1 ] ) ; } if ( difference . Count == 1 ) { Console . Write ( " No ▁ modifications ▁ required " ) ; return ; } foreach ( int it in difference ) { int index = s [ 0 ] - ' a ' ; reconstruct = " " ; flag = 0 ; for ( i = 0 ; i < s . Length && flag <= 1 ; i ++ ) { reconstruct += alphabets [ index ] ; index += it ; if ( index < 0 ) { index += 26 ; } index %= 26 ; if ( reconstruct [ i ] != s [ i ] ) { flag ++ ; hold_i = i ; hold_l = s [ i ] ; } } if ( flag == 1 ) { s [ hold_i ] = reconstruct [ hold_i ] ; break ; } } if ( flag < 1 ) { hold_i = 0 ; hold_l = s [ 0 ] ; int temp = ( s [ 1 ] - ' a ' - ( s [ 2 ] - s [ 1 ] ) ) % 26 ; if ( temp < 0 ) { temp += 26 ; } s [ 0 ] = alphabets [ temp ] ; } Console . Write ( hold_i + " ▁ - > ▁ " + hold_l + " STRNEWLINE " + String . Join ( " " , s ) + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String s = " aeimqux " ; string_modify ( s . ToCharArray ( ) ) ; } }
Create matrix whose sum of diagonals in each sub matrix is even | C # program for the above approach ; Function to print N * N order matrix with all sub - matrix of even order is sum of its diagonal also even ; Even index ; Odd index ; Iterate two nested loop ; For even index the element should be consecutive odd ; For odd index the element should be consecutive even ; Driver code ; Given order of matrix ; Function call
using System ; class GFG { static void evenSubMatrix ( int N ) { int even = 1 ; int odd = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { Console . Write ( even + " ▁ " ) ; even += 2 ; } else { Console . Write ( odd + " ▁ " ) ; odd += 2 ; } } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int N = 4 ; evenSubMatrix ( N ) ; } }
Remove leading zeros from a Number given as a string | C # program to implement the above approach ; Function to remove all leading zeros from a a given string ; Regex to remove leading zeros from a string ; Replaces the matched value with given string ; Driver Code
using System ; using System . Text . RegularExpressions ; class GFG { public static void removeLeadingZeros ( string str ) { string regex = " ^ 0 + ( ? ! $ ) " ; str = Regex . Replace ( str , regex , " " ) ; Console . WriteLine ( str ) ; } public static void Main ( string [ ] args ) { string str = "0001234" ; removeLeadingZeros ( str ) ; } }
Maximum inversions in a sequence of 1 to N after performing given operations at most K times | C # program for the above approach ; Function which computes the maximum number of inversions ; ' answer ' will store the required number of inversions ; We do this because we will never require more than floor ( n / 2 ) operations ; left pointer in the array ; right pointer in the array ; Doing k operations ; Incrementing ans by number of inversions increase due to this swapping ; Driver Code ; Input 1 ; Input 2
using System ; class GFG { static void maximum_inversion ( int n , int k ) { int answer = 0 ; k = Math . Min ( k , n / 2 ) ; int left = 1 ; int right = n ; while ( k != 0 ) { k -- ; answer += 2 * ( right - left ) - 1 ; left ++ ; right -- ; } Console . WriteLine ( answer ) ; } public static void Main ( String [ ] s ) { int N = 5 ; int K = 3 ; maximum_inversion ( N , K ) ; N = 4 ; K = 1 ; maximum_inversion ( N , K ) ; } }
First number to leave an odd remainder after repetitive division by 2 | C # program implementation of the approach ; Function to return the position least significant set bit ; Function return the first number to be converted to an odd integer ; Stores the positions of the first set bit ; If both are same ; If A has the least significant set bit ; Otherwise ; Driver code
using System ; class GFG { static int getFirstSetBitPos ( int n ) { return ( int ) ( Math . Log ( n & - n ) / Math . Log ( 2 ) ) ; } static int oddFirst ( int a , int b ) { int steps_a = getFirstSetBitPos ( a ) ; int steps_b = getFirstSetBitPos ( b ) ; if ( steps_a == steps_b ) { return - 1 ; } else if ( steps_a > steps_b ) { return b ; } else { return a ; } } public static void Main ( ) { int a = 10 ; int b = 8 ; Console . Write ( oddFirst ( a , b ) ) ; } }