codeparrot/xlcost-text-to-code huggingface dataset:
Disjoint Set Union on trees | Set 1 | C # code to find maximum subtree such that all nodes are even in weight ; Structure for Edge ; ' id ' : stores parent of a node . ' sz ' : stores size of a DSU tree . ; Function to assign root ; Function to find Union ; Utility function for Union ; Edge between ' u ' and ' v ' ; 0 - indexed nodes ; If weights of both ' u ' and ' v ' are even then we make union of them . ; Function to find maximum size of DSU tree ; Driver code ; Weights of nodes ; Number of nodes in a tree ; Initializing every node as a tree with single node . ; Find maximum size of DSU tree .
using System ; class GFG { static int N = 100010 ; public class Edge { public int u , v ; public Edge ( int u , int v ) { this . u = u ; this . v = v ; } } static int [ ] id = new int [ N ] ; static int [ ] sz = new int [ N ] ; static int Root ( int idx ) { int i = idx ; while ( i != id [ i ] ) { id [ i ] = id [ id [ i ] ] ; i = id [ i ] ; } return i ; } static void Union ( int a , int b ) { int i = Root ( a ) , j = Root ( b ) ; if ( i != j ) { if ( sz [ i ] >= sz [ j ] ) { id [ j ] = i ; sz [ i ] += sz [ j ] ; sz [ j ] = 0 ; } else { id [ i ] = j ; sz [ j ] += sz [ i ] ; sz [ i ] = 0 ; } } } static void UnionUtil ( Edge [ ] e , int [ ] W , int q ) { for ( int i = 0 ; i < q ; i ++ ) { int u , v ; u = e [ i ] . u ; v = e [ i ] . v ; u -- ; v -- ; if ( W [ u ] % 2 == 0 && W [ v ] % 2 == 0 ) Union ( u , v ) ; } } static int findMax ( int n , int [ ] W ) { int maxi = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( W [ i ] % 2 == 0 ) maxi = Math . Max ( maxi , sz [ i ] ) ; return maxi ; } public static void Main ( String [ ] args ) { int [ ] W = { 1 , 2 , 6 , 4 , 2 , 0 , 3 } ; int n = W . Length ; for ( int i = 0 ; i < n ; i ++ ) { id [ i ] = i ; sz [ i ] = 1 ; } Edge [ ] e = { new Edge ( 1 , 2 ) , new Edge ( 1 , 3 ) , new Edge ( 2 , 4 ) , new Edge ( 2 , 5 ) , new Edge ( 4 , 6 ) , new Edge ( 6 , 7 ) } ; int q = e . Length ; UnionUtil ( e , W , q ) ; int maxi = findMax ( n , W ) ; Console . Write ( " Maximum ▁ size ▁ of ▁ the ▁ subtree ▁ with ▁ " ) ; Console . WriteLine ( " even ▁ weighted ▁ nodes ▁ = ▁ { 0 } STRNEWLINE " , maxi ) ; } }
Odd numbers in N | C # code to find the count of odd numbers in n - th row of Pascal 's Triangle ; Function to get no of set bits in binary representation of positive integer n ; Count number of 1 's in binary  representation of n. ; Number of odd numbers in n - th row is 2 raised to power the count . ; Driver code
using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int countOfOddsPascal ( int n ) { int c = countSetBits ( n ) ; return ( int ) Math . Pow ( 2 , c ) ; } public static void Main ( ) { int n = 20 ; Console . WriteLine ( countOfOddsPascal ( n ) ) ; } }
Queries on XOR of XORs of all subarrays | C # code for Queries on XOR of XORs of all subarrays ; Output for each query ; If number of element is even . ; If number of element is odd . ; if l is even ; if l is odd ; Wrapper Function ; Evaluating prefixodd and prefixeven ; Driver program to test above function
using System ; class GFG { static void ansQueries ( int [ ] prefeven , int [ ] prefodd , int l , int r ) { if ( ( r - l + 1 ) % 2 == 0 ) Console . WriteLine ( "0" ) ; else { if ( l % 2 == 0 ) Console . WriteLine ( prefeven [ r ] ^ prefeven [ l - 1 ] ) ; else Console . WriteLine ( prefodd [ r ] ^ prefodd [ l - 1 ] ) ; } } static void wrapper ( int [ ] arr , int n , int [ ] l , int [ ] r , int q ) { int [ ] prefodd = new int [ 100 ] ; int [ ] prefeven = new int [ 100 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ( i ) % 2 == 0 ) { prefeven [ i ] = arr [ i - 1 ] ^ prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ; } else { prefeven [ i ] = prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ^ arr [ i - 1 ] ; } } int j = 0 ; while ( j != q ) { ansQueries ( prefeven , prefodd , l [ j ] , r [ j ] ) ; j ++ ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int [ ] l = { 1 , 1 , 2 } ; int [ ] r = { 2 , 3 , 4 } ; int q = l . Length ; wrapper ( arr , n , l , r , q ) ; } }
Variation in Nim Game | C # program for the variation in nim game ; Function to return final grundy Number ( G ) of game ; if pile size is odd ; We XOR pile size + 1 ; We XOR pile size - 1 ; Driver code ; Game with 3 piles ; pile with different sizes ; Function to return result of game ; if ( res == 0 ) if G is zero ; else if G is non zero
using System ; class GFG { static int solve ( int [ ] p , int n ) { int G = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] % 2 != 0 ) G ^= ( p [ i ] + 1 ) ; G ^= ( p [ i ] - 1 ) ; } return G ; } public static void Main ( ) { int n = 3 ; int [ ] p = { 32 , 49 , 58 } ; int res = solve ( p , n ) ; Console . WriteLine ( " Player ▁ 2 ▁ wins " ) ; Console . WriteLine ( " Player ▁ 1 ▁ wins " ) ; } }
Maximum AND value of a pair in an array | C # Program to find maximum XOR value of a pair ; Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Driver Code
using System ; public class GfG { static int checkBit ( int pattern , int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( ( pattern & arr [ i ] ) == pattern ) count ++ ; return count ; } static int maxAND ( int [ ] arr , int n ) { int res = 0 , count ; for ( int bit = 31 ; bit >= 0 ; bit -- ) { count = checkBit ( res | ( 1 << bit ) , arr , n ) ; if ( count >= 2 ) res |= ( 1 << bit ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 4 , 8 , 6 , 2 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ AND ▁ Value ▁ = ▁ " + maxAND ( arr , n ) ) ; } }
Minimum flips to make all 1 s in left and 0 s in right | Set 1 ( Using Bitmask ) | C # program to find minimum flips to make all 1 s in left ; Function to count minimum number of flips ; This is converting string s into integer of base 2 ( if s = '100' then num = 4 ) ; initialize minXor with n that can be maximum number of flips ; right shift 1 by ( n - 1 ) bits ; calculate bitwise XOR of num and mask ; Math . Min ( a , b ) returns minimum of a and b return minimum number of flips till that digit ; Function to count number of 1 s ; Driver code
using System ; class GFG { public static int findMiniFlip ( int [ ] nums ) { int n = nums . Length ; String s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += nums [ i ] ; long num = Convert . ToInt32 ( s , 2 ) ; int minXor = n ; long mask = ( 1 << ( n - 1 ) ) ; while ( n - 1 > 0 ) { long temp = ( num ^ mask ) ; minXor = Math . Min ( minXor , countones ( temp ) ) ; n -- ; mask = ( mask | ( 1 << n - 1 ) ) ; } return minXor ; } public static int countones ( long n ) { int c = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; c ++ ; } return c ; } public static void Main ( String [ ] args ) { int [ ] nums = { 1 , 0 , 1 , 1 , 0 , 0 , 0 } ; int n = findMiniFlip ( nums ) ; Console . WriteLine ( n ) ; } }
Check if a number is power of 8 or not | C # program to check if a number is power of 8 ; function to check if power of 8 ; calculate log8 ( n ) ; check if i is an integer or not ; Driver Code
using System ; class GFG { static bool checkPowerof8 ( int n ) { double i = Math . Log ( n ) / Math . Log ( 8 ) ; return ( i - Math . Floor ( i ) < 0.000001 ) ; } static public void Main ( ) { int n = 65 ; if ( checkPowerof8 ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find the n | C # program to find n - th palindrome ; Utility function which is used to convert binary string into integer ; Convert binary string into integer ; Function to find nth binary palindrome number ; Stores the binary palindrome string ; Base case ; Add 2 nd binary palindrome string ; Runs till the nth binary palindrome number ; Remove curr binary palindrome string from queue ; If n == 0 then we find the n 'th binary  palindrome so we return our answer ; Calculate length of curr binary palindrome string ; If length is even . so it is our first case we have two choices ; If length is odd . so it is our second case we have only one choice ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static int convertStringToInt ( String s ) { int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) ans += 1 << i ; } return ans ; } public static int getNthNumber ( int n ) { Queue < String > q = new Queue < String > ( ) ; if ( n == 1 ) return 1 ; n = n - 1 ; q . Enqueue ( "11" ) ; while ( n -- > 0 ) { String curr = q . Dequeue ( ) ; if ( n == 0 ) return convertStringToInt ( curr ) ; int len = curr . Length ; if ( len % 2 == 0 ) { q . Enqueue ( curr . Substring ( 0 , len / 2 ) + "0" + curr . Substring ( len / 2 ) ) ; q . Enqueue ( curr . Substring ( 0 , len / 2 ) + "1" + curr . Substring ( len / 2 ) ) ; } else { char midChar = curr [ len / 2 ] ; q . Enqueue ( curr . Substring ( 0 , len / 2 ) + midChar + curr . Substring ( len / 2 ) ) ; } } return - 1 ; } public static void Main ( String [ ] args ) { int n = 9 ; Console . WriteLine ( getNthNumber ( n ) ) ; } }
Check if binary representation of a given number and its complement are anagram | An efficient C # program to check if binary representations of a number and it 's complement are anagram. ; Returns true if binary representations of a and b are anagram . ; _popcnt64 ( a ) gives number of 1 's present  in binary representation of a. If number  of 1s is half of total bits, return true. ; Driver code
using System ; class GFG { static byte longSize = 8 ; static int ULL_SIZE = 8 * longSize ; static bool bit_anagram_check ( long a ) { return ( BitCount ( ( int ) a ) == ( ULL_SIZE >> 1 ) ) ; } static int BitCount ( int n ) { int count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } public static void Main ( String [ ] args ) { long a = 4294967295L ; Console . WriteLine ( bit_anagram_check ( a ) ) ; } }
Sum of numbers with exactly 2 bits set | C # program to find sum of numbers upto n whose 2 bits are set ; To calculate sum of numbers ; Find numbers whose 2 bits are set ; If number is greater then n we don 't include this in sum ; Return sum of numbers ; Driver Code
using System ; public class main { static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; 1 << i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { int num = ( 1 << i ) + ( 1 << j ) ; if ( num <= n ) sum += num ; } } return sum ; } public static void Main ( String [ ] args ) { int n = 10 ; Console . WriteLine ( findSum ( n ) ) ; } }
Position of rightmost different bit | C # implementation to find the position of rightmost different bit in two number . ; function to find rightmost different bit in two numbers . ; Driver code
using System ; class GFG { static int posOfRightMostDiffBit ( int m , int n ) { return ( int ) Math . Floor ( Math . Log10 ( Math . Pow ( m ^ n , 2 ) ) ) + 2 ; } public static void Main ( String [ ] args ) { int m = 52 , n = 4 ; Console . Write ( " Position ▁ = ▁ " + posOfRightMostDiffBit ( m , n ) ) ; } }
Set the K | C # implementation to set the kth bit of the given number ; function to set the kth bit ; kth bit of n is being set by this operation ; Driver code
using System ; class GFG { static int setKthBit ( int n , int k ) { return ( ( 1 << k ) n ) ; } public static void Main ( ) { int n = 10 , k = 2 ; Console . Write ( " Kth ▁ bit ▁ set ▁ number ▁ = ▁ " + setKthBit ( n , k ) ) ; } }
Reverse an array without using subtract sign â €˜ | C # program to reverse an array without using " - " sign ; Function to reverse array ; Trick to assign - 1 to a variable ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value ; Drivers code ; print the reversed array
using System ; class GFG { static void reverseArray ( int [ ] arr , int n ) { int x = ( int . MinValue / int . MaxValue ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) swap ( arr , i , n + ( x * i ) + x ) ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void Main ( ) { int [ ] arr = { 5 , 3 , 7 , 2 , 1 , 6 } ; int n = arr . Length ; reverseArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Reverse an array without using subtract sign â €˜ | C # program to reverse an array without using " - " sign ; Function to reverse array ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value Note : A - B = A + ~ B + 1 So n - i = n + ~ i + 1 then n - i - 1 = ( n + ~ i + 1 ) + ~ 1 + 1 ; Driver code ; print the reversed array
using System ; class GFG { static void reverseArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n / 2 ; i ++ ) { swap ( arr , i , ( n + ~ i + 1 ) + ~ 1 + 1 ) ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 7 , 2 , 1 , 6 } ; int n = arr . Length ; reverseArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }
Maximum XOR value of a pair from a range | C # program to get maximum xor value of two numbers in a range ; method to get maximum xor value in range [ L , R ] ; get xor of limits ; loop to get msb position of L ^ R ; construct result by adding 1 , msbPos times ; Driver code
using System ; class Xor { static int maxXORInRange ( int L , int R ) { int LXR = L ^ R ; int msbPos = 0 ; while ( LXR > 0 ) { msbPos ++ ; LXR >>= 1 ; } int maxXOR = 0 ; int two = 1 ; while ( msbPos -- > 0 ) { maxXOR += two ; two <<= 1 ; } return maxXOR ; } public static void Main ( ) { int L = 8 ; int R = 20 ; Console . WriteLine ( maxXORInRange ( L , R ) ) ; } }
Numbers whose bitwise OR and sum with N are equal | C # program to count numbers whose bitwise OR and sum with N are equal ; Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to N , whose bitwise OR and SUM with N are equal . ; count number of zero bit in N ; power of 2 to count ; Driver code
using System ; class GFG { static int CountZeroBit ( int n ) { int count = 0 ; while ( n > 0 ) { if ( n % 2 != 0 ) count ++ ; n >>= 1 ; } return count ; } static int CountORandSumEqual ( int N ) { int count = CountZeroBit ( N ) ; return ( 1 << count ) ; } public static void Main ( ) { int N = 10 ; Console . Write ( CountORandSumEqual ( N ) ) ; } }
Count smaller numbers whose XOR with n produces greater value | C # program to count numbers whose XOR with n produces a value more than n . ; Position of current bit in n ; Traverse bits from LSB ( least significant bit ) to MSB ; Initialize result ; If the current bit is 0 , then there are 2 ^ k numbers with current bit 1 and whose XOR with n produces greater value ; Increase position for next bit ; Reduce n to find next bit ; Driver code
using System ; class GFG { static int countNumbers ( int n ) { int k = 0 ; int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count += ( int ) ( Math . Pow ( 2 , k ) ) ; k += 1 ; n >>= 1 ; } return count ; } public static void Main ( ) { int n = 11 ; Console . WriteLine ( countNumbers ( n ) ) ; } }
Count all pairs with given XOR | C # program to Count all pair with given XOR value x ; Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty set that stores the visiting element of array . Refer below post for details of unordered_set https : www . geeksforgeeks . org / unorderd_set - stl - uses / ; If there exist an element in set s with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Make element visited ; return total count of pairs with XOR equal to x ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int xorPairCount ( int [ ] arr , int n , int x ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . Contains ( x ^ arr [ i ] ) ) { result ++ ; } s . Add ( arr [ i ] ) ; } return result ; } public static void Main ( ) { int [ ] arr = { 5 , 4 , 10 , 15 , 7 , 6 } ; int n = arr . Length ; int x = 5 ; Console . WriteLine ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ XOR ▁ = ▁ " + xorPairCount ( arr , n , x ) ) ; } }
Multiples of 4 ( An Interesting Method ) | An interesting XOR based method to check if a number is multiple of 4. ; Returns true if n is a multiple of 4. ; Find XOR of all numbers from 1 to n ; If XOR is equal n , then return true ; Driver method ; Printing multiples of 4 using above method
using System ; class GFG { static bool isMultipleOf4 ( int n ) { if ( n == 1 ) return false ; int XOR = 0 ; for ( int i = 1 ; i <= n ; i ++ ) XOR = XOR ^ i ; return ( XOR == n ) ; } public static void Main ( ) { for ( int n = 0 ; n <= 42 ; n ++ ) { if ( isMultipleOf4 ( n ) ) Console . Write ( n + " ▁ " ) ; } } }
Multiples of 4 ( An Interesting Method ) | An interesting XOR based method to check if a number is multiple of 4. ; Returns true if n is a multiple of 4. ; Driver code to print multiples of 4 ; Printing multiples of 4 using above method
using System ; class GFG { static bool isMultipleOf4 ( int n ) { if ( n == 0 ) return true ; return ( ( ( n >> 2 ) << 2 ) == n ) ; } static void Main ( ) { for ( int n = 0 ; n <= 42 ; n ++ ) if ( isMultipleOf4 ( n ) ) Console . Write ( n + " ▁ " ) ; } }
Check sum of Covered and Uncovered nodes of Binary Tree | C # program to find sum of covered and uncovered nodes of a binary tree ; A binary tree node has key , pointer to left child and a pointer to right child ; Utility function to calculate sum of all node of tree ; Recursive function to calculate sum of left boundary elements ; If left node , then just return its key value ; If left is available then go left otherwise go right ; Recursive function to calculate sum of right boundary elements ; If left node , then just return its key value ; If right is available then go right otherwise go left ; Returns sum of uncovered elements ; Initializing with 0 in case we don 't have left or right boundary  ; returning sum of root node , left boundary and right boundary ; Returns true if sum of covered and uncovered elements is same . ; Sum of uncovered elements ; Sum of all elements ; Check if sum of covered and uncovered is same ; Helper function to print inorder traversal of binary tree ; Driver Code ; Making above given diagram 's binary tree
using System ; public class Node { public int key ; public Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class GFG { public Node root ; public virtual int sum ( Node t ) { if ( t == null ) { return 0 ; } return t . key + sum ( t . left ) + sum ( t . right ) ; } public virtual int uncoveredSumLeft ( Node t ) { if ( t . left == null && t . right == null ) { return t . key ; } if ( t . left != null ) { return t . key + uncoveredSumLeft ( t . left ) ; } else { return t . key + uncoveredSumLeft ( t . right ) ; } } public virtual int uncoveredSumRight ( Node t ) { if ( t . left == null && t . right == null ) { return t . key ; } if ( t . right != null ) { return t . key + uncoveredSumRight ( t . right ) ; } else { return t . key + uncoveredSumRight ( t . left ) ; } } public virtual int uncoverSum ( Node t ) { int lb = 0 , rb = 0 ; if ( t . left != null ) { lb = uncoveredSumLeft ( t . left ) ; } if ( t . right != null ) { rb = uncoveredSumRight ( t . right ) ; } return t . key + lb + rb ; } public virtual bool isSumSame ( Node root ) { int sumUC = uncoverSum ( root ) ; int sumT = sum ( root ) ; return ( sumUC == ( sumT - sumUC ) ) ; } public virtual void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; Console . Write ( root . key + " ▁ " ) ; inorder ( root . right ) ; } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 8 ) ; tree . root . left = new Node ( 3 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . left . right . left = new Node ( 4 ) ; tree . root . left . right . right = new Node ( 7 ) ; tree . root . right = new Node ( 10 ) ; tree . root . right . right = new Node ( 14 ) ; tree . root . right . right . left = new Node ( 13 ) ; if ( tree . isSumSame ( tree . root ) ) { Console . WriteLine ( " Sum ▁ of ▁ covered ▁ and ▁ " + " uncovered ▁ is ▁ same " ) ; } else { Console . WriteLine ( " Sum ▁ of ▁ covered ▁ and ▁ " + " uncovered ▁ is ▁ not ▁ same " ) ; } } }
Check if a number is Bleak | An efficient C # program to check Bleak Number ; Function to get no of set bits in binary representation of passed binary no . ; A function to return ceiling of log x in base 2. For example , it returns 3 for 8 and 4 for 9. ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
using System ; class GFG { static int countSetBits ( int x ) { int count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } static int ceilLog2 ( int x ) { int count = 0 ; x -- ; while ( x > 0 ) { x = x >> 1 ; count ++ ; } return count ; } static bool isBleak ( int n ) { for ( int x = n - ceilLog2 ( n ) ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } public static void Main ( ) { if ( isBleak ( 3 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; if ( isBleak ( 4 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count strings with consecutive 1 's | program to count all distinct binary strings with two consecutive 1 's ; Returns count of n length binary strings with consecutive 1 's ; Count binary strings without consecutive 1 's.  See the approach discussed on  ( http:goo.gl/p8A3sW ) ; Subtract a [ n - 1 ] + b [ n - 1 ] from 2 ^ n ; Driver code
using System ; class GFG { static int countStrings ( int n ) { int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] ; } public static void Main ( ) { Console . WriteLine ( countStrings ( 5 ) ) ; } }
How to swap two bits in a given integer ? | C # code for swapping given bits of a number ; left - shift 1 p1 and p2 times and using XOR ; Driver code
using System ; class GFG { static int swapBits ( int n , int p1 , int p2 ) { n ^= 1 << p1 ; n ^= 1 << p2 ; return n ; } static void Main ( ) { Console . WriteLine ( " Result ▁ = ▁ " + swapBits ( 28 , 0 , 3 ) ) ; } }
Maximum length sub | C # implementation of the approach ; Function to return the maximum length of the required sub - array ; To store the maximum length for a valid subarray ; To store the count of contiguous similar elements for previous group and the current group ; If current element is equal to the previous element then it is a part of the same group ; Else update the previous group and start counting elements for the new group ; Update the maximum possible length for a group ; Return the maximum length of the valid subarray ; Driver code
using System ; class GFG { static int maxLength ( int [ ] a , int n ) { int maxLen = 0 ; int prev_cnt = 0 , curr_cnt = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) curr_cnt ++ ; else { prev_cnt = curr_cnt ; curr_cnt = 1 ; } maxLen = Math . Max ( maxLen , Math . Min ( prev_cnt , curr_cnt ) ) ; } return ( 2 * maxLen ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxLength ( arr , n ) ) ; } }