codeparrot/xlcost-text-to-code huggingface dataset:
Smallest number not less than N which is divisible by all digits of N | C # program for the above approach ; Function to calculate the LCM ; Function to find the smallest number satisfying given constraints ; LCM value is 1 initially ; Finding the LCM of all non zero digits ; Update the value lcm ; Stores ceil value ; Print the answer ; Driver Code ; Function Call
using System ; class GFG { static int LCM ( int A , int B ) { return ( A * B / __gcd ( A , B ) ) ; } static void findSmallestNumber ( int X ) { int lcm = 1 ; int temp = X ; while ( temp > 0 ) { int last = temp % 10 ; temp /= 10 ; if ( last == 0 ) continue ; lcm = LCM ( lcm , last ) ; } int answer = ( ( X + lcm - 1 ) / lcm ) * lcm ; Console . Write ( answer ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int X = 280 ; findSmallestNumber ( X ) ; } }
Minimum sum of two integers whose product is strictly greater than N | C # program for the above approach ; Function to find the minimum sum of two integers such that their product is strictly greater than N ; Initialise low as 0 and high as 1e9 ; Iterate to find the first number ; Find the middle value ; If mid ^ 2 is greater than equal to A , then update high to mid ; Otherwise update low ; Store the first number ; Again , set low as 0 and high as 1e9 ; Iterate to find the second number ; Find the middle value ; If first number * mid is greater than N then update high to mid ; Else , update low to mid ; Store the second number ; Print the result ; Driver Code ; Function Call
using System ; class GFG { static void minSum ( int N ) { long low = 0 , high = 1000000000 ; while ( low + 1 < high ) { long mid = low + ( high - low ) / 2 ; if ( mid * mid >= N ) { high = mid ; } else { low = mid ; } } long first = high ; low = 0 ; high = 1000000000 ; while ( low + 1 < high ) { long mid = low + ( high - low ) / 2 ; if ( first * mid > N ) { high = mid ; } else { low = mid ; } } long second = high ; Console . WriteLine ( first + second ) ; } static public void Main ( ) { int N = 10 ; minSum ( N ) ; } }
Minimum sum of two integers whose product is strictly greater than N | C # program for the above approach ; Function to find the minimum sum of two integers such that their product is strictly greater than N ; Store the answer using the AP - GP inequality ; Print the answer ; Driver Code ; Function Call
using System ; class GFG { static void minSum ( int N ) { int ans = ( int ) Math . Ceiling ( 2 * Math . Sqrt ( N + 1 ) ) ; Console . WriteLine ( ans ) ; } static public void Main ( ) { int N = 10 ; minSum ( N ) ; } }
Query to find length of the longest subarray consisting only of 1 s | C # Program for the above approach ; Arrays to store prefix sums , suffix and MAX 's node respectively ; Function to conSegment Tree ; MAX array for node MAX ; Array for prefix sum node ; Array for suffix sum node ; Calculate MAX node ; Function to update Segment Tree ; Update at position ; Update sums from bottom to the top of Segment Tree ; Update MAX tree ; Update pref tree ; Update suf tree ; Function to perform given queries ; Stores count of queries ; Traverse each query ; Print longest length of subarray in [ 1 , N ] ; Flip the character ; Driver Code ; Size of array ; Given array ; Given queries ; Number of queries
using System ; class GFG { static readonly int INF = 1000000 ; static int [ ] pref = new int [ 500005 ] ; static int [ ] suf = new int [ 500005 ] ; static int [ ] MAX = new int [ 500005 ] ; static void build ( int [ ] a , int tl , int tr , int v ) { if ( tl == tr ) { MAX [ v ] = a [ tl ] ; pref [ v ] = a [ tl ] ; suf [ v ] = a [ tl ] ; } else { int tm = ( tl + tr ) / 2 ; build ( a , tl , tm , v * 2 ) ; build ( a , tm + 1 , tr , v * 2 + 1 ) ; MAX [ v ] = Math . Max ( MAX [ v * 2 ] , Math . Max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) ; pref [ v ] = Math . Max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == ( tm - tl + 1 ) ? 1 : 0 ) * pref [ v * 2 + 1 ] ) ; suf [ v ] = Math . Max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + suf [ v * 2 ] * ( suf [ 2 * v + 1 ] == ( tr - tm ) ? 1 : 0 ) ) ; } } static void update ( int [ ] a , int pos , int tl , int tr , int v ) { if ( tl > pos tr < pos ) { return ; } if ( tl == tr && tl == pos ) { MAX [ v ] = a [ pos ] ; pref [ v ] = a [ pos ] ; suf [ v ] = a [ pos ] ; } else { int tm = ( tl + tr ) / 2 ; update ( a , pos , tl , tm , v * 2 ) ; update ( a , pos , tm + 1 , tr , v * 2 + 1 ) ; MAX [ v ] = Math . Max ( MAX [ v * 2 ] , Math . Max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) ; pref [ v ] = Math . Max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == ( tm - tl + 1 ) ? 1 : 0 ) * pref [ v * 2 + 1 ] ) ; suf [ v ] = Math . Max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + ( suf [ 2 * v + 1 ] == ( tr - tm ) ? 1 : 0 ) * suf [ v * 2 ] ) ; } } static void solveQueries ( int [ ] arr , int n , int [ , ] Q , int k ) { int cntQuery = Q . GetLength ( 0 ) ; build ( arr , 0 , n - 1 , 1 ) ; for ( int i = 0 ; i < cntQuery ; i ++ ) { if ( Q [ i , 0 ] == 1 ) { Console . Write ( MAX [ 1 ] + " ▁ " ) ; } else { arr [ Q [ i , 1 ] - 1 ] ^= 1 ; update ( arr , Q [ i , 1 ] - 1 , 0 , n - 1 , 1 ) ; } } } public static void Main ( String [ ] args ) { int N = 10 ; int [ ] arr = { 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int [ , ] Q = { { 1 , 0 } , { 2 , 3 } , { 1 , 0 } } ; int K = 3 ; solveQueries ( arr , N , Q , K ) ; } }
Find the array element having maximum frequency of the digit K | C # program for the above approach ; Function to find the count of digits , k in the given number n ; Stores count of occurrence of digit K in N ; Iterate over digits of N ; If current digit is k ; Update count ; Update N ; Utility function to find an array element having maximum frequency of digit k ; Stores frequency of digit K in arr [ i ] ; Stores maximum frequency of digit K in the array ; Stores an array element having maximum frequency of digit k ; Initialize max ; Traverse the array ; Count the frequency of digit k in arr [ i ] ; Update max with maximum frequency found so far ; Update ele ; If there is no array element having digit k in it ; Function to find an array element having maximum frequency of digit k ; Stores an array element having maximum frequency of digit k ; If there is no element found having digit k in it ; Print the element having max frequency of digit k ; Driver Code ; The digit whose max occurrence has to be found ; Given array ; Size of array ; Function Call
using System ; class GFG { static int countFreq ( int N , int K ) { int count = 0 ; while ( N > 0 ) { if ( N % 10 == K ) { count ++ ; } N = N / 10 ; } return count ; } static int findElementUtil ( int [ ] arr , int N , int K ) { int c ; int max ; int ele = 0 ; max = 0 ; for ( int i = 0 ; i < N ; i ++ ) { c = countFreq ( arr [ i ] , K ) ; if ( c > max ) { max = c ; ele = arr [ i ] ; } } if ( max == 0 ) return - 1 ; else return ele ; } static void findElement ( int [ ] arr , int N , int K ) { int ele = findElementUtil ( arr , N , K ) ; if ( ele == - 1 ) Console . Write ( " - 1" ) ; else Console . Write ( ele ) ; } public static void Main ( String [ ] args ) { int K = 3 ; int [ ] arr = { 3 , 77 , 343 , 456 } ; int N = arr . Length ; findElement ( arr , K , N ) ; } }
Minimum removals required to make any interval equal to the union of the given Set | C # implementation of the above approach ; Function to count minimum number of removals required to make an interval equal to the union of the given Set ; Stores the minimum number of removals ; Traverse the Set ; Left Boundary ; Right Boundary ; Stores count of intervals lying within current interval ; Traverse over all remaining intervals ; Check if interval lies within the current interval ; Increase count ; Update minimum removals required ; Driver Code ; Returns the minimum removals
using System ; public class GFG { static int findMinDeletions ( int [ , ] v , int n ) { int minDel = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int L = v [ i , 0 ] ; int R = v [ i , 1 ] ; int Count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ j , 0 ] >= L && v [ j , 1 ] <= R ) { Count += 1 ; } } minDel = Math . Min ( minDel , n - Count ) ; } return minDel ; } public static void Main ( String [ ] args ) { int [ , ] v = { { 1 , 3 } , { 4 , 12 } , { 5 , 8 } , { 13 , 20 } } ; int N = v . GetLength ( 0 ) ; Console . Write ( findMinDeletions ( v , N ) ) ; } }
Maximum even numbers present in any subarray of size K | C # program to implement the above approach ; Function to find the maximum count of even numbers from all the subarrays of size K ; Stores the count of even numbers in a subarray of size K ; Calculate the count of even numbers in the current subarray ; If current element is an even number ; Stores the maximum count of even numbers from all the subarrays of size K ; Traverse remaining subarrays of size K using sliding window technique ; If the first element of the subarray is even ; Update curr ; If i - th element is even increment the count ; Update the answer ; Return answer ; Driver Code ; Size of the input array ; Function call
using System ; class GFG { static int maxEvenints ( int [ ] arr , int N , int M ) { int curr = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( arr [ i ] % 2 == 0 ) curr ++ ; } int ans = curr ; for ( int i = M ; i < N ; i ++ ) { if ( arr [ i - M ] % 2 == 0 ) { curr -- ; } if ( arr [ i ] % 2 == 0 ) curr ++ ; ans = Math . Max ( ans , curr ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 4 , 7 , 6 } ; int M = 3 ; int N = arr . Length ; Console . Write ( maxEvenints ( arr , N , M ) + " STRNEWLINE " ) ; } }
Count numbers up to N having digit D in its octal representation | C # program to implement the above approach ; Function to count the numbers in given range whose octal representation contains atleast digit , d ; Store count of numbers up to n whose octal representation contains digit d ; Iterate over the range [ 1 , n ] ; Calculate digit of i in octal representation ; Check if octal representation of x contains digit d ; Update total ; Update x ; Print the answer ; Driver Code ; Given N and D ; Counts and prints numbers up to N having D as a digit in its octal representation
using System ; class GFG { static void countNumbers ( int n , int d ) { int total = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int x = i ; while ( x > 0 ) { if ( x % 8 == d ) { total ++ ; break ; } x = x / 8 ; } } Console . WriteLine ( total ) ; } static void Main ( ) { int n = 20 , d = 7 ; countNumbers ( n , d ) ; } }
Count number of triplets ( a , b , c ) from first N natural numbers such that a * b + c = N | C # program to implement the above approach ; Function to find the count of triplets ( a , b , c ) with a * b + c = N ; Stores count of triplets of 1 st N natural numbers which are of the form a * b + c = N ; Iterate over the range [ 1 , N ] ; If N is divisible by i ; Update cntTriplet ; Update cntTriplet ; Driver code
using System ; class GFG { static int findCntTriplet ( int N ) { int cntTriplet = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( N % i != 0 ) { cntTriplet += N / i ; } else { cntTriplet += ( N / i ) - 1 ; } } return cntTriplet ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . WriteLine ( findCntTriplet ( N ) ) ; } }
Count divisors which generates same Quotient and Remainder | C # program of the above approach ; Function to calculate the count of numbers such that it gives same quotient and remainder ; Stores divisor of number N . ; Iterate through numbers from 2 to sqrt ( N ) and store divisors of N ; As N is also divisor of itself ; Iterate through divisors ; Checking whether x satisfies the required condition ; Print the count ; Driver Code ; Given N ; Function Call
using System ; class GFG { static void countDivisors ( int n ) { int count = 0 ; int j = 0 ; int [ ] divisor = new int [ n ] ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { divisor [ j ] = i ; j += 1 ; } else { divisor [ j ] = i ; divisor [ j + 1 ] = n / i ; j += 2 ; } } } divisor [ j ] = n ; for ( int i = 0 ; i <= j ; i ++ ) { int x = divisor [ i ] ; x -= 1 ; if ( ( n / x ) == ( n % x ) ) count ++ ; } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int N = 10000000 ; countDivisors ( N ) ; } }
Smallest indexed array element required to be flipped to make sum of array equal to 0 | C # program to implement the above approach ; Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Traverse the given array ; Flip the sign of current element ; Stores the sum of array elements ; Find the sum of the array ; Update sum ; If sum is equal to 0 ; Update pos ; Reset the current element to its original value ; Reset the value of arr [ i ] ; Driver Code
using System ; class GFG { static int smallestIndexArrayElementsFlip ( int [ ] arr , int N ) { int pos = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] *= - 1 ; int sum = 0 ; for ( int j = 0 ; j < N ; j ++ ) { sum += arr [ j ] ; } if ( sum == 0 ) { pos = i ; break ; } else { arr [ i ] *= - 1 ; } } return pos ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , - 5 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( smallestIndexArrayElementsFlip ( arr , N ) ) ; } }
Smallest indexed array element required to be flipped to make sum of array equal to 0 | C # program for above approach ; Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Stores the sum of the array ; Traverse the given array ; Update ArrSum ; Traverse the given array ; If sum of array elements double the value of the current element ; Update pos ; Driver function
using System ; class GFG { static int smallestIndexArrayElementsFlip ( int [ ] arr , int N ) { int pos = - 1 ; int ArrSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ArrSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( 2 * arr [ i ] == ArrSum ) { pos = i ; break ; } } return pos ; } static public void Main ( ) { int [ ] arr = new int [ ] { 1 , 3 , - 5 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( smallestIndexArrayElementsFlip ( arr , N ) ) ; } }
Check if a string can be converted to another given string by removal of a substring | C # program to implement the above approach ; Function to check if S can be converted to T by removing at most one subString from S ; Check if S can be converted to T by removing at most one subString from S ; Stores length of String T ; Stores length of String S ; Iterate over the range [ 0 , M - 1 ] ; Stores Length of the subString { S [ 0 ] , ... , S [ i ] } ; Stores Length of the subString { S [ 0 ] , ... , S [ i ] } ; Stores prefix subString ; Stores suffix subString ; Checking if prefix + suffix == T ; Driver Code ; Given String S and T ; Function call
using System ; public class GFG { static String make_String_S_to_T ( String S , String T ) { bool possible = false ; int M = T . Length ; int N = S . Length ; for ( int i = 0 ; i <= M ; i ++ ) { int prefix_length = i ; int suffix_length = M - i ; String prefix = S . Substring ( 0 , prefix_length ) ; String suffix = S . Substring ( N - suffix_length , suffix_length ) ; if ( ( prefix + suffix ) . Equals ( T ) ) { possible = true ; break ; } } if ( possible ) return " YES " ; else return " NO " ; } public static void Main ( String [ ] args ) { String S = " ababcdcd " ; String T = " abcd " ; Console . Write ( make_String_S_to_T ( S , T ) ) ; } }
Sum of maximum of all subarrays by adding even frequent maximum twice | C # program for the above approach ; Function to calculate sum of maximum of all subarrays ; Stores the sum of maximums ; Traverse the array ; Store the frequency of the maximum element in subarray ; Finding maximum ; Increment frequency by 1 ; If new maximum is obtained ; If frequency of maximum is even , then add 2 * maxNumber . Otherwise , add maxNumber ; Print the sum obtained ; Driver Code ; Function Call
using System ; class GFG { public static void findSum ( int [ ] a ) { int ans = 0 ; for ( int low = 0 ; low < a . Length ; low ++ ) { for ( int high = low ; high < a . Length ; high ++ ) { int count = 0 ; int maxNumber = 0 ; for ( int i = low ; i <= high ; i ++ ) { if ( a [ i ] == maxNumber ) count ++ ; else if ( a [ i ] > maxNumber ) { maxNumber = a [ i ] ; count = 1 ; } } ans += maxNumber * ( ( count % 2 == 0 ) ? 2 : 1 ) ; } } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 4 , 4 , 2 } ; findSum ( arr ) ; } }
Count pairs of indices having equal prefix and suffix sums | C # code for above approach ; Function to find the count of index pairs having equal prefix and suffix sums ; Maps indices with prefix sums ; Traverse the array ; Update prefix sum ; Update frequency in Map ; Traverse the array in reverse ; Update suffix sum ; Check if any prefix sum of equal value exists or not ; Print the obtained count ; Driver code ; Given array ; Given size ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] arr , int n ) { Dictionary < int , int > mp1 = new Dictionary < int , int > ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( mp1 . ContainsKey ( sum ) ) { mp1 . Add ( sum , mp1 [ sum ] + 1 ) ; } else { mp1 . Add ( sum , 1 ) ; } } sum = 0 ; int ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; if ( mp1 . ContainsKey ( sum ) ) { ans += mp1 [ sum ] ; } } Console . Write ( ans ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 1 , 1 } ; int n = arr . Length ; countPairs ( arr , n ) ; } }
Queries to count Composite Magic Numbers from a given range [ L , R ] | C # program to implement the above approach ; Check if a number is magic number or not ; Check number is composite number or not ; Corner cases ; Check if the number is a multiple of 2 or 3 ; Check for multiples of remaining primes ; Function to find Composite Magic Numbers in given ranges ; dp [ i ] : Stores the count of composite Magic numbers up to i ; Traverse in the range [ 1 , 1e5 ) ; Check if number is Composite number as well as a Magic number or not ; Print results for each query ; Driver Code
using System ; class GFG { static bool isMagic ( int num ) { return ( num % 9 == 1 ) ; } static bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static void find ( int [ ] L , int [ ] R , int q ) { int [ ] dp = new int [ 1000005 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; for ( int i = 1 ; i < 1000005 ; i ++ ) { if ( isComposite ( i ) && isMagic ( i ) == true ) { dp [ i ] = dp [ i - 1 ] + 1 ; } else dp [ i ] = dp [ i - 1 ] ; } for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( dp [ R [ i ] ] - dp [ L [ i ] - 1 ] ) ; } public static void Main ( ) { int [ ] L = { 10 , 3 } ; int [ ] R = { 100 , 2279 } ; int Q = 2 ; find ( L , R , Q ) ; } }
Smallest positive integer that divides all array elements to generate quotients with sum not exceeding K | C # program to implement the above approach ; Function to find the smallest positive integer that divides array elements to get the sum <= K ; Stores minimum possible of the smallest positive integer satisfying the condition ; Stores maximum possible of the smallest positive integer satisfying the condition ; Apply binary search over the range [ left , right ] ; Stores middle element of left and right ; Stores sum of array elements ; Traverse the array ; Update sum ; If sum is greater than K ; Update left ; Update right ; Driver Code
using System ; using System . Linq ; class GFG { static int findSmallestInteger ( int [ ] arr , int N , int K ) { int left = 1 ; int right = arr . Max ( ) ; while ( left < right ) { int mid = ( left + right ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += ( arr [ i ] + mid - 1 ) / mid ; } if ( sum > K ) { left = mid + 1 ; } else { right = mid ; } } return left ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 5 , 9 } ; int N = arr . Length ; int K = 6 ; Console . Write ( findSmallestInteger ( arr , N , K ) ) ; } }
Minimum Deci | C # program to implement the above approach ; Function to find the count of minimum Deci - Binary numbers required to obtain S ; Stores the minimum count ; Iterate over the string s ; Convert the char to its equivalent integer ; If current character is the maximum so far ; Update the maximum digit ; Print the required result ; Driver Code
using System ; class GFG { static int minimum_deci_binary_number ( string s ) { int m = int . MinValue ; for ( int i = 0 ; i < s . Length ; i ++ ) { int temp = s [ i ] - '0' ; if ( temp > m ) { m = temp ; } } return m ; } public static void Main ( String [ ] args ) { string S = "31" ; Console . WriteLine ( minimum_deci_binary_number ( S ) ) ; } }
Make all the elements of array odd by incrementing odd | C # program for the above approach ; Function to count minimum subarrays whose odd - indexed elements need to be incremented to make array odd ; Stores the minimum number of operations required ; Iterate over even - indices ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment number of operations ; Iterate over odd indexed positions of [ ] arr ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment the number of operations ; Print the number of operations ; Driver Code ; Function Call
using System ; class GFG { static void minOperations ( int [ ] arr , int n ) { int flips = 0 ; for ( int i = 0 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } for ( int i = 1 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } Console . WriteLine ( flips ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 3 , 5 , 3 , 2 } ; int N = arr . Length ; minOperations ( arr , N ) ; } }
Minimum substring reversals required to make given Binary String alternating | C # program for the above approach ; Function to count the minimum number of substrings required to be reversed to make the string S alternating ; Store count of consecutive pairs ; Stores the count of 1 s and 0 s ; Traverse through the string ; Increment 1 s count ; Increment 0 s count ; Increment K if consecutive same elements are found ; Increment 1 s count ; else Increment 0 s count ; Check if it is possible or not ; Otherwise , print the number of required operations ; Driver code ; Function Call
using System ; public class GFG { static int minimumReverse ( String s , int n ) { int k = 0 , l = 0 ; int sum1 = 0 , sum0 = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) sum1 ++ ; else sum0 ++ ; if ( s [ i ] == s [ i - 1 ] && s [ i ] == '0' ) k ++ ; else if ( s [ i ] == s [ i - 1 ] && s [ i ] == '1' ) l ++ ; } if ( s [ 0 ] == '1' ) sum1 ++ ; sum0 ++ ; if ( Math . Abs ( sum1 - sum0 ) > 1 ) return - 1 ; return Math . Max ( k , l ) ; } public static void Main ( String [ ] args ) { String S = "10001" ; int N = S . Length ; Console . Write ( minimumReverse ( S , N ) ) ; } }