codeparrot/xlcost-text-to-code huggingface dataset:
Find a number M < N such that difference between their XOR and AND is maximum | C # program for the above approach ; Function to flip all bits of N ; Finding most significant bit of N ; Calculating required number ; Return the answer ; Driver Code ; Given number ; Function call
using System ; class GFG { static int findM ( int N ) { int M = 0 ; int MSB = ( int ) Math . Log ( N ) ; for ( int i = 0 ; i < MSB ; i ++ ) { if ( ( N & ( 1 << i ) ) == 0 ) M += ( 1 << i ) ; } return M ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( findM ( N ) ) ; } }
Number of containers that can be filled in the given time | C # program for the above problem ; Matrix of containers ; Function to find the number of containers that will be filled in X seconds ; Container on top level ; If container gets filled ; Dividing the liquid equally in two halves ; Driver code
using System ; class GFG { static double [ , ] cont = new double [ 1000 , 1000 ] ; static void num_of_containers ( int n , double x ) { int count = 0 ; cont [ 1 , 1 ] = x ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { if ( cont [ i , j ] >= ( double ) 1 ) { count ++ ; cont [ i + 1 , j ] += ( cont [ i , j ] - ( double ) 1 ) / ( double ) 2 ; cont [ i + 1 , j + 1 ] += ( cont [ i , j ] - ( double ) 1 ) / ( double ) 2 ; } } } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int n = 3 ; double x = 5 ; num_of_containers ( n , x ) ; } }
Check whether there exists a triplet ( i , j , k ) such that arr [ i ] < arr [ k ] < arr [ j ] for i < j < k | C # program for the above approach ; Function to check if there exist triplet in the array such that i < j < k and arr [ i ] < arr [ k ] < arr [ j ] ; Initialize the heights of h1 and h2 to INT_MAX and INT_MIN respectively ; Store the current element as h1 ; If the element at top of stack is less than the current element then pop the stack top and keep updating the value of h3 ; Push the current element on the stack ; If current element is less than h3 , then we found such triplet and return true ; No triplet found , hence return false ; Driver code ; Given array ; Function call
using System ; using System . Collections . Generic ; class GFG { public static bool findTriplet ( int [ ] arr ) { int n = arr . Length ; Stack < int > st = new Stack < int > ( ) ; int h3 = int . MinValue ; int h1 = int . MaxValue ; for ( int i = n - 1 ; i >= 0 ; i -- ) { h1 = arr [ i ] ; while ( st . Count != 0 && st . Peek ( ) < arr [ i ] ) { h3 = st . Peek ( ) ; st . Pop ( ) ; } st . Push ( arr [ i ] ) ; if ( h1 < h3 ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 7 , 5 , 6 } ; if ( findTriplet ( arr ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Minimum number of distinct elements after removing M items | Set 2 | C # program to implement the above approach ; Function to return minimum distinct character after M removals ; Count the occurences of number and store in count ; Count the occurences of the frequencies ; Take answer as total unique numbers and remove the frequency and subtract the answer ; Remove the minimum number of times ; Return the answer ; Driver code ; Initialize array ; Size of array ; Function call
using System ; using System . Collections . Generic ; class GFG { static int distinctNumbers ( int [ ] arr , int m , int n ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( count . ContainsKey ( arr [ i ] ) ) { count [ arr [ i ] ] = count [ arr [ i ] ] + 1 ; } else { count . Add ( arr [ i ] , 1 ) ; } int [ ] fre_arr = new int [ n + 1 ] ; foreach ( int it in count . Values ) { fre_arr [ it ] ++ ; } int ans = count . Count ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; int t = Math . Min ( temp , m / i ) ; ans -= t ; m -= i * t ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 } ; int n = arr . Length ; int m = 2 ; Console . WriteLine ( distinctNumbers ( arr , m , n ) ) ; } }
Find minimum moves to bring all elements in one cell of a matrix | C # implementation to find the minimum number of moves to bring all non - zero element in one cell of the matrix ; Function to find the minimum number of moves to bring all elements in one cell of matrix ; Moves variable to store the sum of number of moves ; Loop to count the number of the moves ; Condition to check that the current cell is a non - zero element ; Driver code ; Coordinates of given cell ; Given matrix ; Function call
using System ; class GFG { static int M = 4 ; static int N = 5 ; public static void no_of_moves ( int [ , ] Matrix , int x , int y ) { int moves = 0 ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( Matrix [ i , j ] != 0 ) { moves += Math . Abs ( x - i ) ; moves += Math . Abs ( y - j ) ; } } } Console . WriteLine ( moves ) ; } public static void Main ( String [ ] args ) { int x = 3 ; int y = 2 ; int [ , ] Matrix = { { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 1 , 0 , 1 } , { 0 , 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 0 , 0 } } ; no_of_moves ( Matrix , x , y ) ; } }
Check if all array elements can be removed by the given operations | C # program to implement the above approach ; Function to check if it is possible to remove all array elements ; Condition if we can remove all elements from the array ; Driver Code
using System ; class GFG { static void removeAll ( int [ ] arr , int n ) { if ( arr [ 0 ] < arr [ n - 1 ] ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } public static void Main ( String [ ] args ) { int [ ] Arr = { 10 , 4 , 7 , 1 , 3 , 6 } ; int size = Arr . Length ; removeAll ( Arr , size ) ; } }
Smallest subarray having an element with frequency greater than that of other elements | C # program for the above approach ; Function to find subarray ; If the array has only one element , then there is no answer . ; Array to store the last occurrences of the elements of the array . ; To maintain the length ; Variables to store start and end indices ; Check if element is occurring for the second time in the array ; Find distance between last and current index of the element . ; If the current distance is less then len update len and set ' start ' and ' end ' ; Set the last occurrence of current element to be ' i ' . ; If flag is equal to 0 , it means there is no answer . ; Driver code
using System ; class GFG { public static void FindSubarray ( int [ ] arr , int n ) { if ( n == 1 ) { Console . WriteLine ( " No ▁ such ▁ subarray ! " ) ; } int [ ] vis = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) vis [ i ] = - 1 ; vis [ arr [ 0 ] ] = 0 ; int len = int . MaxValue , flag = 0 ; int start = 0 , end = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int t = arr [ i ] ; if ( vis [ t ] != - 1 ) { int distance = i - vis [ t ] + 1 ; if ( distance < len ) { len = distance ; start = vis [ t ] ; end = i ; } flag = 1 ; } vis [ t ] = i ; } if ( flag == 0 ) Console . WriteLine ( " No ▁ such ▁ subarray ! " ) ; else { for ( int i = start ; i <= end ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 2 , 4 , 5 } ; int n = arr . Length ; FindSubarray ( arr , n ) ; } }
Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 | C # implementation to find the substrings in binary string such that every character belongs to a palindrome ; Function to to find the substrings in binary string such that every character belongs to a palindrome ; Total substrings ; Loop to store the count of continious characters in the given string ; Subtract non special strings from answer ; Driver code ; Given string s ; Function call
using System ; using System . Collections . Generic ; class GFG { public static int countSubstrings ( String s ) { int n = s . Length ; int answer = ( n * ( n - 1 ) ) / 2 ; int cnt = 1 ; List < int > v = new List < int > ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) cnt ++ ; else { v . Add ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . Add ( cnt ) ; for ( int i = 0 ; i < v . Count - 1 ; i ++ ) { answer -= ( v [ i ] + v [ i + 1 ] - 1 ) ; } return answer ; } public static void Main ( String [ ] args ) { String s = "00111" ; Console . Write ( countSubstrings ( s ) ) ; } }
Minimum minutes needed to make the time palindromic | C # program for the above approach ; Function to get the required minutes ; Storing hour and minute value in integral form ; Keep iterating till first digit hour becomes equal to second digit of minute and second digit of hour becomes equal to first digit of minute ; If mins is 60 , increase hour , and reinitilialized to 0 ; If hours is 60 , reinitialized to 0 ; Return the required time ; Driver code ; Given Time as a string ; Function Call
using System ; class GFG { public static int get_palindrome_time ( string str ) { int hh , mm ; hh = ( str [ 0 ] - 48 ) * 10 + ( str [ 1 ] - 48 ) ; mm = ( str [ 3 ] - 48 ) * 10 + ( str [ 4 ] - 48 ) ; int requiredTime = 0 ; while ( hh % 10 != mm / 10 hh / 10 != mm % 10 ) { ++ mm ; if ( mm == 60 ) { mm = 0 ; ++ hh ; } if ( hh == 24 ) hh = 0 ; ++ requiredTime ; } return requiredTime ; } public static void Main ( string [ ] args ) { string str = "05:39" ; Console . Write ( get_palindrome_time ( str ) ) ; } }
Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed | C # implementation to find the maximum sum of K subarrays when shuffling is allowed ; Function to find the maximum sum of all subarrays ; Initialize maxsum and prefixArray ; Find the frequency using prefix Array ; Perform prefix sum ; Sort both arrays to get a greedy result ; Finally multiply largest frequency with largest array element . ; Return the answer ; Driver Code ; Initial Array ; Subarrays ; Function call
using System ; using System . Collections . Generic ; class GFG { static int maximumSubarraySum ( int [ ] a , int n , List < List < int > > subarrays ) { int i , maxsum = 0 ; int [ ] prefixArray = new int [ n ] ; for ( i = 0 ; i < subarrays . Count ; ++ i ) { prefixArray [ subarrays [ i ] [ 0 ] - 1 ] ++ ; prefixArray [ subarrays [ i ] [ 1 ] ] -- ; } for ( i = 1 ; i < n ; i ++ ) { prefixArray [ i ] += prefixArray [ i - 1 ] ; } Array . Sort ( prefixArray ) ; Array . Sort ( a ) ; for ( i = 0 ; i < n ; i ++ ) maxsum += a [ i ] * prefixArray [ i ] ; return maxsum ; } static void Main ( ) { int n = 6 ; int [ ] a = { 4 , 1 , 2 , 1 , 9 , 2 } ; List < List < int > > subarrays = new List < List < int > > ( ) ; subarrays . Add ( new List < int > { 1 , 2 } ) ; subarrays . Add ( new List < int > { 1 , 3 } ) ; subarrays . Add ( new List < int > { 1 , 4 } ) ; subarrays . Add ( new List < int > { 3 , 4 } ) ; Console . WriteLine ( maximumSubarraySum ( a , n , subarrays ) ) ; } }
Maximum profit such that total stolen value is less than K to get bonus | C # implementation to find the maximum stolen value such that total stolen value is less than K ; Function to find the maximum profit from the given values ; Iterating over every possible permutation ; Driver code ; Function call
using System ; class GFG { static int maxProfit ( int [ ] value , int N , int K ) { Array . Sort ( value ) ; int maxval = value [ N - 1 ] ; int maxProfit = 0 ; int curr_val ; do { curr_val = 0 ; for ( int i = 0 ; i < N ; i ++ ) { curr_val += value [ i ] ; if ( curr_val <= K ) { maxProfit = Math . Max ( curr_val + maxval * ( i + 1 ) , maxProfit ) ; } } } while ( next_permutation ( value ) ) ; return maxProfit ; } static bool next_permutation ( int [ ] p ) { for ( int a = p . Length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . Length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . Length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } static void Main ( ) { int N = 4 , K = 6 ; int [ ] values = { 5 , 2 , 7 , 3 } ; Console . WriteLine ( maxProfit ( values , N , K ) ) ; } }
Min operations to reduce N by multiplying by any number or taking square root | C # implementation of the approach ; Function to reduce N to its minimum possible value by the given operations ; Keep replacing n until is an integer ; Keep replacing n until n is divisible by i * i ; Print the answer ; Driver code ; Given N ; Function call
using System ; class GFG { static void minValue ( int n ) { while ( ( int ) Math . Sqrt ( n ) == Math . Sqrt ( n ) && n > 1 ) { n = ( int ) ( Math . Sqrt ( n ) ) ; } for ( int i = ( int ) ( Math . Sqrt ( n ) ) ; i > 1 ; i -- ) { while ( n % ( i * i ) == 0 ) n /= i ; } Console . Write ( n ) ; } public static void Main ( ) { int N = 20 ; minValue ( N ) ; } }
Count of ways to split given string into two non | C # program to implement the above approach ; Function to check whether the substring from l to r is palindrome or not ; If characters at l and r differ ; Not a palindrome ; If the string is a palindrome ; Function to count and return the number of possible splits ; Stores the count of splits ; Check if the two substrings after the split are palindromic or not ; If both are palindromes ; Print the final count ; Driver Code
using System ; class GFG { public static bool isPalindrome ( int l , int r , string s ) { while ( l <= r ) { if ( s [ l ] != s [ r ] ) return false ; l ++ ; r -- ; } return true ; } public static int numWays ( string s ) { int n = s . Length ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( isPalindrome ( 0 , i , s ) && isPalindrome ( i + 1 , n - 1 , s ) ) { ans ++ ; } } return ans ; } public static void Main ( string [ ] args ) { string S = " aaaaa " ; Console . Write ( numWays ( S ) ) ; } }
Final direction after visiting every cell of Matrix starting from ( 0 , 0 ) | C # program to find the direction when stopped moving ; Function to find the direction when stopped moving ; Driver code ; Given size of NxM grid ; Function Call
using System ; class GFG { static void findDirection ( int n , int m ) { if ( n > m ) { if ( m % 2 == 0 ) Console . Write ( " Up STRNEWLINE " ) ; else Console . Write ( " Down STRNEWLINE " ) ; } else { if ( n % 2 == 0 ) Console . Write ( " Left STRNEWLINE " ) ; else Console . Write ( " Right STRNEWLINE " ) ; } } public static void Main ( ) { int n = 3 , m = 3 ; findDirection ( n , m ) ; } }
Maximize the sum of modulus with every Array element | C # program to find the maximum sum of modulus with every array element ; Function to return the maximum sum of modulus with every array element ; Sum of array elements ; Return the answer ; Driver Code
using System ; class GFG { static int maxModulosum ( int [ ] a , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return sum - n ; } public static void Main ( String [ ] args ) { int [ ] a = new int [ ] { 3 , 4 , 6 } ; int n = a . Length ; Console . Write ( maxModulosum ( a , n ) ) ; } }
Minimum jumps required to group all 1 s together in a given Binary string | C # program to find the minimum number of jumps required to group all ones together in the binary string ; Function to get the minimum jump value ; Store all indices of ones ; Populating one 's indices ; Calculate median ; Jumps required for 1 's  to the left of median ; Jumps required for 1 's  to the right of median ; Return the final answer ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public static int getMinJumps ( string s ) { ArrayList ones = new ArrayList ( ) ; int jumps = 0 , median = 0 , ind = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) ones . Add ( i ) ; } if ( ones . Count == 0 ) return jumps ; median = ( int ) ones [ ones . Count / 2 ] ; ind = median ; for ( int i = ind ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) { jumps += ind - i ; ind -- ; } } ind = median ; for ( int i = ind ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) { jumps += i - ind ; ind ++ ; } } return jumps ; } public static void Main ( string [ ] args ) { string S = "00100000010011" ; Console . Write ( getMinJumps ( S ) ) ; } }
Find GCD of each subtree of a given node in an N | C # program to find GCD of each subtree for a given node by Q queries ; Maximum Number of nodes ; Tree represented as adjacency list ; For storing value associates with node ; For storing GCD of every subarray ; Number of nodes ; Function to find GCD of two numbers using Euclidean algo ; If b == 0 then simply return a ; DFS function to traverse the tree ; Initializing answer with GCD of this node . ; Iterate over each child of current node ; Skipping the parent ; Call DFS for each child ; Taking GCD of the answer of the child to find node 's GCD ; Calling DFS from the root ( 1 ) for precomputing answers ; Function to find and print GCD for Q queries ; Doing preprocessing ; iterate over each given query ; Driver code ; Tree : 1 ( 2 ) / \ 2 ( 3 ) 3 ( 4 ) / \ 4 ( 8 ) 5 ( 16 ) ; Making a undirected tree ; Values associated with nodes ; Function call
using System ; using System . Collections . Generic ; public class GFG { static int N = ( int ) ( 1e5 + 5 ) ; static List < int > [ ] v = new List < int > [ N ] ; static int [ ] val = new int [ N ] ; static int [ ] answer = new int [ N ] ; static int n ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void DFS ( int node , int parent ) { answer [ node ] = val [ node ] ; foreach ( int child in v [ node ] ) { if ( child == parent ) continue ; DFS ( child , node ) ; answer [ node ] = gcd ( answer [ node ] , answer [ child ] ) ; } } static void preprocess ( ) { DFS ( 1 , - 1 ) ; } static void findGCD ( int [ ] queries , int q ) { preprocess ( ) ; for ( int i = 0 ; i < q ; i ++ ) { int GCD = answer [ queries [ i ] ] ; Console . Write ( " For ▁ subtree ▁ of ▁ " + queries [ i ] + " , ▁ GCD ▁ = ▁ " + GCD + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { n = 5 ; for ( int i = 0 ; i < v . Length ; i ++ ) v [ i ] = new List < int > ( ) ; v [ 1 ] . Add ( 2 ) ; v [ 2 ] . Add ( 1 ) ; v [ 1 ] . Add ( 3 ) ; v [ 3 ] . Add ( 1 ) ; v [ 3 ] . Add ( 4 ) ; v [ 4 ] . Add ( 3 ) ; v [ 3 ] . Add ( 5 ) ; v [ 5 ] . Add ( 3 ) ; val [ 1 ] = 2 ; val [ 2 ] = 3 ; val [ 3 ] = 4 ; val [ 4 ] = 8 ; val [ 5 ] = 16 ; int [ ] queries = { 2 , 3 , 1 } ; int q = queries . Length ; findGCD ( queries , q ) ; } }
Minimize cost to convert given two integers to zero using given operations | C # implementation to find the minimum cost to make the two integers equal to zero using given operations ; Function to find out the minimum cost to make two number X and Y equal to zero ; If x is greater than y then swap ; Cost of making y equal to x ; Cost if we choose 1 st operation ; Cost if we choose 2 nd operation ; Total cost ; Driver code
using System ; class GFG { static void makeZero ( int x , int y , int a , int b ) { if ( x > y ) { int temp = x ; x = y ; y = temp ; } int tot_cost = ( y - x ) * a ; int cost1 = 2 * x * a ; int cost2 = x * b ; tot_cost += Math . Min ( cost1 , cost2 ) ; Console . Write ( tot_cost ) ; } public static void Main ( ) { int X = 1 , Y = 3 ; int cost1 = 391 , cost2 = 555 ; makeZero ( X , Y , cost1 , cost2 ) ; } }
Find N fractions that sum upto a given fraction N / D | C # implementation to split the fraction into N parts ; Function to split the fraction into the N parts ; Loop to find the N - 1 fraction ; Loop to print the Fractions ; Driver Code ; Function Call
using System ; class GFG { public static void splitFraction ( int n , int d ) { long [ ] ar = new long [ n ] ; long first = d + n - 1 ; ar [ 0 ] = first ; for ( int i = 1 ; i < n ; i ++ ) { ar [ i ] = first * ( -- first ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( ar [ i ] % n == 0 ) { Console . Write ( "1 / " + ar [ i ] / n + " , ▁ " ) ; } else { Console . Write ( n + " / " + ar [ i ] + " , ▁ " ) ; } } } public static void Main ( String [ ] args ) { int N = 4 ; int D = 2 ; splitFraction ( N , D ) ; } }
Count of pairs ( i , j ) in the array such that arr [ i ] is a factor of arr [ j ] | C # Program to find the number of pairs ( i , j ) such that arr [ i ] is a factor of arr [ j ] ; Function to return the count of Pairs ; Driver code
using System ; class GFG { static int numPairs ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) ans ++ ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 2 , 3 , 3 } ; int n = arr . Length ; Console . Write ( numPairs ( arr , n ) ) ; } }
Check if array can be converted into strictly decreasing sequence | C # implementation to check that array can be converted into a strictly decreasing sequence ; Function to check that array can be converted into a strictly decreasing sequence ; Loop to check that each element is greater than the ( N - index ) ; If element is less than ( N - index ) ; If array can be converted ; Driver Code ; Function calling
using System ; class GFG { static bool check ( int [ ] arr , int n ) { bool flag = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < n - i ) { flag = false ; } } if ( flag ) { return true ; } else { return false ; } } static public void Main ( String [ ] args ) { int [ ] arr1 = { 11 , 11 , 11 , 11 } ; int n1 = arr1 . Length ; if ( check ( arr1 , n1 ) ) { Console . Write ( " Yes " + " STRNEWLINE " ) ; } else { Console . Write ( " No " + " STRNEWLINE " ) ; } } }
Maximum Balanced String Partitions | C # program to find a maximum number X , such that a given String can be partitioned into X subStrings that are each balanced ; Function to find a maximum number X , such that a given String can be partitioned into X subStrings that are each balanced ; If the size of the String is 0 , then answer is zero ; Variable that represents the number of ' R ' s and ' L ' s ; To store maximum number of possible partitions ; Increment the variable r if the character in the String is ' R ' ; Increment the variable l if the character in the String is ' L ' ; If r and l are equal , then increment ans ; Return the required answer ; Driver code ; Function call
using System ; class GFG { static int BalancedPartition ( string str , int n ) { if ( n == 0 ) return 0 ; int r = 0 , l = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' R ' ) { r ++ ; } else if ( str [ i ] == ' L ' ) { l ++ ; } if ( r == l ) { ans ++ ; } } return ans ; } public static void Main ( ) { string str = " LLRRRLLRRL " ; int n = str . Length ; Console . Write ( BalancedPartition ( str , n ) + " STRNEWLINE " ) ; } }
Minimize the non | C # implementation to minimize the non - zero elements in the array ; Function to minimize the non - zero elements in the given array ; To store the min pos needed ; Loop to iterate over the elements of the given array ; If current position A [ i ] is occupied the we can place A [ i ] , A [ i + 1 ] and A [ i + 2 ] elements together at A [ i + 1 ] if exists . ; Driver code ; Function Call
using System ; class GFG { static int minOccupiedPosition ( int [ ] A , int n ) { int minPos = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] > 0 ) { ++ minPos ; i += 2 ; } } return minPos ; } static void Main ( ) { int [ ] A = { 8 , 0 , 7 , 0 , 0 , 6 } ; int n = A . Length ; Console . WriteLine ( minOccupiedPosition ( A , n ) ) ; } }
Find minimum number K such that sum of array after multiplication by K exceed S | C # implementation of the approach ; Function to return the minimum value of k that satisfies the given condition ; Store sum of array elements ; Calculate the sum after ; Return minimum possible K ; Driver code
using System ; class GFG { static int findMinimumK ( int [ ] a , int n , int S ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return ( int ) Math . Ceiling ( ( ( S + 1 ) * 1.0 ) / ( sum * 1.0 ) ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 10 , 7 , 8 , 10 , 12 , 19 } ; int n = a . Length ; int S = 200 ; Console . Write ( findMinimumK ( a , n , S ) ) ; } }