codeparrot/xlcost-text-to-code huggingface dataset:
Count of square submatrices with average at least K | C # program for the above approach ; Function to count submatrixes with average greater than or equals to K ; Stores count of submatrices ; Stores the prefix sum of matrix ; Iterate over the range [ 1 , N ] ; Iterate over the range [ 1 , M ] ; Update the prefix sum ; Iterate over the range [ 1 , N ] ; Iterate over the range [ 1 , M ] ; Iterate until l and r are greater than 0 ; Update count ; Stores sum of submatrix with bottom right corner as ( i , j ) and top left corner as ( l , r ) ; If sum1 is less than or equal to sum2 ; Increment cnt by 1 ; Return cnt as the answer ; Driver code ; Given Input ; Function Call
using System ; class GFG { static int MAX = 1000 ; static int cntMatrices ( int [ , ] arr , int N , int M , int K ) { int cnt = 0 ; int [ , ] pre = new int [ N + 1 , M + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { pre [ i , j ] = arr [ i - 1 , j - 1 ] + pre [ i - 1 , j ] + pre [ i , j - 1 ] - pre [ i - 1 , j - 1 ] ; } } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { for ( int l = i , r = j ; l > 0 && r > 0 ; l -- , r -- ) { int sum1 = ( K * ( i - l + 1 ) * ( i - r + 1 ) ) ; int sum2 = pre [ i , j ] - pre [ l - 1 , r ] - pre [ l , r - 1 ] + pre [ l - 1 , r - 1 ] ; if ( sum1 <= sum2 ) cnt ++ ; } } } return cnt ; } public static void Main ( string [ ] args ) { int [ , ] arr = { { 2 , 2 , 3 } , { 3 , 4 , 5 } , { 4 , 5 , 5 } } ; int K = 4 ; int N = arr . GetLength ( 0 ) ; int M = arr . GetLength ( 0 ) ; Console . WriteLine ( cntMatrices ( arr , N , M , K ) ) ; } }
Concatenate the Array of elements into a single element | C # program for the above approach ; Function to find the integer value obtained by joining array elements together ; Stores the resulting integer value ; Traverse the array arr [ ] ; Stores the count of digits of arr [ i ] ; Update ans ; Increment ans by arr [ i ] ; Return the ans ; Driver Code ; Input ; Function call
using System ; class GFG { static int ConcatenateArr ( int [ ] arr , int N ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { int l = ( int ) Math . Floor ( Math . Log10 ( arr [ i ] ) + 1 ) ; ans = ans * ( int ) Math . Pow ( 10 , l ) ; ans += arr [ i ] ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 23 , 456 } ; int N = arr . Length ; Console . Write ( ConcatenateArr ( arr , N ) ) ; } }
Count of integers K in range [ 0 , N ] such that ( K XOR K + 1 ) equals ( K + 2 XOR K + 3 ) | C # program for the above approach ; Function to count all the integers less than N satisfying the given condition ; Store the count of even numbers less than N + 1 ; Return the count ; Driver code ; Given Input ; Function Call
using System ; class GFG { static int countXor ( int N ) { int cnt = ( int ) N / 2 + 1 ; return cnt ; } static void Main ( ) { int N = 4 ; Console . WriteLine ( countXor ( N ) ) ; } }
Maximize absolute displacement from origin by moving on X | C # program for the above approach ; Recursive function to find the maximum absolute displacement from origin by performing the given set of moves ; If i is equal to N ; If S [ i ] is equal to ' L ' ; If S [ i ] is equal to ' R ' ; If S [ i ] is equal to ' ? ' ; Function to find the maximum absolute displacement from the origin ; Return the maximum absolute displacement ; Driver Code ; Input ; Function call
using System ; using System . Collections . Generic ; class GFG { static int DistRecursion ( string S , int i , int dist ) { if ( i == S . Length ) return Math . Abs ( dist ) ; if ( S [ i ] == ' L ' ) return DistRecursion ( S , i + 1 , dist - 1 ) ; if ( S [ i ] == ' R ' ) return DistRecursion ( S , i + 1 , dist + 1 ) ; return Math . Max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) ; } static int maxDistance ( string S ) { return DistRecursion ( S , 0 , 0 ) ; } public static void Main ( ) { string S = " ? RRR ? " ; Console . Write ( maxDistance ( S ) ) ; } }
Count of pairs in range [ P , Q ] with numbers as multiple of R and their product lie in range [ P * Q / 4 , P * Q ] | C # program for the above approach ; Function to find the number of pairs such that both the elements are in the range [ P , Q ] and the numbers should be multiple of R , and the product of numbers should lie in the range [ P * Q / 4 , P * Q ] ; Store multiple of r in range of [ P , Q ] ; Itearte in the range [ p , q ] ; Vector to store pair of answer ; Iterate through the vector v ; Iterate in the range [ i + 1 , v . size ( ) - 1 ] ; If pair follow this condition insert the pair in vector ans ; If no pair satisfy the conditions , print - 1 ; Print the pairs which satisfy the given condition ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void findPairs ( int p , int q , int r ) { List < int > v = new List < int > ( ) ; for ( int i = p ; i <= q ; i ++ ) { if ( i % r == 0 ) { v . Add ( i ) ; } } List < List < int > > ans = new List < List < int > > ( ) ; for ( int i = 0 ; i < v . Count ; i ++ ) { for ( int j = i + 1 ; j < v . Count ; j ++ ) { if ( v [ i ] * v [ j ] >= p * q / 4 && v [ i ] * v [ j ] <= p * q ) { List < int > temp = new List < int > ( ) ; temp . Add ( v [ i ] ) ; temp . Add ( v [ j ] ) ; ans . Add ( temp ) ; } } } if ( ans . Count == 0 ) { Console . Write ( - 1 ) ; } else { foreach ( List < int > subList in ans ) { foreach ( int item in subList ) { Console . Write ( item + " ▁ " ) ; } Console . WriteLine ( ) ; } } } public static void Main ( ) { int p = 14 , q = 30 , r = 5 ; findPairs ( p , q , r ) ; } }
Print rectangular pattern with given center | C # program for the above approach ; Function to print the matrix filled with rectangle pattern having center coordinates are c1 , c2 ; Iterate in the range [ 0 , n - 1 ] ; Iterate in the range [ 0 , n - 1 ] ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static void printRectPattern ( int c1 , int c2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( ( Math . Max ( Math . Abs ( c1 - i ) , Math . Abs ( c2 - j ) ) ) + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int c1 = 2 ; int c2 = 2 ; int n = 5 ; printRectPattern ( c1 , c2 , n ) ; } }
Count of numbers in range [ L , R ] with LSB as 0 in their Binary representation | C # implementation of the approach ; Function to return the count of required numbers ; If rightmost bit is 0 ; Return the required count ; Driver code ; Call function countNumbers
using System ; class GFG { static int countNumbers ( int l , int r ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( ( i & 1 ) == 0 ) count += 1 ; } return count ; } public static void Main ( ) { int l = 10 , r = 20 ; Console . WriteLine ( countNumbers ( l , r ) ) ; } }
Count of numbers in range [ L , R ] with LSB as 0 in their Binary representation | C # implementation of the approach ; Function to return the count of required numbers ; Count of numbers in range which are divisible by 2 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countNumbers ( int l , int r ) { return ( ( r / 2 ) - ( l - 1 ) / 2 ) ; } public static void Main ( ) { int l = 10 , r = 20 ; Console . Write ( countNumbers ( l , r ) ) ; } }
Length of longest Fibonacci subarray | C # program for the above approach ; A utility function that returns true if x is perfect square ; Returns true if n is a Fibinacci Number , else false ; Here n is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perferct square ; Function to find the length of the largest sub - array of an array every element of whose is a Fibonacci number ; Traverse the array arr [ ] ; Check if arr [ i ] is a Fibonacci number ; Stores the maximum length of the Fibonacci number subarray ; Finally , return the maximum length ; Driver code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool isPerfectSquare ( int x ) { int s = ( int ) Math . Sqrt ( x ) ; return ( s * s == x ) ; } static bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } static int contiguousFibonacciNumber ( int [ ] arr , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isFibonacci ( arr [ i ] ) ) { current_length ++ ; } else { current_length = 0 ; } max_length = Math . Max ( max_length , current_length ) ; } return max_length ; } public static void Main ( ) { int [ ] arr = { 11 , 8 , 21 , 5 , 3 , 28 , 4 } ; int n = arr . Length ; Console . Write ( contiguousFibonacciNumber ( arr , n ) ) ; } }
Minimum number of flips required such that a Binary Matrix doesn 't contain any path from the top left to the bottom right consisting only of 0s | C # program for the above approach ; The four direction coordinates changes from the current cell ; Function that returns true if there exists any path from the top - left to the bottom - right cell of 0 s ; If the bottom - right cell is reached ; Update the cell to 1 ; Traverse in all four directions ; Find the new coordinates ; If the new cell is valid ; Recursively call DFS ; If path exists , then return true ; Return false , if there doesn 't  exists any such path ; Function to flip the minimum number of cells such that there doesn 't exists any such path from (0, 0) to (N - 1, M - 1) cell consisting of 0s ; Case 1 : If no such path exists already ; Case 2 : If there exists only one path ; Case 3 : If there exists two - path ; Driver code
using System ; class GFG { static int [ , ] direction = { { - 1 , 0 } , { 0 , 1 } , { 0 , - 1 } , { 1 , 0 } } ; static bool dfs ( int [ , ] matrix , int i , int j , int N , int M ) { if ( i == N - 1 && j == M - 1 ) { return true ; } matrix [ i , j ] = 1 ; for ( int k = 0 ; k < 4 ; k ++ ) { int newX = i + direction [ k , 0 ] ; int newY = j + direction [ k , 1 ] ; if ( newX >= 0 && newX < N && newY >= 0 && newY < M && matrix [ newX , newY ] == 0 ) { if ( dfs ( matrix , newX , newY , N , M ) ) { return true ; } } } return false ; } static int solve ( int [ , ] matrix ) { int N = matrix . GetLength ( 0 ) ; int M = matrix . GetLength ( 1 ) ; if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 0 ; } if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 1 ; } return 2 ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; Console . WriteLine ( solve ( mat ) ) ; } }
Maximize product of subarray sum with its maximum element | C # program for the above approach ; Function to find the maximum product of the sum of the subarray with its maximum element ; Traverse the array arr [ ] ; Increment currSum by a [ i ] ; Maximize the value of currMax ; Maximize the value of largestSum ; If currSum goes less than 0 then update currSum = 0 ; Return the resultant value ; Function to maximize the product of the sum of the subarray with its maximum element ; Find the largest sum of the subarray ; Multiply each array element with - 1 ; Find the largest sum of the subarray with negation of all array element ; Return the resultant maximum value ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int Kadane ( int [ ] arr , int n ) { int largestSum = 0 , currMax = 0 ; int currSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currSum += arr [ i ] ; currMax = Math . Max ( currMax , arr [ i ] ) ; largestSum = Math . Max ( largestSum , currMax * currSum ) ; if ( currSum < 0 ) { currMax = 0 ; currSum = 0 ; } } return largestSum ; } static int maximumWeight ( int [ ] arr , int n ) { int largestSum = Kadane ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = - arr [ i ] ; } largestSum = Math . Max ( largestSum , Kadane ( arr , n ) ) ; return largestSum ; } public static void Main ( ) { int [ ] arr = { 2 , - 3 , 8 , - 2 , 5 } ; int N = arr . Length ; Console . Write ( maximumWeight ( arr , N ) ) ; } }
Partition an array into two subsets with equal count of unique elements | C # program for the above approach ; Function to partition the array into two subsets such that count of unique elements in both subsets is the same ; Stores the subset number for each array elements ; Stores the count of unique array elements ; Stores the frequency of elements ; Traverse the array ; Count of elements having a frequency of 1 ; Check if there exists any element with frequency > 2 ; Count of elements needed to have frequency exactly 1 in each subset ; Initialize all values in the / array ans [ ] as 1 ; Traverse the array ans [ ] ; This array element is a part of first subset ; Half array elements with frequency 1 are part of the second subset ; If count of elements is exactly 1 are odd and has no element with frequency > 2 ; If count of elements that occurs exactly once are even ; Print the result ; If the count of elements has exactly 1 frequency are odd and there is an element with frequency greater than 2 ; Print the result ; Driver Codea
using System ; using System . Collections . Generic ; class GFG { static void arrayPartition ( int [ ] a , int n ) { int [ ] ans = new int [ n ] ; int cnt = 0 ; int ind = 0 , flag = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) mp [ a [ i ] ] ++ ; else mp . Add ( a [ i ] , 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) && mp [ a [ i ] ] == 1 ) cnt ++ ; if ( mp . ContainsKey ( a [ i ] ) && mp [ a [ i ] ] > 2 && flag == 0 ) { flag = 1 ; ind = i ; } } int p = ( cnt + 1 ) / 2 ; int ans1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) && mp [ a [ i ] ] == 1 && ans1 < p ) { ans [ i ] = 1 ; ans1 ++ ; } else if ( mp . ContainsKey ( a [ i ] ) && mp [ a [ i ] ] == 1 ) { ans [ i ] = 2 ; } } if ( cnt % 2 == 1 && flag == 0 ) { Console . Write ( - 1 ) ; return ; } if ( cnt % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } else { for ( int i = 0 ; i < n ; i ++ ) { if ( ind == i ) Console . Write ( 2 + " ▁ " ) ; else Console . Write ( ans [ i ] + " ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 3 , 4 , 4 } ; int N = arr . Length ; arrayPartition ( arr , N ) ; } }
Modify a given array by replacing each element with the sum or product of their digits based on a given condition | C # program for the above approach ; Function to modify the given array as per the given conditions ; Traverse the given array arr [ ] ; Initialize the count of even and odd digits ; Initialize temp with the current array element ; For count the number of even digits ; Increment the odd count ; Otherwise ; Divide temp by 10 ; Performe addition ; Performe multiplication ; Otherwise ; Driver Code
using System ; class GFG { static void evenOdd ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int even_digits = 0 ; int odd_digits = 0 ; int temp = arr [ i ] ; while ( temp > 0 ) { if ( ( temp % 10 ) % 2 != 0 ) odd_digits ++ ; else even_digits ++ ; temp /= 10 ; } if ( even_digits > odd_digits ) { int res = 0 ; while ( arr [ i ] > 0 ) { res += arr [ i ] % 10 ; arr [ i ] /= 10 ; } Console . Write ( res + " ▁ " ) ; } else if ( odd_digits > even_digits ) { int res = 1 ; while ( arr [ i ] > 0 ) { res *= arr [ i ] % 10 ; arr [ i ] /= 10 ; } Console . Write ( res + " ▁ " ) ; } else Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 113 , 141 , 214 , 3186 } ; int N = arr . Length ; evenOdd ( arr , N ) ; } }
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers | C # implementation of the above approach ; Function to print the prime factorization of the product of all numbers <= N that are even and can be expressed as a product of two distinct prime numbers ; sieve of Eratosthenese ; Store prime numbers in the range [ 3 , N / 2 ] ; Print the coefficient of 2 in the prime factorization ; Print the coefficients of other primes ; Driver Code ; Input ; Function calling
using System ; using System . Collections . Generic ; class GFG { static void primeFactorization ( int N ) { int [ ] sieve = new int [ N / 2 + 1 ] ; for ( int i = 0 ; i <= N / 2 ; i ++ ) { sieve [ i ] = 0 ; } for ( int i = 2 ; i <= N / 2 ; i ++ ) { if ( sieve [ i ] == 0 ) { for ( int j = i * i ; j <= N / 2 ; j += i ) { sieve [ j ] = 1 ; } } } List < int > prime = new List < int > ( ) ; for ( int i = 3 ; i <= N / 2 ; i ++ ) if ( sieve [ i ] == 0 ) prime . Add ( i ) ; int x = prime . Count ; Console . WriteLine ( "2 - > " + x ) ; foreach ( int i in prime ) Console . WriteLine ( i + " - > 1" ) ; } public static void Main ( String [ ] args ) { int N = 18 ; primeFactorization ( N ) ; } }
Sum of the first M elements of Array formed by infinitely concatenating given array | C # program for the above approach ; Function to find the sum of first M numbers formed by the infinite concatenation of the array A [ ] ; Stores the resultant sum ; Iterate over the range [ 0 , M - 1 ] ; Add the value A [ i % N ] to sum ; Return the resultant sum ; Driver Code
using System ; class GFG { static int sumOfFirstM ( int [ ] A , int N , int M ) { int sum = 0 ; for ( int i = 0 ; i < M ; i ++ ) { sum = sum + A [ i % N ] ; } return sum ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int M = 5 ; int N = arr . Length ; Console . WriteLine ( sumOfFirstM ( arr , N , M ) ) ; } }
Check if it is possible to reach M from 0 by given paths | C # program for the above approach ; Function to check if it is possible to reach M from 0 ; Stores the farther point that can reach from 1 point ; Stores the farthest point it can go for each index i ; Initialize rightMost [ i ] with 0 ; Traverse the array ; Update the rightMost position reached from a1 ; Find the farthest point it can reach from i ; If point < can be reached ; Driver Code
using System ; class GFG { static void canReach0toM ( int [ , ] a , int n , int m ) { int [ ] rightMost = new int [ m + 1 ] ; int [ ] dp = new int [ m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { rightMost [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { int a1 = a [ i , 0 ] ; int b1 = a [ i , 1 ] ; rightMost [ a1 ] = Math . Max ( rightMost [ a1 ] , b1 ) ; } for ( int i = m ; i >= 0 ; i -- ) { dp [ i ] = i ; for ( int j = Math . Min ( m , rightMost [ i ] ) ; j > i ; j -- ) { dp [ i ] = Math . Max ( dp [ i ] , dp [ j ] ) ; } } if ( dp [ 0 ] >= m ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { int [ , ] arr = { { 0 , 2 } , { 2 , 2 } , { 2 , 5 } , { 4 , 5 } } ; int M = 5 ; int N = arr . GetLength ( 0 ) ; canReach0toM ( arr , N , M ) ; } }
Count of non co | C # program for the above approach ; Recursive function to return gcd of two numbers ; Function to count the number of non co - prime pairs for each query ; Traverse the array arr [ ] ; Stores the count of non co - prime pairs ; Iterate over the range [ 1 , x ] ; Iterate over the range [ x , y ] ; If gcd of current pair is greater than 1 ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void countPairs ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int x = 1 ; x <= arr [ i ] ; x ++ ) { for ( int y = x ; y <= arr [ i ] ; y ++ ) { if ( gcd ( x , y ) > 1 ) count ++ ; } } Console . Write ( count + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 10 , 20 } ; int N = 3 ; countPairs ( arr , N ) ; } }
Count of non co | C # program for the above approach ; Auxiliary function to pre - compute the answer for each array ; Iterate over the range [ 1 , MAX ] ; Iterate over the range [ 1 , MAX ] ; If the number is prime ; Subtract the number of pairs which has i as one of their factors ; Iterate over the range [ 1 , MAX ] ; Function to count the number of non co - prime pairs for each query ; The i - th element stores the count of element that are co - prime with i ; Stores the resulting array ; Function Call ; Traverse the array arr [ ] ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static int MAX = 1005 ; static void preCalculate ( int [ ] phi , int [ ] ans ) { phi [ 0 ] = 0 ; phi [ 1 ] = 1 ; for ( int i = 2 ; i <= MAX ; i ++ ) phi [ i ] = i ; for ( int i = 2 ; i <= MAX ; i ++ ) { if ( phi [ i ] == i ) { for ( int j = i ; j <= MAX ; j += i ) phi [ j ] -= ( phi [ j ] / i ) ; } } for ( int i = 1 ; i <= MAX ; i ++ ) ans [ i ] = ans [ i - 1 ] + ( i - phi [ i ] ) ; } static void countPairs ( int [ ] arr , int N ) { int [ ] phi = new int [ 100000 ] ; Array . Clear ( phi , 0 , 100000 ) ; int [ ] ans = new int [ 100000 ] ; Array . Clear ( ans , 0 , 100000 ) ; preCalculate ( phi , ans ) ; for ( int i = 0 ; i < N ; ++ i ) { Console . Write ( ans [ arr [ i ] ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 5 , 10 , 20 } ; int N = 3 ; countPairs ( arr , N ) ; } }
Maximum number of multiplication by 3 or division by 2 operations possible on an array | C # program for the above approach ; Function to count maximum number of multiplication by 3 or division by 2 operations that can be performed ; Stores the maximum number of operations possible ; Traverse the array arr [ ] ; Iterate until arr [ i ] is even ; Increment count by 1 ; Update arr [ i ] ; Return the value of Count as the answer ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int maximumTurns ( int [ ] arr , int N ) { int Count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { while ( arr [ i ] % 2 == 0 ) { Count ++ ; arr [ i ] = arr [ i ] / 2 ; } } return Count ; } public static void Main ( ) { int [ ] arr = { 5 , 2 , 4 } ; int N = arr . Length ; Console . Write ( maximumTurns ( arr , N ) ) ; } }
Distribute the white and black objects into maximum groups under certain constraints | C # program for the above approach ; Function to check if it is possible to distribute W and B into maximum groups possible ; If W is greater than B , swap them ; Distribution is not possible ; Distribution is possible ; Driver code ; Input ; Function call
using System ; class GFG { static void isPossible ( int W , int B , int D ) { if ( W > B ) { int temp = W ; W = B ; B = temp ; } if ( B > W * ( D + 1 ) ) Console . WriteLine ( " NO " ) ; else Console . WriteLine ( " YES " ) ; } public static void Main ( ) { int W = 2 ; int B = 5 ; int D = 2 ; isPossible ( W , B , D ) ; } }
Find the maximum GCD possible for some pair in a given range [ L , R ] | C # program for the above approach ; Function to calculate GCD ; Function to calculate maximum GCD in a range ; Variable to store the answer ; If Z has two multiples in [ L , R ] ; Update ans ; Return the value ; Driver code ; Input ; Function Call
using System ; class GFG { public static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } public static int maxGCDInRange ( int L , int R ) { int ans = 1 ; for ( int Z = R ; Z >= 1 ; Z -- ) { if ( ( R / Z ) - ( L - 1 ) / Z > 1 ) { ans = Z ; break ; } } return ans ; } public static void Main ( ) { int L = 102 ; int R = 139 ; Console . Write ( maxGCDInRange ( L , R ) ) ; } }
Number of ways to form a number with maximum Ks in it | C # program for the above approach ; Function to calculate number of ways a number can be formed that has the maximum number of Ks ; Convert to string ; Calculate length of subarrays that can contribute to the answer ; Count length of subarray where adjacent digits add up to K ; Current subarray can contribute to the answer only if it is odd ; Return the answer ; Driver code ; Input ; Function call
using System ; using System . Collections . Generic ; class GFG { static int noOfWays ( int N , int K ) { string S = N . ToString ( ) ; int ans = 1 ; for ( int i = 1 ; i < S . Length ; i ++ ) { int count = 1 ; while ( i < S . Length && ( int ) S [ i ] - 48 + ( int ) S [ i - 1 ] - 48 == K ) { count ++ ; i ++ ; } if ( count % 2 == 1 ) ans *= ( count + 1 ) / 2 ; } return ans ; } public static void Main ( ) { int N = 1454781 ; int K = 9 ; Console . Write ( noOfWays ( N , K ) ) ; } }