codeparrot/xlcost-text-to-code huggingface dataset:
Minimum replacements required to have at most K distinct elements in the array | C # program to implement the above approach ; Function to find minimum count of array elements required to be replaced such that count of distinct elements is at most K ; Store the frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Store frequency of each distinct element of the array ; Traverse the map ; Stores key of the map ; Insert mp [ i ] into Freq [ ] ; Sort Freq [ ] in descending order ; Stores size of Freq [ ] ; If len is less than or equal to K ; Stores minimum count of array elements required to be replaced such that count of distinct elements is at most K ; Iterate over the range [ K , len ] ; Update cntMin ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int min_elements ( int [ ] arr , int N , int K ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } List < int > Freq = new List < int > ( ) ; foreach ( KeyValuePair < int , int > it in mp ) { int i = it . Key ; Freq . Add ( mp [ i ] ) ; } Freq . Sort ( ) ; Freq . Reverse ( ) ; int len = Freq . Count ; if ( len <= K ) { return 0 ; } int cntMin = 0 ; for ( int i = K ; i < len ; i ++ ) { cntMin += Freq [ i ] ; } return cntMin ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 1 , 3 , 2 , 4 , 1 , 1 , 2 , 3 , 4 } ; int N = arr . Length ; int K = 3 ; Console . Write ( min_elements ( arr , N , K ) ) ; } }
Check if sum of the given array can be reduced to 0 by reducing array elements by K | C # program for the above approach ; Function to check if the sum can be made 0 or not ; Stores sum of array elements ; Traverse the array ; Driver Code ; Given array [ ] arr
using System ; class GFG { static int sumzero ( int [ ] arr , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum == 0 ) Console . Write ( " Yes " ) ; else if ( sum > 0 ) { if ( sum % K == 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } else Console . Write ( " No " ) ; return 0 ; } public static void Main ( String [ ] args ) { int K , N ; int [ ] arr1 = { 1 , - 6 , 2 , 2 } ; K = 1 ; N = arr1 . Length ; sumzero ( arr1 , N , K ) ; } }
Counts 1 s that can be obtained in an Array by performing given operations | C # program to implement the above approach ; Function to count total number of 1 s in array by performing given operations ; Stores count of 1 s in the array by performing the operations ; Iterate over the range [ 1 , N ] ; Flip all array elements whose index is multiple of i ; Update arr [ i ] ; Traverse the array ; If current element is 1 ; Update cntOnes ; Driver Code
using System ; class GFG { static int cntOnesArrWithGivenOp ( int [ ] arr , int N ) { int cntOnes = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i - 1 ; j < N ; j += i ) { arr [ j ] = arr [ j ] == 0 ? 1 : 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOnes += 1 ; } } return cntOnes ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 0 , 0 , 0 , 0 } ; int N = arr . Length ; Console . Write ( cntOnesArrWithGivenOp ( arr , N ) ) ; } }
Counts 1 s that can be obtained in an Array by performing given operations | C # program to implement the above approach ; Function to count total number of 1 s in array by performing the given operations ; Stores count of 1 s in the array by performing the operations ; Update cntOnes ; Driver code
using System ; class GFG { static int cntOnesArrWithGivenOp ( int [ ] arr , int N ) { int cntOnes = 0 ; cntOnes = ( int ) Math . Sqrt ( N ) ; return cntOnes ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 0 , 0 , 0 , 0 } ; int N = arr . Length ; Console . WriteLine ( cntOnesArrWithGivenOp ( arr , N ) ) ; } }
Count Knights that can attack a given pawn in an N * N board | C # program to implement the above approach ; Function to count the knights that are attacking the pawn in an M * M board ; Stores count of knights that are attacking the pawn ; Traverse the knights [ ] [ ] array ; Stores absolute difference of X co - ordinate of i - th knight and pawn ; Stores absolute difference of Y co - ordinate of i - th knight and pawn ; If X is 1 and Y is 2 or X is 2 and Y is 1 ; Update cntKnights ; Driver code ; Stores total count of knights
using System ; class GFG { static int cntKnightsAttackPawn ( int [ , ] knights , int [ ] pawn , int M ) { int cntKnights = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int X = Math . Abs ( knights [ i , 0 ] - pawn [ 0 ] ) ; int Y = Math . Abs ( knights [ i , 1 ] - pawn [ 1 ] ) ; if ( ( X == 1 && Y == 2 ) || ( X == 2 && Y == 1 ) ) { cntKnights ++ ; } } return cntKnights ; } static void Main ( ) { int [ , ] knights = { { 0 , 4 } , { 4 , 5 } , { 1 , 4 } , { 3 , 1 } } ; int [ ] pawn = { 2 , 3 } ; int M = knights . GetLength ( 0 ) ; Console . WriteLine ( cntKnightsAttackPawn ( knights , pawn , M ) ) ; } }
Find the winner of game of removing array elements having GCD equal to 1 | C # program to implement the above approach ; Function to find the winner of the game by removing array elements whose GCD is 1 ; mp [ i ] : Stores count of array elements whose prime factor is i ; Traverse the array ; Calculate the prime factors of arr [ i ] ; If arr [ i ] is divisible by j ; Update mp [ j ] ; Update arr [ i ] ; If arr [ i ] exceeds 1 ; Stores maximum value present in the Map ; Traverse the map ; Update maxCnt ; If n is an even number ; If maxCnt is greater than or equal to n - 1 ; Player 1 wins ; Player 2 wins ; If maxCnt equal to n ; Player 1 wins ; Player 2 wins ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findWinnerGameRemoveGCD ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 2 ; j * j <= arr [ i ] ; j ++ ) { if ( arr [ i ] % j == 0 ) { if ( mp . ContainsKey ( j ) ) { mp [ j ] = mp [ j ] + 1 ; } else { mp . Add ( j , 1 ) ; } while ( arr [ i ] % j == 0 ) { arr [ i ] = arr [ i ] / j ; } } } if ( arr [ i ] > 1 ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } } int maxCnt = 0 ; foreach ( KeyValuePair < int , int > i in mp ) { maxCnt = Math . Max ( maxCnt , i . Value ) ; } if ( n % 2 == 0 ) { if ( maxCnt >= n - 1 ) { Console . Write ( " Player ▁ 1" ) ; } else { Console . Write ( " Player ▁ 2" ) ; } } else { if ( maxCnt == n ) { Console . Write ( " Player ▁ 1" ) ; } else { Console . Write ( " Player ▁ 2" ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 8 } ; int N = arr . Length ; findWinnerGameRemoveGCD ( arr , N ) ; } }
Maximum possible sum of K even multiples of 5 in a given range | C # program to implement the above approach ; Function to find the maximum sum of K even multiples of 5 in the range [ L , R ] ; Store the total number of even multiples of 5 in the range [ L , R ] ; Check if K > N ; If true , print - 1 and return ; Otherwise , divide R by 10 ; Store the sum using the formula ; Print the sum ; Driver code ; Given L , R and K ; Function Call
using System ; class GFG { static void maxksum ( int L , int R , int K ) { int N = ( R / 10 - L / 10 ) + 1 ; if ( K > N ) { Console . Write ( " - 1" ) ; return ; } R = R / 10 ; int X = R - K ; int sum = 10 * ( ( R * ( R + 1 ) ) / 2 - ( X * ( X + 1 ) ) / 2 ) ; Console . Write ( sum ) ; } public static void Main ( ) { int L = 16 , R = 60 , K = 4 ; maxksum ( L , R , K ) ; } }
Minimum swaps of same | C # program to implement the above approach ; Function to count the minimum swaps of same - indexed elements from arrays arr1 [ ] and arr2 [ ] required to make the sum of both the arrays even ; Store the sum of elements of the array arr1 and arr2 respectively ; Store the array sum of both the arrays ; If both sumArr1 and sumArr2 are even , print 0 and return ; If both sumArr1 and sumArr2 are odd and check for a pair with sum odd sum ; Stores if a pair with odd sum exists or not ; Traverse the array ; If a pair exists with odd sum , set flag = 1 ; Print the answer and return ; For all other cases , print - 1 ; Driver code ; Function Call
using System ; class GFG { static void minimumSwaps ( int [ ] arr1 , int [ ] arr2 , int n ) { int sumArr1 = 0 , sumArr2 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { sumArr1 += arr1 [ i ] ; sumArr2 += arr2 [ i ] ; } if ( sumArr1 % 2 == 0 && sumArr2 % 2 == 0 ) { Console . Write ( 0 ) ; return ; } if ( sumArr1 % 2 != 0 && sumArr2 % 2 != 0 ) { int flag = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) { flag = 1 ; break ; } } Console . Write ( flag ) ; return ; } Console . Write ( - 1 ) ; } public static void Main ( ) { int [ ] arr1 = { 11 , 14 , 20 , 2 } ; int [ ] arr2 = { 5 , 9 , 6 , 3 } ; int N = arr1 . Length ; minimumSwaps ( arr1 , arr2 , N ) ; } }
Count of seats booked on each of the given N flights | C # program for the above approach ; Function to find the total of the seats booked in each of the flights ; Stores the resultant sequence ; Traverse the array ; Store the first booked flight ; Store the last booked flight ; Store the total number of seats booked in flights [ l , r ] ; Add K to the flight L ; Subtract K from flight number R + 1 ; Find the prefix sum of the array ; Print the total number of seats booked in each flight ; Driver Code ; Given list of bookings ; Function Call
using System ; class GFG { static void corpFlightBookings ( int [ , ] Bookings , int N ) { int [ ] res = new int [ N ] ; for ( int i = 0 ; i < Bookings . GetLength ( 0 ) ; i ++ ) { int l = Bookings [ i , 0 ] ; int r = Bookings [ i , 1 ] ; int K = Bookings [ i , 2 ] ; res [ l - 1 ] = res [ l - 1 ] + K ; if ( r <= res . Length - 1 ) res [ r ] = ( - K ) + res [ r ] ; } for ( int i = 1 ; i < res . Length ; i ++ ) res [ i ] = res [ i ] + res [ i - 1 ] ; for ( int i = 0 ; i < res . Length ; i ++ ) { Console . Write ( res [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ , ] bookings = { { 1 , 3 , 100 } , { 2 , 6 , 100 } , { 3 , 4 , 100 } } ; int N = 6 ; corpFlightBookings ( bookings , N ) ; } }
Bitwise XOR of all odd numbers from a given range | C # program for the above approach ; Function to calculate Bitwise XOR of odd numbers in the range [ 1 , N ] ; N & 3 is equivalent to n % 4 ; If n is multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to find the XOR of odd numbers less than or equal to N ; If number is even ; Print the answer ; If number is odd ; Print the answer ; Driver Code ; Function Call
using System ; public class GFG { static int findXOR ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; } return 0 ; } static void findOddXOR ( int n ) { if ( n % 2 == 0 ) Console . Write ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n / 2 ) ) ) ) ; else Console . Write ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) / 2 ) ) ) ) ; } public static void Main ( String [ ] args ) { int N = 11 ; findOddXOR ( N ) ; } }
Smallest number greater than or equal to N which is divisible by its non | C # program for the above approach ; Function to find the smallest number greater than or equal to N such that it is divisible by its non - zero digits ; Iterate in range [ N , N + 2520 ] ; To check if the current number satisfies the given condition ; Store the number in a temporary variable ; Loop until temp > 0 ; Check only for non zero digits ; Extract the current digit ; If number is divisible by current digit or not ; Otherwise , set possible to 0 ; Break out of the loop ; Divide by 10 ; Driver code ; Function Call
using System ; class GFG { static void findSmallestNumber ( int n ) { for ( int i = n ; i <= ( n + 2520 ) ; ++ i ) { int possible = 1 ; int temp = i ; while ( temp != 0 ) { if ( temp % 10 != 0 ) { int digit = temp % 10 ; if ( i % digit != 0 ) { possible = 0 ; break ; } } temp /= 10 ; } if ( possible == 1 ) { Console . Write ( i ) ; return ; } } } public static void Main ( String [ ] args ) { int N = 31 ; findSmallestNumber ( N ) ; } }
Print path from a node to root of given Complete Binary Tree | C # program for the above approach ; Function to print the path from node to root ; Iterate until root is reached ; Print the value of the current node ; Move to parent of the current node ; Driver Code
using System ; class GFG { static void path_to_root ( int node ) { while ( node >= 1 ) { Console . Write ( node + " ▁ " ) ; node /= 2 ; } } public static void Main ( String [ ] args ) { int N = 7 ; path_to_root ( N ) ; } }
Smallest number whose sum of digits is N and every digit occurring at most K times | C # program to implement the above approach ; Function to find the smallest number whose sum of digits is N and each digit occurring at most K times ; Maximum sum possible using each digit at most K times ; Append smallest number into the resultant string ; Iterate over the range [ 9 , 1 ] ; If the count of the digit is K , then update count and check for the next digit ; If N is greater than current digit ; Subtract i from N ; Insert i as a digit ; Insert remaining N as a digit ; Increment count ; Reverse the string ; Print the answer ; Driver Code ; Function Call
using System ; class GFG { static void findSmallestNumberPossible ( int N , int K ) { if ( N > 45 * K ) { Console . Write ( " - 1" ) ; return ; } string res = " " ; int count = 0 ; for ( int i = 9 ; i >= 1 ; ) { if ( count == K ) { i -- ; count = 0 ; } if ( N > i ) { N -= i ; res += ( char ) ( '0' + i ) ; } else { res += ( char ) ( '0' + N ) ; N = 0 ; break ; } count ++ ; } string ans = " " ; for ( int i = res . Length - 1 ; i >= 0 ; i -- ) { ans += res [ i ] + " " ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { int N = 25 , K = 3 ; findSmallestNumberPossible ( N , K ) ; } }
Minimum sum of values subtracted from array elements to make all array elements equal | C # program for the above approach ; Function to find the sum of values removed to make all array elements equal ; Stores the minimum of the array ; Stores required sum ; Traverse the array ; Add the value subtracted from the current element ; Return the total sum ; Driver Code ; Function Call
using System ; class GFG { static int minValue ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int minimum = arr [ 0 ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] - minimum ) ; } return sum ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . Length ; Console . WriteLine ( minValue ( arr , N ) ) ; } }
Smallest positive number made up of non | C # program for the above approach ; Function to find smallest positive number made up of non - repeating digits whose sum of its digits is equal to n ; No such number exists ; Stores the required answer ; Store the digit at unit 's place ; Iterate until n > digit ; Push digit at the start of res ; Decrement n by digit ; Decrement digit by 1 ; Push the remaining number as the starting digit ; Print the required number ; Driver Code ; Function Call
using System ; class GFG { static void result ( int n ) { if ( n > 45 ) { Console . Write ( - 1 ) ; return ; } string res = " " ; int digit = 9 ; while ( n > digit ) { res = ( char ) ( '0' + digit ) + res ; n -= digit ; digit -= 1 ; } if ( n > 0 ) { res = ( char ) ( '0' + n ) + res ; } Console . Write ( res ) ; } public static void Main ( ) { int N = 19 ; result ( N ) ; } }
Count pair of integers having even sum | C # program for the above approach ; Function to count even pairs ; Stores count of pairs having even sum ; Stores count of even numbers up to N ; Stores count of odd numbers up to N ; Stores count of even numbers up to M ; Stores count of odd numbers up to M ; Return the count ; Driver Code
using System ; class GFG { public static int countEvenPairs ( int N , int M ) { int count = 0 ; int nEven = ( int ) Math . Floor ( ( double ) N / 2 ) ; int nOdd = ( int ) Math . Ceiling ( ( double ) N / 2 ) ; int mEven = ( int ) Math . Floor ( ( double ) M / 2 ) ; int mOdd = ( int ) Math . Ceiling ( ( double ) M / 2 ) ; count = nEven * mEven + nOdd * mOdd ; return count ; } public static void Main ( String [ ] args ) { int N = 4 ; int M = 6 ; Console . Write ( countEvenPairs ( N , M ) ) ; } }
Generate an N | C # program to implement the above approach ; Function to generate a String of length N having longest palindromic subString of length K ; Fill first K characters with ' a ' ; Stores a non - palindromic sequence to be repeated for N - k slots ; Print N - k remaining characters ; Driver Code ; Given N and K
using System ; class GFG { static void String_palindrome ( int N , int K ) { for ( int i = 0 ; i < K ; i ++ ) Console . Write ( " a " ) ; string s = " bcd " ; for ( int i = 0 ; i < N - K ; i ++ ) Console . Write ( s [ i % 3 ] ) ; } public static void Main ( string [ ] args ) { int N = 5 , K = 3 ; String_palindrome ( N , K ) ; } }
Last remaining character after repeated removal of the first character and flipping of characters of a Binary String | C # program to implement the above approach ; Function to find the last removed character from the String ; Stores length of the String ; Base Case : ; If the second last character is '0' ; If the second last character is '1' ; Driver Code
using System ; class GFG { static char lastRemovedCharacter ( char [ ] str ) { int n = str . Length ; if ( n == 1 ) return str [ 0 ] ; if ( str [ n - 2 ] == '0' ) { return ( char ) ( '1' - str [ n - 1 ] + '0' ) ; } else return str [ n - 1 ] ; } public static void Main ( ) { string str = "10010" ; Console . Write ( lastRemovedCharacter ( str . ToCharArray ( ) ) ) ; } }
Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs | C # code for above approach ; Function to find the triplet with given Bitwise XOR and Bitwise AND values of all possible pairs of the triplet ; Stores values of a triplet ; Stores a + b ; Stores a + c ; Stores b + c ; Calculate aSUMb ; Calculate aSUMc ; Calculate bSUMc ; Calculate a ; Calculate b ; Calculate c ; Print a ; Print b ; Print c ; Driver code
using System ; public class GFG { static void findNumbers ( int aXORb , int aANDb , int aXORc , int aANDc , int bXORc , int bANDc ) { int a , b , c ; int aSUMb ; int aSUMc ; int bSUMc ; aSUMb = aXORb + aANDb * 2 ; aSUMc = aXORc + aANDc * 2 ; bSUMc = bXORc + bANDc * 2 ; a = ( aSUMb - bSUMc + aSUMc ) / 2 ; b = aSUMb - a ; c = aSUMc - a ; System . Console . Write ( " a ▁ = ▁ " + a ) ; System . Console . Write ( " , ▁ b ▁ = ▁ " + b ) ; System . Console . Write ( " , ▁ c ▁ = ▁ " + c ) ; } static public void Main ( ) { int aXORb = 30 , aANDb = 0 , aXORc = 20 , aANDc = 10 , bXORc = 10 , bANDc = 20 ; findNumbers ( aXORb , aANDb , aXORc , aANDc , bXORc , bANDc ) ; } }
Find N distinct numbers whose Bitwise XOR is equal to K | C # program for the above approach ; Function to find N integers having Bitwise XOR equal to K ; Base Cases ; Assign values to P and Q ; Stores Bitwise XOR of the first ( N - 3 ) elements ; Print the first N - 3 elements ; Calculate Bitwise XOR of first ( N - 3 ) elements ; Driver Code ; Function Call
using System ; class GFG { static void findArray ( int N , int K ) { if ( N == 1 ) { Console . Write ( K + " ▁ " ) ; return ; } if ( N == 2 ) { Console . Write ( 0 + " ▁ " + K ) ; return ; } int P = N - 2 ; int Q = N - 1 ; int VAL = 0 ; for ( int i = 1 ; i <= ( N - 3 ) ; i ++ ) { Console . Write ( i + " ▁ " ) ; VAL ^= i ; } if ( VAL == K ) { Console . Write ( P + " ▁ " + Q + " ▁ " + ( P ^ Q ) ) ; } else { Console . Write ( 0 + " ▁ " + P + " ▁ " + ( P ^ K ^ VAL ) ) ; } } public static void Main ( ) { int N = 4 , X = 6 ; findArray ( N , X ) ; } }
Flip consecutive set bits starting from LSB of a given number | C # program to implement the above approach ; Function to find the number after converting 1 s from end to 0 s ; Count of 1 s ; AND operation of N and 1 ; Left shift N by count ; Driver Code ; Function Call
using System ; class GFG { static int findNumber ( int N ) { int count = 0 ; while ( ( N & 1 ) == 1 ) { N = N >> 1 ; count ++ ; } return N << count ; } public static void Main ( ) { int N = 39 ; Console . WriteLine ( findNumber ( N ) ) ; } }
Flip consecutive set bits starting from LSB of a given number | C # program for the above approach ; Function to find the number after converting 1 s from end to 0 s ; Return the logical AND of N and ( N + 1 ) ; Driver Code ; Function Call
using System ; class GFG { static int findNumber ( int N ) { return N & ( N + 1 ) ; } public static void Main ( String [ ] args ) { int N = 39 ; Console . Write ( findNumber ( N ) ) ; } }
Count of substrings of a string containing another given string as a substring | C # program for the above approach ; Function to store all subStrings of S ; Stores the subStrings of S ; Pick start point in outer loop and lengths of different Strings for a given starting point ; Return the array containing subStrings of S ; Function to check if a String is present in another String ; Check if target is in the String str or not ; Function to count the subString of S containing T in it as subString ; Store all subStrings of S in the array v [ ] ; Store required count of subStrings ; Iterate through all the subStrings of S ; If String T is present in the current subString , then increment the ans ; Print the answer ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static List < string > subString ( string s , int n ) { List < string > v = new List < string > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = 1 ; len <= n - i ; len ++ ) { string find = s . Substring ( i , len ) ; v . Add ( find ) ; } } return v ; } static int IsPresent ( string str , string target ) { if ( str . Contains ( target ) ) { return 1 ; } return - 1 ; } static void countSubStrings ( string S , string T ) { List < string > v = subString ( S , S . Length ) ; int ans = 0 ; foreach ( string it in v ) { if ( IsPresent ( it , T ) != - 1 ) { ans ++ ; } } Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { string S = " dabc " ; string T = " ab " ; countSubStrings ( S , T ) ; } }
Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal | C # program for the above approach ; Function to find the minimum flip of subarrays required at alternate index to make binary arrays equals ; Stores count of total operations ; Stores count of consecutive unequal elements ; Loop to run on odd positions ; Incrementing the global counter ; Change count to 0 ; If all last elements are equal ; Loop to run on even positions ; Incrementing the global counter ; Change count to 0 ; Print the minimum operations ; Driver Code ; Function Call
using System ; class GFG { static void minOperation ( int [ ] X , int [ ] Y , int n ) { int C = 0 ; int count = 0 ; for ( int i = 1 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; count = 0 ; for ( int i = 0 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; Console . Write ( C ) ; } public static void Main ( String [ ] args ) { int [ ] X = { 1 , 0 , 0 , 0 , 0 , 1 } ; int [ ] Y = { 1 , 1 , 0 , 1 , 1 , 1 } ; int N = X . Length ; minOperation ( X , Y , N ) ; } }
Minimum subarray reversals required to make given binary array alternating | C # program to implement the above approach ; Function to count minimum subarray reversal operations required to make array alternating ; Stores minimum count of operations required to make array alternating ; Traverse the array ; If arr [ i ] is greater than arr [ i + 1 ] ; Update cntOp ; Driver code
using System ; class GFG { static int minimumcntOperationReq ( int [ ] arr , int N ) { int cntOp = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { cntOp ++ ; } } return ( cntOp + 1 ) / 2 ; } static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 } ; int N = arr . Length ; Console . WriteLine ( minimumcntOperationReq ( arr , N ) ) ; } }