codeparrot/xlcost-text-to-code huggingface dataset:
Construct XOR tree by Given leaf nodes of Perfect Binary Tree | C # implementation of the above approach ; Maximum size for xor tree ; Allocating space to xor tree ; A recursive function that constructs xor tree for vector array [ start ... . . end ] . x is index of current node in XOR tree ; If there is one element in vector array , store it in current node of XOR tree ; Console . Write ( xor_tree [ x ] + " ▁ x " ; ; for left subtree ; for right subtree ; for getting the middle index from corner indexes . ; Build the left and the right subtrees by xor operation ; merge the left and right subtrees by XOR operation ; Function to conXOR tree from the given vector array . This function calls construct_Xor_Tree_Util ( ) to fill the allocated memory of xor_tree vector array ; Driver Code ; leaf nodes of Perfect Binary Tree ; Build the xor tree ; Height of xor tree ; Maximum size of xor tree ; Root node is at index 0 considering 0 - based indexing in XOR Tree ; print value at root node
using System ; using System . Collections . Generic ; class GFG { static int maxsize = 100005 ; static int [ ] xor_tree = new int [ maxsize ] ; static void construct_Xor_Tree_Util ( int [ ] current , int start , int end , int x ) { if ( start == end ) { xor_tree [ x ] = current [ start ] ; return ; } int left = x * 2 + 1 ; int right = x * 2 + 2 ; int mid = start + ( end - start ) / 2 ; construct_Xor_Tree_Util ( current , start , mid , left ) ; construct_Xor_Tree_Util ( current , mid + 1 , end , right ) ; xor_tree [ x ] = ( xor_tree [ left ] ^ xor_tree [ right ] ) ; } static void construct_Xor_Tree ( int [ ] arr , int n ) { construct_Xor_Tree_Util ( arr , 0 , n - 1 , 0 ) ; } public static void Main ( String [ ] args ) { int [ ] leaf_nodes = { 40 , 32 , 12 , 1 , 4 , 3 , 2 , 7 } ; int n = leaf_nodes . Length ; construct_Xor_Tree ( leaf_nodes , n ) ; int x = ( int ) ( Math . Ceiling ( Math . Log ( n ) ) ) ; int max_size = 2 * ( int ) Math . Pow ( 2 , x ) - 1 ; Console . Write ( " Nodes ▁ of ▁ the ▁ XOR ▁ Tree : STRNEWLINE " ) ; for ( int i = 0 ; i < max_size ; i ++ ) { Console . Write ( xor_tree [ i ] + " ▁ " ) ; } int root = 0 ; Console . Write ( " Root : " } }
Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s | C # program to Maximize distance between any two consecutive 1 ' s ▁ after ▁ flipping ▁ M ▁ 0' s ; Function to return the count ; Flipping zeros at distance " d " ; Function to implement binary search ; Check for valid distance i . e mid ; Driver code
using System ; class GFG { static bool check ( int [ ] arr , int n , int m , int d ) { int i = 0 ; while ( i < n && m > 0 ) { m -- ; i += d ; } return m == 0 ? true : false ; } static int maximumDistance ( int [ ] arr , int n , int m ) { int low = 1 , high = n - 1 ; int ans = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; bool flag = check ( arr , n , m , mid ) ; if ( flag ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int n = 5 , m = 3 ; int [ ] arr = new int [ n ] ; Console . Write ( maximumDistance ( arr , n , m ) ) ; } }
Maximum XOR value of maximum and second maximum element among all possible subarrays | C # implementation of the approach ; Function to return the maximum possible xor ; To store the readonly answer ; Borward traversal ; Backward traversal ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int maximumXor ( int [ ] arr , int n ) { Stack < int > sForward = new Stack < int > ( ) , sBackward = new Stack < int > ( ) ; int ans = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { while ( sForward . Count != 0 && arr [ i ] < arr [ sForward . Peek ( ) ] ) { ans = Math . Max ( ans , arr [ i ] ^ arr [ sForward . Peek ( ) ] ) ; sForward . Pop ( ) ; } sForward . Push ( i ) ; while ( sBackward . Count != 0 && arr [ n - i - 1 ] < arr [ sBackward . Peek ( ) ] ) { ans = Math . Max ( ans , arr [ n - i - 1 ] ^ arr [ sBackward . Peek ( ) ] ) ; sBackward . Pop ( ) ; } sBackward . Push ( n - i - 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 1 , 2 } ; int n = arr . Length ; Console . Write ( maximumXor ( arr , n ) ) ; } }
Minimum count of Full Binary Trees such that the count of leaves is N | C # implementation of the approach ; Function to return the minimum count of trees required ; To store the count of set bits in n ; Driver code
using System ; class GFG { static int minTrees ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } public static void Main ( ) { int n = 7 ; Console . Write ( minTrees ( n ) ) ; } }
Count number of steps to cover a distance if steps can be taken in powers of 2 | C # program to count minimum number of steps ; Function to count the minimum number of steps ; count the number of set bits in a number ; Driver Code
using System ; class GFG { static int getMinSteps ( int K ) { return countSetBits ( K ) ; } static int countSetBits ( int x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int n = 343 ; Console . WriteLine ( getMinSteps ( n ) ) ; } }
Game Theory in Balanced Ternary Numeral System | ( Moving 3 k steps at a time ) | C # implementation of the approach ; Function that returns true if the game cannot be won ; Driver code ; Common length
using System ; class GfG { static bool isDefeat ( string s1 , string s2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( ( s1 [ i ] == '0' && s2 [ i ] == '1' ) || ( s1 [ i ] == '1' && s2 [ i ] == '0' ) ) continue ; else if ( ( s1 [ i ] == '0' && s2 [ i ] == ' Z ' ) || ( s1 [ i ] == ' Z ' && s2 [ i ] == '0' ) ) continue ; else { return true ; } } return false ; } public static void Main ( ) { string s1 = ( "01001101ZZ " ) ; string s2 = ( "10Z1001000" ) ; int n = 10 ; if ( isDefeat ( s1 , s2 , n ) ) Console . WriteLine ( " Defeat " ) ; else Console . WriteLine ( " Victory " ) ; } }
Check if matrix A can be converted to B by changing parity of corner elements of any submatrix | C # implementation of the above approach ; Boolean function that returns true or false ; Traverse for all elements ; If both are not equal ; Change the parity of all corner elements ; Check if A is equal to B ; Not equal ; Driver Code ; First binary matrix ; Second binary matrix
using System ; class GFG { static readonly int N = 3 , M = 3 ; static bool check ( int [ , ] a , int [ , ] b ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i , j ] != b [ i , j ] ) { a [ i , j ] ^= 1 ; a [ 0 , 0 ] ^= 1 ; a [ 0 , j ] ^= 1 ; a [ i , 0 ] ^= 1 ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i , j ] != b [ i , j ] ) return false ; } } return true ; } public static void Main ( String [ ] args ) { int [ , ] a = { { 0 , 1 , 0 } , { 0 , 1 , 0 } , { 1 , 0 , 0 } } ; int [ , ] b = { { 1 , 0 , 0 } , { 1 , 0 , 0 } , { 1 , 0 , 0 } } ; if ( check ( a , b ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Position of the K | C # program to implement the above approach ; Function that returns the Kth set bit ; Traverse in the binary ; Check if the last bit is set or not ; Check if count is equal to k then return the index ; Increase the index as we move right ; Right shift the number by 1 ; Driver Code
using System ; class GFG { static int FindIndexKthBit ( int n , int k ) { int cnt = 0 ; int ind = 0 ; while ( n > 0 ) { if ( ( n & 1 ) != 0 ) cnt ++ ; if ( cnt == k ) return ind ; ind ++ ; n = n >> 1 ; } return - 1 ; } public static void Main ( ) { int n = 15 , k = 3 ; int ans = FindIndexKthBit ( n , k ) ; if ( ans != - 1 ) Console . WriteLine ( ans ) ; else Console . WriteLine ( " No ▁ k - th ▁ set ▁ bit " ) ; } }
Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | C # implementation of the above approach ; Function to check ; Converting integer to its equivalent binary number ; If adjacent character are same then increase counter ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void hasEqualBlockFrequency ( int N ) { string S = Convert . ToString ( N , 2 ) ; HashSet < int > p = new HashSet < int > ( ) ; int c = 1 ; for ( int i = 0 ; i < S . Length - 1 ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) c += 1 ; else { p . Add ( c ) ; c = 1 ; } p . Add ( c ) ; } if ( p . Count == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static void Main ( ) { int N = 5 ; hasEqualBlockFrequency ( N ) ; } }
Distance between two nodes of binary tree with node values from 1 to N | C # program to find minimum distance between two nodes in binary tree ; Function to get minimum path distance ; count bit length of n1 and n2 ; find bit difference and maxBit ; calculate result by formula ; Driver code
using System ; class GFG { static int minDistance ( int n1 , int n2 ) { int bitCount1 = ( int ) Math . Floor ( ( Math . Log ( n1 ) / Math . Log ( 2 ) ) ) + 1 ; int bitCount2 = ( int ) Math . Floor ( ( Math . Log ( n2 ) / Math . Log ( 2 ) ) ) + 1 ; int bitDiff = Math . Abs ( bitCount1 - bitCount2 ) ; int maxBitCount = Math . Max ( bitCount1 , bitCount2 ) ; if ( bitCount1 > bitCount2 ) { n2 = n2 * ( int ) Math . Pow ( 2 , bitDiff ) ; } else { n1 = n1 * ( int ) Math . Pow ( 2 , bitDiff ) ; } int xorValue = n1 ^ n2 ; int bitCountXorValue ; if ( xorValue == 0 ) bitCountXorValue = 1 ; else { bitCountXorValue = ( int ) Math . Floor ( ( Math . Log ( xorValue ) / Math . Log ( 2 ) ) ) + 1 ; } int disSimilarBitPosition = maxBitCount - bitCountXorValue ; int result = bitCount1 + bitCount2 - 2 * disSimilarBitPosition ; return result ; } public static void Main ( String [ ] args ) { int n1 = 12 , n2 = 5 ; Console . WriteLine ( minDistance ( n1 , n2 ) ) ; } }
Remove one bit from a binary number to get maximum value | C # program to find next maximum binary number with one bit removed ; Function to find the maximum binary number ; Traverse the binary number ; Try finding a 0 and skip it ; Driver Code ; Get the binary number ; Find the maximum binary number
using System ; class GFG { static int printMaxAfterRemoval ( String s ) { bool flag = false ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' && flag == false ) { flag = true ; continue ; } else Console . Write ( s [ i ] ) ; } return 0 ; } static void Main ( ) { String s = "1001" ; printMaxAfterRemoval ( s ) ; } }
Find position of left most dis | C # to find the leftmost position of first dis - similar bit ; Function to find first dis - similar bit ; return zero for equal number ; count bit length of n1 and n2 ; find bit difference and maxBit ; Driver Code
using System ; class GFG { static int bitPos ( int n1 , int n2 ) { if ( n1 == n2 ) return 0 ; int bitCount1 = ( int ) Math . Floor ( Math . Log ( n1 ) / Math . Log ( 2 ) ) + 1 ; int bitCount2 = ( int ) Math . Floor ( Math . Log ( n2 ) / Math . Log ( 2 ) ) + 1 ; int bitDiff = Math . Abs ( bitCount1 - bitCount2 ) ; int maxBitCount = Math . Max ( bitCount1 , bitCount2 ) ; if ( bitCount1 > bitCount2 ) { n2 = n2 * ( int ) Math . Pow ( 2 , bitDiff ) ; } else { n1 = n1 * ( int ) Math . Pow ( 2 , bitDiff ) ; } int xorValue = n1 ^ n2 ; int bitCountXorValue = ( int ) Math . Floor ( Math . Log ( xorValue ) / Math . Log ( 2 ) ) + 1 ; int disSimilarBitPosition = maxBitCount - bitCountXorValue + 1 ; return disSimilarBitPosition ; } public static void Main ( ) { int n1 = 53 , n2 = 55 ; Console . Write ( bitPos ( n1 , n2 ) ) ; } }
Number of pairs with Bitwise OR as Odd number | C # program to count pairs with odd OR ; Function to count pairs with odd OR ; Count total even numbers in array ; Even pair count ; Total pairs ; Return Odd pair count ; Driver main
using System ; public class GFG { static int countOddPair ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 != 1 ) ) { count ++ ; } } int evenPairCount = count * ( count - 1 ) / 2 ; int totPairs = N * ( N - 1 ) / 2 ; return totPairs - evenPairCount ; } public static void Main ( ) { int [ ] A = { 5 , 6 , 2 , 8 } ; int N = A . Length ; Console . WriteLine ( countOddPair ( A , N ) ) ; } }
Replace every array element by Bitwise Xor of previous and next element | C # program to update every array element with sum of previous and next numbers in array ; Nothing to do when array size is 1 ; store current value of arr [ 0 ] and update it ; Update rest of the array elements ; Store current value of next interaction ; Update current value using previous value ; Update previous value ; Update last array element separately ; Driver Code ; Print the modified array
using System ; class GFG { static void ReplaceElements ( int [ ] arr , int n ) { if ( n <= 1 ) return ; int prev = arr [ 0 ] ; arr [ 0 ] = arr [ 0 ] ^ arr [ 1 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int curr = arr [ i ] ; arr [ i ] = prev ^ arr [ i + 1 ] ; prev = curr ; } arr [ n - 1 ] = prev ^ arr [ n - 1 ] ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Lucky alive person in a circle | Set | C # implementation of the above approach ; To calculate the number of bits in the binary equivalent of n ; Finding complement by inverting the bits one by one from last ; XOR of n2 with ( 1 << i ) to flip the last ( i + 1 ) th bit of binary equivalent of n2 ; n2 will be the one 's complement of n ; Driver code
using System ; class GFG { static int luckiest_person ( int n ) { int len = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; int n2 = n ; for ( int i = 0 ; i < len ; i ++ ) { n2 = n2 ^ ( 1 << i ) ; } return Math . Abs ( n - n2 ) ; } public static void Main ( ) { int N = 3 ; int lucky_p = luckiest_person ( N ) ; Console . Write ( lucky_p ) ; } }
Lucky alive person in a circle | Set | Here is a O ( 1 ) solution for this problemit will work for 32 bit integers ] ; function to find the highest power of 2 which is less than n ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000 ; Return original MSB after shifting . n now becomes 100000000 ; to calculate the highest number which is power of 2 and less than n ; return the correct answer as per the approach in above article ; Driver code
using System ; class GFG { static int setBitNumber ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n = n + 1 ; return ( n >> 1 ) ; } static int luckiest_person ( int n ) { int nearestPower = setBitNumber ( n ) ; return 2 * ( n - nearestPower ) + 1 ; } public static void Main ( ) { int N = 8 ; int lucky_p = luckiest_person ( N ) ; Console . Write ( lucky_p ) ; } }
Find triplets in an array whose AND is maximum | C # program to find triplet with maximum bitwise AND . ; Flag Array initially set to true for all numbers ; 2D array for bit representation of all the numbers . Initially all bits are set to 0. ; Finding bit representation of every number and storing it in bits array . ; Checking last bit of the number ; Dividing number by 2. ; maximum And number initially 0. ; Traversing the 2d binary representation . 0 th index represents 32 th bits while 32 th index represents 0 th bit . ; If cnt greater than 3 then ( 32 - i ) th bits of the number will be set . ; Setting flags of the numbers whose ith bit is not set . ; Counting the numbers whose flag are true . ; Driver code
using System ; class GFG { static void maxTriplet ( int [ ] a , int n ) { Boolean [ ] f = new Boolean [ n ] ; for ( int i = 0 ; i < n ; ++ i ) f [ i ] = true ; int [ , ] bits = new int [ n , 33 ] ; for ( int i = 0 ; i < n ; ++ i ) { int num = a [ i ] ; int j = 32 ; while ( num > 0 ) { if ( num % 2 == 1 ) { bits [ i , j ] = 1 ; } j -- ; num >>= 1 ; } } long ans = 0 ; int cnt ; for ( int i = 0 ; i <= 32 ; ++ i ) { cnt = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( bits [ j , i ] == 1 & f [ j ] ) { cnt ++ ; } } if ( cnt >= 3 ) { ans += ( long ) Math . Pow ( 2 , 32 - i ) ; for ( int j = 0 ; j < n ; ++ j ) { if ( bits [ j , i ] != 1 ) { f [ j ] = false ; } } } } cnt = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( f [ i ] ) { cnt ++ ; } } long NumberOfTriplets = ( cnt * ( cnt - 1 ) * ( cnt - 2 ) ) / 6 ; Console . Write ( NumberOfTriplets + " ▁ " + ans ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 4 , 11 , 10 , 15 , 26 } ; int n = a . Length ; maxTriplet ( a , n ) ; } }
Find bitwise OR of all possible sub | C # program to find OR of all the sub - arrays ; function to return OR of sub - arrays ; Driver Code ; print OR of all subarrays
using System ; class GFG { static int OR ( int [ ] a , int n ) { int ans = a [ 0 ] ; int i ; for ( i = 1 ; i < n ; i ++ ) { ans |= a [ i ] ; } return ans ; } public static void Main ( ) { int [ ] a = { 1 , 4 , 6 } ; int n = a . Length ; Console . Write ( OR ( a , n ) ) ; } }
Maximum set bit sum in array without considering adjacent elements | C # program to maximum set bit sum in array without considering adjacent elements ; Function to count total number of set bits in an integer ; Maximum sum of set bits ; Calculate total number of set bits for every element of the array ; find total set bits for each number and store back into the array ; current max excluding i ; current max including i ; return max of incl and excl ; Driver code
using System ; class GFG { static int bit ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n & ( n - 1 ) ; } return count ; } static int maxSumOfBits ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = bit ( arr [ i ] ) ; } int incl = arr [ 0 ] ; int excl = 0 ; int excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = ( incl > excl ) ? incl : excl ; incl = excl + arr [ i ] ; excl = excl_new ; } return ( ( incl > excl ) ? incl : excl ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 5 , 6 , 7 , 20 , 25 } ; int n = arr . Length ; Console . WriteLine ( maxSumOfBits ( arr , n ) ) ; } }
Increment a number without using ++ or + | C # program to increment an unsigned char using bitwise operators . ; function that increment the value . ; Invert bits and apply negative sign ; Driver code
class GFG { static char increment ( char i ) { int i1 = - ( ~ ( int ) ( i ) ) ; return ( char ) ( i1 ) ; } static void Main ( ) { char n = ' a ' ; System . Console . WriteLine ( increment ( n ) ) ; } }
Count pairs with Bitwise XOR as ODD number | C # program to count pairs with XOR giving a odd number ; Function to count number of odd pairs ; find all pairs ; return number of odd pair ; Driver Code ; calling function findOddPair and print number of odd pair
using System ; class GFG { static int findOddPair ( int [ ] A , int N ) { int i , count = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) count ++ ; } return count * ( N - count ) ; } public static void Main ( ) { int [ ] a = { 5 , 4 , 7 , 2 , 1 } ; int n = a . Length ; Console . Write ( findOddPair ( a , n ) ) ; } }
Bitwise OR ( or | ) of a range | C # Program to find the bitwise OR of all the integers in range L - R ; Returns the Most Significant Bit Position ( MSB ) ; Returns the Bitwise OR of all integers between L and R ; Find the MSB position in L ; Find the MSB position in R ; Add this value until msb_p1 and msb_p2 are same ; ; Calculate msb_p1 and msb_p2 ; Find the max of msb_p1 and msb_p2 ; Set all the bits from msb_p1 upto 0 th bit in the result ; Driver Code
using System ; class GFG { static int MSBPosition ( long N ) { int msb_p = - 1 ; while ( N > 0 ) { N = N >> 1 ; msb_p ++ ; } return msb_p ; } static long findBitwiseOR ( long L , long R ) { long res = 0 ; int msb_p1 = MSBPosition ( L ) ; int msb_p2 = MSBPosition ( R ) ; while ( msb_p1 == msb_p2 ) { long res_val = ( 1 << msb_p1 ) ; res += res_val ; L -= res_val ; R -= res_val ; msb_p1 = MSBPosition ( L ) ; msb_p2 = MSBPosition ( R ) ; } msb_p1 = Math . Max ( msb_p1 , msb_p2 ) ; for ( int i = msb_p1 ; i >= 0 ; i -- ) { long res_val = ( 1 << i ) ; res += res_val ; } return res ; } public static void Main ( ) { int L = 12 , R = 18 ; Console . WriteLine ( findBitwiseOR ( L , R ) ) ; } }
Maximize the bitwise OR of an array | C # program to maximize the Bitwise OR Sum in given array ; Function to maximize the bitwise OR sum ; Compute x ^ k ; Find prefix bitwise OR ; Find suffix bitwise OR ; Find maximum OR value ; Drivers code
using System ; class GFG { public static long maxOR ( long [ ] arr , int n , int k , int x ) { long [ ] preSum = new long [ n + 1 ] ; long [ ] suffSum = new long [ n + 1 ] ; long res = 0 , pow = 1 ; for ( int i = 0 ; i < k ; i ++ ) pow *= x ; preSum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) preSum [ i + 1 ] = preSum [ i ] | arr [ i ] ; suffSum [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) suffSum [ i ] = suffSum [ i + 1 ] | arr [ i ] ; res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = Math . Max ( res , preSum [ i ] | ( arr [ i ] * pow ) suffSum [ i + 1 ] ) ; return res ; } public static void Main ( ) { long [ ] arr = { 1 , 2 , 4 , 8 } ; int n = 4 ; int k = 2 , x = 3 ; long ans = maxOR ( arr , n , k , x ) ; Console . Write ( ans ) ; } }
How to turn on a particular bit in a number ? | C # program to turn on a particular bit ; Returns a number that has all bits same as n except the k 'th  bit which is made 1 ; k must be greater than 0 ; Do | of n with a number with all unset bits except the k 'th bit ; Driver program to test above function
using System ; class GFG { static int turnOnK ( int n , int k ) { if ( k <= 0 ) return n ; return ( n | ( 1 << ( k - 1 ) ) ) ; } public static void Main ( ) { int n = 4 ; int k = 2 ; Console . Write ( turnOnK ( n , k ) ) ; } }
Minimum sum of two numbers formed from digits of an array | C # a program to find minimum sum of two numbers formed from all digits in a given array . ; Returns sum of two numbers formed from all digits in a [ ] ; sort the elements ; Driver code
using System ; public class GFG { static int minSum ( int [ ] a , int n ) { Array . Sort ( a ) ; int num1 = 0 ; int num2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) num1 = num1 * 10 + a [ i ] ; else num2 = num2 * 10 + a [ i ] ; } return num2 + num1 ; } static public void Main ( ) { int [ ] arr = { 5 , 3 , 0 , 7 , 4 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ required ▁ sum ▁ is ▁ " + minSum ( arr , n ) ) ; } }
Find value of k | C # program to find k - th bit from right ; Driver Code ; Function Call
using System ; class GFG { static void printKthBit ( long n , long k ) { Console . WriteLine ( ( n & ( 1 << ( k - 1 ) ) ) >> ( k - 1 ) ) ; } public static void Main ( ) { long n = 13 , k = 2 ; printKthBit ( n , k ) ; } }