codeparrot/xlcost-text-to-code huggingface dataset:
Print all paths of the Binary Tree with maximum element in each path greater than or equal to K | C # program to print paths with maximum element in the path greater than K ; A Binary Tree node ; A utility function to create a new node ; A recursive function to print the paths whose maximum element is greater than or equal to K . ; If the current node value is greater than or equal to k , then all the subtrees following that node will get printed , flag = 1 indicates to print the required path ; If the leaf node is encountered , then the path is printed if the size of the path vector is greater than 0 ; Append the node to the path vector ; Recur left and right subtrees ; Backtracking to return the vector and print the path if the flag is 1 ; Function to initialize the variables and call the utility function to print the paths with maximum values greater than or equal to K ; Initialize flag ; ans is used to check empty condition ; Call function that print path ; If the path doesn 't exist ; Driver code ; Constructing the following tree : 10 / \ 5 8 / \ / \ 29 2 1 98 / \ 20 50
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left , right ; } ; static int ans ; static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return ( newNode ) ; } static void findPathUtil ( Node root , int k , List < int > path , int flag ) { if ( root == null ) return ; if ( root . data >= k ) flag = 1 ; if ( root . left == null && root . right == null ) { if ( flag == 1 ) { ans = 1 ; Console . Write ( " ( " ) ; for ( int i = 0 ; i < path . Count ; i ++ ) { Console . Write ( path [ i ] + " , ▁ " ) ; } Console . Write ( root . data + " ) , ▁ " ) ; } return ; } path . Add ( root . data ) ; findPathUtil ( root . left , k , path , flag ) ; findPathUtil ( root . right , k , path , flag ) ; path . RemoveAt ( path . Count - 1 ) ; } static void findPath ( Node root , int k ) { int flag = 0 ; ans = 0 ; List < int > v = new List < int > ( ) ; findPathUtil ( root , k , v , flag ) ; if ( ans == 0 ) Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int K = 25 ; Node root = newNode ( 10 ) ; root . left = newNode ( 5 ) ; root . right = newNode ( 8 ) ; root . left . left = newNode ( 29 ) ; root . left . right = newNode ( 2 ) ; root . right . right = newNode ( 98 ) ; root . right . left = newNode ( 1 ) ; root . right . right . right = newNode ( 50 ) ; root . left . left . left = newNode ( 20 ) ; findPath ( root , K ) ; } }
Tridecagonal Number | C # program to find N - th tridecagonal number ; Function to find N - th tridecagonal number ; Formula to calculate nth tridecagonal number ; Driver Code
using System ; class GFG { static int Tridecagonal_num ( int n ) { return ( 11 * n * n - 9 * n ) / 2 ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( Tridecagonal_num ( n ) + " STRNEWLINE " ) ; n = 10 ; Console . Write ( Tridecagonal_num ( n ) + " STRNEWLINE " ) ; } }
Check if a given Binary Tree is Heap | C # program to checks if a binary tree is max heap or not ; Tree node structure ; To add a new node ; Driver code ; Function call
using System ; using System . Collections . Generic ; public class GFG { public class Node { public int data ; public Node left ; public Node right ; } ; static Node newNode ( int k ) { Node node = new Node ( ) ; node . data = k ; node . right = node . left = null ; return node ; } static bool isHeap ( Node root ) { Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; bool nullish = false ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; if ( temp . left != null ) { if ( nullish temp . left . data >= temp . data ) { return false ; } q . Enqueue ( temp . left ) ; } else { nullish = true ; } if ( temp . right != null ) { if ( nullish temp . right . data >= temp . data ) { return false ; } q . Enqueue ( temp . right ) ; } else { nullish = true ; } } return true ; } public static void Main ( String [ ] args ) { Node root = null ; root = newNode ( 10 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 8 ) ; root . left . left = newNode ( 7 ) ; root . left . right = newNode ( 6 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 4 ) ; root . left . left . left = newNode ( 3 ) ; root . left . left . right = newNode ( 2 ) ; root . left . right . left = newNode ( 1 ) ; if ( isHeap ( root ) ) Console . Write ( " Given ▁ binary ▁ tree ▁ is ▁ a ▁ Heap STRNEWLINE " ) ; else Console . Write ( " Given ▁ binary ▁ tree ▁ is ▁ not ▁ a ▁ Heap STRNEWLINE " ) ; } }
Number of N digit integers with weight W | C # program to find total possible numbers with n digits and weight w ; Function to find total possible numbers with n digits and weight w ; When Weight of an integer is Positive ; Subtract the weight from 9 ; When weight of an integer is negative ; add the weight to 10 to make it positive ; Driver code ; number of digits in an integer and w as weight ; print the total possible numbers with n digits and weight w
using System ; class GFG { static int findNumbers ( int n , int w ) { int x = 0 , sum = 0 ; if ( w >= 0 && w <= 8 ) { x = 9 - w ; } else if ( w >= - 9 && w <= - 1 ) { x = 10 + w ; } sum = ( int ) Math . Pow ( 10 , n - 2 ) ; sum = ( x * sum ) ; return sum ; } static public void Main ( ) { int n , w ; n = 3 ; w = 4 ; Console . WriteLine ( findNumbers ( n , w ) ) ; } }
Maximum height of triangular arrangement of array values | C # program to find the maximum height of Pyramidal Arrangement of array values ; Just checking whether ith level is possible or not if possible then we must have atleast ( i * ( i + 1 ) ) / 2 elements in the array ; updating the result value each time ; otherwise we have exceeded n value ; Driver Code
using System ; class GFG { static int MaximumHeight ( int [ ] a , int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { int y = ( i * ( i + 1 ) ) / 2 ; if ( y < n ) result = i ; else break ; } return result ; } static public void Main ( ) { int [ ] arr = { 40 , 100 , 20 , 30 } ; int n = arr . Length ; Console . WriteLine ( MaximumHeight ( arr , n ) ) ; } }
k | C # program to find k - th element in the Odd - Even sequence . ; insert all the odd numbers from 1 to n . ; insert all the even numbers from 1 to n . ; Driver code
using System ; using System . Collections ; class GFG { static int findK ( int n , int k ) { ArrayList a = new ArrayList ( n ) ; for ( int i = 1 ; i < n ; i ++ ) if ( i % 2 == 1 ) a . Add ( i ) ; for ( int i = 1 ; i < n ; i ++ ) if ( i % 2 == 0 ) a . Add ( i ) ; return ( int ) ( a [ k - 1 ] ) ; } static void Main ( ) { int n = 10 , k = 3 ; Console . WriteLine ( findK ( n , k ) ) ; } }
One line function for factorial of a number | C # program to find factorial of given number ; Function to calculate factorial ; single line to find factorial
using System ; class GFG { static int factorial ( int n ) { return ( n == 1 n == 0 ) ? 1 : n * factorial ( n - 1 ) ; } public static void Main ( ) { int num = 5 ; Console . WriteLine ( " Factorial ▁ of ▁ " + num + " ▁ is ▁ " + factorial ( num ) ) ; } }
Pell Number | Iterative Pell Number Series in C # ; calculate nth pell number ; Driver function
using System ; class PellNumber { public static int pell ( int n ) { if ( n <= 2 ) return n ; int a = 1 ; int b = 2 ; int c ; for ( int i = 3 ; i <= n ; i ++ ) { c = 2 * b + a ; a = b ; b = c ; } return b ; } public static void Main ( ) { int n = 4 ; Console . Write ( pell ( n ) ) ; } }
An efficient way to check whether n | A simple C # program to check if n - th Fibonacci number is multiple of 10. ; Returns true if n - th Fibonacci number is multiple of 10. ; main function
using System ; class GFG { static bool isMultipleOf10 ( int n ) { if ( n % 15 == 0 ) return true ; return false ; } public static void Main ( ) { int n = 30 ; if ( isMultipleOf10 ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Find politeness of a number | C # program to find politeness of a number . ; A function to count all odd prime factors of a given number n ; Eliminate all even prime factor of number of n ; n must be odd at this point , so iterate for only odd numbers till sqrt ( n ) ; if i divides n , then start counting of Odd divisors ; If n odd prime still remains then count it ; Driver code
using System ; public class GFG { static int countOddPrimeFactors ( int n ) { int result = 1 ; while ( n % 2 == 0 ) n /= 2 ; for ( int i = 3 ; i * i <= n ; i += 2 ) { int divCount = 0 ; while ( n % i == 0 ) { n /= i ; ++ divCount ; } result *= divCount + 1 ; } if ( n > 2 ) result *= 2 ; return result ; } static int politness ( int n ) { return countOddPrimeFactors ( n ) - 1 ; } public static void Main ( ) { int n = 90 ; Console . WriteLine ( " Politness ▁ of ▁ " + n + " ▁ = ▁ " + politness ( n ) ) ; n = 15 ; Console . WriteLine ( " Politness ▁ of ▁ " + n + " ▁ = ▁ " + politness ( n ) ) ; } }
Nearest prime less than given number n | C # program to find the nearest prime to n . ; array to store all primes less than 10 ^ 6 ; Utility function of Sieve of Sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x ; This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; eliminate indexes which does not produce primes ; Since 2 is a prime number ; Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; modified binary search to find nearest prime less than N ; base condition is , if we are reaching at left corner or right corner of primes [ ] array then return that corner element because before or after that we don 't have any prime number in  primes array ; now if n is itself a prime so it will be present in primes array and here we have to find nearest prime less than n so we will return primes [ mid - 1 ] ; now if primes [ mid ] < n and primes [ mid + 1 ] > n that mean we reached at nearest prime ; Driver code
using System ; using System . Collections ; class GFG { static int MAX = 1000000 ; static ArrayList primes = new ArrayList ( ) ; static void Sieve ( ) { int n = MAX ; int nNew = ( int ) Math . Sqrt ( n ) ; int [ ] marked = new int [ n / 2 + 500 ] ; for ( int i = 1 ; i <= ( nNew - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= n / 2 ; j = j + 2 * i + 1 ) marked [ j ] = 1 ; primes . Add ( 2 ) ; for ( int i = 1 ; i <= n / 2 ; i ++ ) if ( marked [ i ] == 0 ) primes . Add ( 2 * i + 1 ) ; } static int binarySearch ( int left , int right , int n ) { if ( left <= right ) { int mid = ( left + right ) / 2 ; if ( mid == 0 mid == primes . Count - 1 ) return ( int ) primes [ mid ] ; if ( ( int ) primes [ mid ] == n ) return ( int ) primes [ mid - 1 ] ; if ( ( int ) primes [ mid ] < n && ( int ) primes [ mid + 1 ] > n ) return ( int ) primes [ mid ] ; if ( n < ( int ) primes [ mid ] ) return binarySearch ( left , mid - 1 , n ) ; else return binarySearch ( mid + 1 , right , n ) ; } return 0 ; } static void Main ( ) { Sieve ( ) ; int n = 17 ; Console . WriteLine ( binarySearch ( 0 , primes . Count - 1 , n ) ) ; } }
Program for factorial of a number | C # program to find factorial of given number ; method to find factorial of given number ; Driver method
using System ; class Test { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } public static void Main ( ) { int num = 5 ; Console . WriteLine ( " Factorial ▁ of ▁ " + num + " ▁ is ▁ " + factorial ( 5 ) ) ; } }
Turn off the rightmost set bit | Set 2 | C # program to unset the rightmost set bit ; Unsets the rightmost set bit of n and returns the result ; Driver Code
using System ; class GFG { static int FlipBits ( int n ) { return n -= ( n & ( - n ) ) ; } public static void Main ( String [ ] args ) { int N = 12 ; Console . Write ( " The ▁ number ▁ after " + " unsetting ▁ the ▁ " ) ; Console . Write ( " rightmost ▁ set ▁ bit : ▁ " + FlipBits ( N ) ) ; } }
Maximum value of XOR among all triplets of an array | C # implementation of the approach ; function to count maximum XOR value for a triplet ; set is used to avoid repetitions ; store all possible unique XOR value of pairs ; store maximum value ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void Maximum_xor_Triplet ( int n , int [ ] a ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { s . Add ( a [ i ] ^ a [ j ] ) ; } } int ans = 0 ; foreach ( int i in s ) { for ( int j = 0 ; j < n ; j ++ ) { ans = Math . Max ( ans , i ^ a [ j ] ) ; } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 3 , 8 , 15 } ; int n = a . Length ; Maximum_xor_Triplet ( n , a ) ; } }
Find missing elements of a range | A sorting based Java program to find missing elements from an array ; Print all elements of range [ low , high ] that are not present in arr [ 0. . n - 1 ] ; Do binary search for ' low ' in sorted array and find index of first element which either equal to or greater than low . ; Start from the found index and linearly search every range element x after this index in arr [ ] ; If x doesn 't math with current  element print it ; If x matches , move to next element in arr [ ] ; Move to next element in range [ low , high ] ; Print range elements thar are greater than the last element of sorted array . ; Utility function to find ceil index of given element ; Driver Code
using System ; class GFG { static void printMissing ( int [ ] ar , int low , int high ) { Array . Sort ( ar ) ; int index = ceilindex ( ar , low , 0 , ar . Length - 1 ) ; int x = low ; while ( index < ar . Length && x <= high ) { if ( ar [ index ] != x ) { Console . Write ( x + " ▁ " ) ; } else index ++ ; x ++ ; } while ( x <= high ) { Console . Write ( x + " ▁ " ) ; x ++ ; } } static int ceilindex ( int [ ] ar , int val , int low , int high ) { if ( val < ar [ 0 ] ) return 0 ; if ( val > ar [ ar . Length - 1 ] ) return ar . Length ; int mid = ( low + high ) / 2 ; if ( ar [ mid ] == val ) return mid ; if ( ar [ mid ] < val ) { if ( mid + 1 < high && ar [ mid + 1 ] >= val ) return mid + 1 ; return ceilindex ( ar , val , mid + 1 , high ) ; } else { if ( mid - 1 >= low && ar [ mid - 1 ] < val ) return mid ; return ceilindex ( ar , val , low , mid - 1 ) ; } } static public void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 4 } ; int low = 1 , high = 10 ; printMissing ( arr , low , high ) ; } }
Find missing elements of a range | An array based C # program to find missing elements from an array ; Print all elements of range [ low , high ] that are not present in arr [ 0. . n - 1 ] ; Create boolean array of size high - low + 1 , each index i representing wether ( i + low ) th element found or not . ; If ith element of arr is in range low to high then mark corresponding index as true in array ; Traverse through the range and print all elements whose value is false ; Driver code
using System ; class GFG { static void printMissing ( int [ ] arr , int n , int low , int high ) { bool [ ] points_of_range = new bool [ high - low + 1 ] ; for ( int i = 0 ; i < high - low + 1 ; i ++ ) points_of_range [ i ] = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( low <= arr [ i ] && arr [ i ] <= high ) points_of_range [ arr [ i ] - low ] = true ; } for ( int x = 0 ; x <= high - low ; x ++ ) { if ( points_of_range [ x ] == false ) Console . Write ( " { 0 } ▁ " , low + x ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 4 } ; int n = arr . Length ; int low = 1 , high = 10 ; printMissing ( arr , n , low , high ) ; } }
Find missing elements of a range | A hashing based C # program to find missing elements from an array ; Print all elements of range [ low , high ] that are not present in arr [ 0. . n - 1 ] ; Insert all elements of arr [ ] in set ; Traverse throught the range an print all missing elements ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void printMissing ( int [ ] arr , int n , int low , int high ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { s . Add ( arr [ i ] ) ; } for ( int x = low ; x <= high ; x ++ ) if ( ! s . Contains ( x ) ) Console . Write ( x + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 4 } ; int n = arr . Length ; int low = 1 , high = 10 ; printMissing ( arr , n , low , high ) ; } }
k | C # program to find the k - th missing element in a given sequence ; Returns k - th missing element . It returns - 1 if no k is more than number of missing elements . ; Insert all elements of givens sequence b [ ] . ; Traverse through increasing sequence and keep track of count of missing numbers . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int find ( int [ ] a , int [ ] b , int k , int n1 , int n2 ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n2 ; i ++ ) s . Add ( b [ i ] ) ; int missing = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( ! s . Contains ( a [ i ] ) ) missing ++ ; if ( missing == k ) return a [ i ] ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] a = { 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 15 } ; int [ ] b = { 4 , 10 , 6 , 8 , 12 } ; int n1 = a . Length ; int n2 = b . Length ; int k = 3 ; Console . WriteLine ( find ( a , b , k , n1 , n2 ) ) ; } }
Minimize steps to form string S from any random string of length K using a fixed length subsequences | C # program for the above approach ; Function to find the minimum number of string required to generate the original string ; Stores the frequency of each character of string S ; Iterate over the range [ 0 , 25 ] ; Stores the frequency of each character of string S ; Count unique characters in S ; If unique characters is greater then N , then return - 1 ; Otherwise ; Perform Binary Search ; Find the value of mid ; Iterate over the range [ 0 , 26 ] ; If the amount [ i ] is greater than 0 ; Update the ranges ; Find the resultant string ; Generate the subsequence ; If the length of resultant string is less than N than add a character ' a ' ; Print the string ; Driver Code
using System ; class GFG { static void findString ( string S , int N ) { int [ ] amounts = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { amounts [ i ] = 0 ; } for ( int i = 0 ; i < S . Length ; i ++ ) { amounts [ ( int ) ( S [ i ] - 97 ) ] ++ ; } int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( amounts [ i ] > 0 ) count ++ ; } if ( count > N ) { Console . Write ( " - 1" ) ; } else { string ans = " " ; int high = 100001 ; int low = 0 ; int mid , total ; while ( ( high - low ) > 1 ) { total = 0 ; mid = ( high + low ) / 2 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( amounts [ i ] > 0 ) { total += ( amounts [ i ] - 1 ) / mid + 1 ; } } if ( total <= N ) { high = mid ; } else { low = mid ; } } Console . Write ( high + " ▁ " ) ; total = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( amounts [ i ] > 0 ) { total += ( amounts [ i ] - 1 ) / high + 1 ; for ( int j = 0 ; j < ( ( amounts [ i ] - 1 ) / high + 1 ) ; j ++ ) { ans += ( char ) ( i + 97 ) ; } } } for ( int i = total ; i < N ; i ++ ) { ans += ' a ' ; } string reverse = " " ; int Len = ans . Length - 1 ; while ( Len >= 0 ) { reverse = reverse + ans [ Len ] ; Len -- ; } Console . Write ( reverse ) ; } } public static void Main ( ) { string S = " toffee " ; int K = 4 ; findString ( S , K ) ; } }
Find the first repeating element in an array of integers | C # program to find first repeating element in arr [ ] ; This function prints the first repeating element in arr [ ] ; Initialize index of first repeating element ; Creates an empty hashset ; Traverse the input array from right to left ; If element is already in hash set , update min ; Else add element to hash set ; Print the result ; Driver method to test above method
using System ; using System . Collections . Generic ; public class GFG { public static void printFirstRepeating ( int [ ] arr ) { int min = - 1 ; HashSet < int > set = new HashSet < int > ( ) ; for ( int i = arr . Length - 1 ; i >= 0 ; i -- ) { if ( set . Contains ( arr [ i ] ) ) { min = i ; } else { set . Add ( arr [ i ] ) ; } } if ( min != - 1 ) { Console . WriteLine ( " The ▁ first ▁ repeating ▁ element ▁ is ▁ " + arr [ min ] ) ; } else { Console . WriteLine ( " There ▁ are ▁ no ▁ repeating ▁ elements " ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; printFirstRepeating ( arr ) ; } }
Find the first repeating element in an array of integers | C # program to find first repeating element in arr [ ] ; This function prints the first repeating element in arr [ ] ; This will set k = 1 , if any repeating element found ; max = maximum from ( all elements & n ) ; Array a is for storing 1 st time occurence of element initialized by 0 ; Store 1 in array b if element is duplicate initialized by 0 ; Duplicate element found ; storing 1 st occurence of arr [ i ] ; trace array a & find repeating element with min index ; Driver code
using System ; class GFG { static void printFirstRepeating ( int [ ] arr , int n ) { int k = 0 ; int max = n ; for ( int i = 0 ; i < n ; i ++ ) if ( max < arr [ i ] ) max = arr [ i ] ; int [ ] a = new int [ max + 1 ] ; int [ ] b = new int [ max + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ arr [ i ] ] != 0 ) { b [ arr [ i ] ] = 1 ; k = 1 ; continue ; } else a [ arr [ i ] ] = i ; } if ( k == 0 ) Console . WriteLine ( " No ▁ repeating ▁ element ▁ found " ) ; else { int min = max + 1 ; for ( int i = 0 ; i < max + 1 ; i ++ ) if ( ( a [ i ] != 0 ) && min > a [ i ] && ( b [ i ] != 0 ) ) min = a [ i ] ; Console . Write ( arr [ min ] ) ; } Console . WriteLine ( ) ; } static void Main ( ) { int [ ] arr = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; int n = arr . Length ; printFirstRepeating ( arr , n ) ; } }
k | C # program to print k - th distinct element in a given array ; Returns k - th distinct element in arr ; Check if current element is present somewhere else . ; If element is unique ; Driver code
using System ; class GFG { static int printKDistinct ( int [ ] arr , int n , int k ) { int dist_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && arr [ j ] == arr [ i ] ) break ; if ( j == n ) dist_count ++ ; if ( dist_count == k ) return arr [ i ] ; } return - 1 ; } public static void Main ( ) { int [ ] ar = { 1 , 2 , 1 , 3 , 4 , 2 } ; int n = ar . Length ; int k = 2 ; Console . Write ( printKDistinct ( ar , n , k ) ) ; } }
Count subarrays having an equal count of 0 s and 1 s segregated | C # program for the above approach ; Function to count subarrays having equal count of 0 s and 1 s with all 0 s and all 1 s grouped together ; Stores the count ; Initialize cur with first element ; If the next element is same as the current element ; Increment count ; Update curr ; Iterate over the array count ; Consider the minimum ; Driver code ; Given [ ] arr ; Function Call
using System ; class GFG { static void countSubarrays ( int [ ] A ) { int res = 0 ; int curr = A [ 0 ] ; int [ ] cnt = new int [ A . Length ] ; cnt [ 0 ] = 1 ; for ( int c = 1 ; c < A . Length ; c ++ ) { if ( A == curr ) cnt ++ ; else curr = A ; cnt = 1 ; } for ( int i = 1 ; i < cnt . Length ; i ++ ) { res += Math . Min ( cnt [ i - 1 ] , cnt [ i ] ) ; } Console . WriteLine ( res - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 1 , 0 , 0 , 1 , 0 } ; countSubarrays ( A ) ; } }