codeparrot/xlcost-text-to-code huggingface dataset:
Program to convert a given number to words | Set 2 | C # program to print a given number in words . The program handles till 9 digits numbers and can be easily extended to 20 digit number ; strings at index 0 is not used , it is to make array indexing simple ; strings at index 0 and 1 are not used , they is to make array indexing simple ; n is 1 - or 2 - digit number ; if n is more than 19 , divide it ; if n is non - zero ; Function to print a given number in words ; stores word representation of given number n ; handles digits at ten millions and hundred millions places ( if any ) ; handles digits at hundred thousands and one millions places ( if any ) ; handles digits at thousands and tens thousands places ( if any ) ; handles digit at hundreds places ( if any ) ; handles digits at ones and tens places ( if any ) ; Driver code ; long handles upto 9 digit no change to unsigned long long int to handle more digit number ; convert given number in words
using System ; class GFG { static string [ ] one = { " " , " one ▁ " , " two ▁ " , " three ▁ " , " four ▁ " , " five ▁ " , " six ▁ " , " seven ▁ " , " eight ▁ " , " nine ▁ " , " ten ▁ " , " eleven ▁ " , " twelve ▁ " , " thirteen ▁ " , " fourteen ▁ " , " fifteen ▁ " , " sixteen ▁ " , " seventeen ▁ " , " eighteen ▁ " , " nineteen ▁ " } ; static string [ ] ten = { " " , " " , " twenty ▁ " , " thirty ▁ " , " forty ▁ " , " fifty ▁ " , " sixty ▁ " , " seventy ▁ " , " eighty ▁ " , " ninety ▁ " } ; static string numToWords ( int n , string s ) { string str = " " ; if ( n > 19 ) { str += ten [ n / 10 ] + one [ n % 10 ] ; } else { str += one [ n ] ; } if ( n != 0 ) { str += s ; } return str ; } static string convertToWords ( long n ) { string out1 = " " ; out1 += numToWords ( ( int ) ( n / 10000000 ) , " crore ▁ " ) ; out1 += numToWords ( ( int ) ( ( n / 100000 ) % 100 ) , " lakh ▁ " ) ; out1 += numToWords ( ( int ) ( ( n / 1000 ) % 100 ) , " thousand ▁ " ) ; out1 += numToWords ( ( int ) ( ( n / 100 ) % 10 ) , " hundred ▁ " ) ; if ( n > 100 && n % 100 > 0 ) { out1 += " and ▁ " ; } out1 += numToWords ( ( int ) ( n % 100 ) , " " ) ; return out1 ; } static void Main ( ) { long n = 438237764 ; Console . WriteLine ( convertToWords ( n ) ) ; } }
Find number of subarrays with even sum | C # program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; result may be large enough not to fit in int ; ; to keep track of subarrays with even sum starting from index i ; if a [ i ] is odd then all subarrays starting from index i + 1 which was odd becomeseven when a [ i ] gets added to it . ; if a [ i ] is even then all subarrays starting from index i + 1 which was even remainseven and one extra a [ i ] even subarray gets added to it . ; Driver Code
using System ; public class GFG { public static long countEvenSum ( int [ ] a , int n ) { long res = 0 ; int s = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] % 2 == 1 ) { s = n - i - 1 - s ; } else { s = s + 1 ; } res = res + s ; } return res ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " + countEvenSum ( arr , n ) ) ; } }
Linear Diophantine Equations | C # program to check for solutions of diophantine equations ; Utility function to find the GCD of two numbers ; This function checks if integral solutions are possible ; Driver Code ; First example ; Second example ; Third example
using System ; class GFG { static int gcd ( int a , int b ) { return ( a % b == 0 ) ? Math . Abs ( b ) : gcd ( b , a % b ) ; } static bool isPossible ( int a , int b , int c ) { return ( c % gcd ( a , b ) == 0 ) ; } public static void Main ( ) { int a = 3 , b = 6 , c = 9 ; if ( isPossible ( a , b , c ) ) Console . WriteLine ( " Possible " ) ; else Console . WriteLine ( " Not ▁ Possible " ) ; a = 3 ; b = 6 ; c = 8 ; if ( isPossible ( a , b , c ) ) Console . WriteLine ( " Possible " ) ; else Console . WriteLine ( " Not ▁ Possible " ) ; a = 2 ; b = 5 ; c = 1 ; if ( isPossible ( a , b , c ) ) Console . WriteLine ( " Possible " ) ; else Console . WriteLine ( " Not ▁ Possible " ) ; } }
Farey Sequence | Efficient C # program to print Farey Sequence of order n ; Optimized function to print Farey sequence of order n ; We know first two terms are 0 / 1 and 1 / n ; double x , y = 0 ; For next terms to be evaluated ; Using recurrence relation to find the next term ; Print next term ; Update x1 , y1 , x2 and y2 for next iteration ; Driver program
using System ; public class GFG { static void farey ( int n ) { double x1 = 0 , y1 = 1 , x2 = 1 , y2 = n ; Console . Write ( " { 0 : F0 } / { 1 : F0 } ▁ { 2 : F0 } / { 3 : F0 } " , x1 , y1 , x2 , y2 ) ; while ( y != 1.0 ) { x = Math . Floor ( ( y1 + n ) / y2 ) * x2 - x1 ; y = Math . Floor ( ( y1 + n ) / y2 ) * y2 - y1 ; Console . Write ( " ▁ { 0 : F0 } / { 1 : F0 } " , x , y ) ; x1 = x2 ; x2 = x ; y1 = y2 ; y2 = y ; } } public static void Main ( String [ ] args ) { int n = 7 ; Console . Write ( " Farey ▁ Sequence ▁ of ▁ order ▁ " + n + " ▁ is STRNEWLINE " ) ; farey ( n ) ; } }
Find smallest values of x and y such that ax | C # program to find the smallest values of x and y that satisfy " ax ▁ - ▁ by ▁ = ▁ 0" ; To find GCD using Eculcid 's algorithm ; Prints smallest values of x and y that satisfy " ax ▁ - ▁ by ▁ = ▁ 0" ; Find LCM ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return ( gcd ( b , a % b ) ) ; } static void findSmallest ( int a , int b ) { int lcm = ( a * b ) / gcd ( a , b ) ; Console . Write ( " x ▁ = ▁ " + lcm / a + " y = " } public static void Main ( ) { int a = 25 , b = 35 ; findSmallest ( a , b ) ; } }
Compute n ! under modulo p | C # program to comput n ! % p using Wilson 's Theorem ; Utility function to do modular exponentiation . It returns ( x ^ y ) % p ; int res = 1 ; Initialize result x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to find modular inverse of a under modulo p using Fermat 's method. Assumption: p is prime ; Returns n ! % p using Wilson 's Theorem ; n ! % p is 0 if n >= p ; Initialize result as ( p - 1 ) ! which is - 1 or ( p - 1 ) ; Multiply modulo inverse of all numbers from ( n + 1 ) to p ; Driver method
using System ; class GFG { public int power ( int x , int y , int p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } public int modInverse ( int a , int p ) { return power ( a , p - 2 , p ) ; } public int modFact ( int n , int p ) { if ( p <= n ) return 0 ; int res = ( p - 1 ) ; for ( int i = n + 1 ; i < p ; i ++ ) res = ( res * modInverse ( i , p ) ) % p ; return res ; } public static void Main ( ) { GFG g = new GFG ( ) ; int n = 25 , p = 29 ; Console . WriteLine ( g . modFact ( n , p ) ) ; } }
Count number of ways to divide a number in 4 parts | A Simple C # program to count number of ways to represent a number n as sum of four . ; Returns count of ways ; Generate all possible quadruplet and increment counter when sum of a quadruplet is equal to n ; Driver Code
using System ; class GFG { static int countWays ( int n ) { for ( int i = 1 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) for ( int k = j ; k < n ; k ++ ) for ( int l = k ; l < n ; l ++ ) if ( i + j + k + l == n ) counter ++ ; return counter ; } static public void Main ( ) { int n = 8 ; Console . WriteLine ( countWays ( n ) ) ; } }
Sum of Bitwise And of all pairs in a given array | A Simple C # program to compute sum of bitwise AND of all pairs ; Returns value of " arr [ 0 ] ▁ & ▁ arr [ 1 ] ▁ + ▁ ▁ arr [ 0 ] ▁ & ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ & ▁ arr [ j ] ▁ + ▁ ▁ . . . . . ▁ arr [ n - 2 ] ▁ & ▁ arr [ n - 1 ] " ; Consider all pairs ( arr [ i ] , arr [ j ) such that i < j ; Driver program to test above function
using System ; class GFG { static int pairAndSum ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) ans += arr [ i ] & arr [ j ] ; return ans ; } public static void Main ( ) { int [ ] arr = { 5 , 10 , 15 } ; int n = arr . Length ; Console . Write ( pairAndSum ( arr , n ) ) ; } }
Sum of Bitwise And of all pairs in a given array | An efficient C # program to compute sum of bitwise AND of all pairs ; Returns value of " arr [ 0 ] ▁ & ▁ arr [ 1 ] ▁ + ▁ ▁ arr [ 0 ] ▁ & ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ & ▁ arr [ j ] ▁ + ▁ ▁ . . . . . ▁ arr [ n - 2 ] ▁ & ▁ arr [ n - 1 ] " ; Traverse over all bits ; Count number of elements with i 'th bit set Initialize the count ; There are k set bits , means k ( k - 1 ) / 2 pairs . Every pair adds 2 ^ i to the answer . Therefore , we add "2 ^ i ▁ * ▁ ▁ [ k * ( k - 1 ) / 2 ] " to the answer . ; Driver program to test above function
using System ; class GFG { static int pairAndSum ( int [ ] arr , int n ) { for ( int i = 0 ; i < 32 ; i ++ ) { int k = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) k ++ ; } ans += ( 1 << i ) * ( k * ( k - 1 ) / 2 ) ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 5 , 10 , 15 } ; int n = arr . Length ; Console . Write ( pairAndSum ( arr , n ) ) ; } }
Primality Test | Set 1 ( Introduction and School Method ) | A optimized school method based C # program to check if a number is prime ; Corner cases ; Check from 2 to n - 1 ; Driver program
using System ; namespace prime { public class GFG { public static bool isprime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } public static void Main ( ) { if ( isprime ( 11 ) ) Console . WriteLine ( " true " ) ; else Console . WriteLine ( " false " ) ; if ( isprime ( 15 ) ) Console . WriteLine ( " true " ) ; else Console . WriteLine ( " false " ) ; } } }
Euler 's Totient function for all numbers smaller than or equal to n | C # program to check if given two strings are at distance one . ; Computes and prints totient of all numbers smaller than or equal to n ; Create and initialize an array to store phi or totient values ; indicates not evaluated yet and initializes for product formula . ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Print precomputed phi values ; Driver code
using System ; class GFG { static void computeTotient ( int n ) { long [ ] phi = new long [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p <= n ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i <= n ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } for ( int i = 1 ; i <= n ; i ++ ) Console . WriteLine ( " Totient ▁ of ▁ " + i + " ▁ is ▁ " + phi [ i ] ) ; } public static void Main ( ) { int n = 12 ; computeTotient ( n ) ; } }
Euler 's Totient function for all numbers smaller than or equal to n | C # program for the above approach ; Driver code
using System ; class GFG { static long Euler_totient_function ( long n ) { long result = 1 ; for ( long i = 2 ; i * i <= n ; i ++ ) { long c = 0 ; if ( n % i == 0 ) { while ( n % i == 0 ) { c ++ ; n /= i ; } } if ( c > 0 ) { long sm = ( long ) Math . Pow ( i , c - 1 ) * ( i - 1 ) ; result *= sm ; } } if ( n > 1 ) { result *= ( n - 1 ) ; } return result ; } public static void Main ( ) { for ( long i = 1 ; i < 13 ; i ++ ) { Console . Write ( " Euler _ totient _ function ( " + i + " ) : ▁ " ) ; Console . WriteLine ( Euler_totient_function ( i ) ) ; } } }
Check if a number can be expressed as x ^ y ( x raised to power y ) | C # program to check if a given number can be expressed as power ; Returns true if n can be written as x ^ y ; Driver code
using System ; class GFG { static bool isPower ( int n ) { double p ; if ( n <= 1 ) { return true ; } for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { p = Math . Log ( n ) / Math . Log ( i ) ; if ( ( Math . Ceiling ( p ) == Math . Floor ( p ) ) && p > 1 ) { return true ; } } return false ; } static public void Main ( ) { for ( int i = 2 ; i < 100 ; i ++ ) { if ( isPower ( i ) ) Console . Write ( i + " ▁ " ) ; } } }
Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | C # program to find sum of all element between to K1 ' th ▁ and ▁ k2' th smallest elements in array ; Returns sum between two kth smallest element of array ; Sort the given array ; Below code is equivalent to ; Driver code
using System ; class GFG { static int sumBetweenTwoKth ( int [ ] arr , int n , int k1 , int k2 ) { Array . Sort ( arr ) ; int result = 0 ; for ( int i = k1 ; i < k2 - 1 ; i ++ ) result += arr [ i ] ; return result ; } public static void Main ( ) { int [ ] arr = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; int k1 = 3 , k2 = 6 ; int n = arr . Length ; Console . Write ( sumBetweenTwoKth ( arr , n , k1 , k2 ) ) ; } }
Puzzle | Program to find number of squares in a chessboard | C # find number of squares in a chessboard ; Function to return count of squares ; ; A better way to write n * ( n + 1 ) * ( 2 n + 1 ) / 6 ; Driver code
using System ; public class GFG { static int countSquares ( int n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( " Count ▁ of " + " squares ▁ is ▁ " + countSquares ( n ) ) ; } }
Find nth Magic Number | C # program to find nth magic number ; Function to find nth magic number ; Go through every bit of n ; If last bit of n is set ; proceed to next bit or n = n / 2 ; Driver Code
using System ; public class GFG { static int nthMagicNo ( int n ) { int pow = 1 , answer = 0 ; while ( n != 0 ) { pow = pow * 5 ; if ( ( int ) ( n & 1 ) == 1 ) answer += pow ; n >>= 1 ; } return answer ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( " nth ▁ magic " + " ▁ number ▁ is ▁ " + nthMagicNo ( n ) ) ; } }
Given a number n , count all multiples of 3 and / or 5 in set { 1 , 2 , 3 , ... n } | C # program to find count of multiples of 3 and 5 in { 1 , 2 , 3 , . . n } ; Add multiples of 3 and 5. Since common multiples are counted twice in n / 3 + n / 15 , subtract common multiples ; Driver program to test above function
using System ; public class GFG { static uint countOfMultiples ( uint n ) { return ( n / 3 + n / 5 - n / 15 ) ; } static public void Main ( ) { Console . WriteLine ( countOfMultiples ( 6 ) ) ; Console . WriteLine ( countOfMultiples ( 16 ) ) ; } }
Program to find GCD or HCF of two numbers | C # program to find GCD of two numbers ; Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; Driver method
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } public static void Main ( ) { int a = 98 , b = 56 ; Console . WriteLine ( " GCD ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + gcd ( a , b ) ) ; } }
Find XOR sum of Bitwise AND of all pairs from given two Arrays | C # program for the above approach ; Function to calculate the XOR sum of all ANDS of all pairs on A [ ] and B [ ] ; Variable to store xor sums of first array and second array respectively . ; Xor sum of first array ; Xor sum of second array ; Required answer ; Driver code ; Input ; Function call
using System ; class GFG { static int XorSum ( int [ ] A , int [ ] B , int N , int M ) { int ans1 = 0 , ans2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) ans1 = ans1 ^ A [ i ] ; for ( int i = 0 ; i < M ; i ++ ) ans2 = ans2 ^ B [ i ] ; return ( ans1 & ans2 ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 5 } ; int [ ] B = { 2 , 3 } ; int N = A . Length ; int M = B . Length ; Console . Write ( XorSum ( A , B , N , M ) ) ; } }
Count levels in a Binary Tree consisting of node values having set bits at different positions | C # program for the above approach ; Structure of a node in the binary tree ; Function to find total unique levels ; Stores count of levels , where the set bits of all the nodes are at different positions ; Store nodes at each level of the tree using BFS ; Performing level order traversal ; Stores count of nodes at current level ; Stores prefix XOR of all the nodes at current level ; Stores prefix OR of all the nodes at current level ; Check if set bit of all the nodes at current level is at different positions or not ; Traverse nodes at current level ; Stores front element of the que ; Update prefix_OR ; Update prefix_XOR ; If left subtree not null ; If right subtree not null ; If bitwise AND is zero ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; public class GFG { class TreeNode { public int val = 0 ; public TreeNode left , right ; public TreeNode ( int x ) { val = x ; left = null ; right = null ; } } ; static void uniqueLevels ( TreeNode root ) { int uniqueLevels = 0 ; Queue < TreeNode > que = new Queue < TreeNode > ( ) ; que . Enqueue ( root ) ; while ( que . Count > 0 ) { int length = que . Count ; int prefix_XOR = 0 ; int prefix_OR = 0 ; bool flag = true ; for ( int i = 0 ; i < length ; i ++ ) { TreeNode temp = que . Peek ( ) ; que . Dequeue ( ) ; prefix_OR |= temp . val ; prefix_XOR ^= temp . val ; if ( prefix_XOR != prefix_OR ) flag = false ; if ( temp . left != null ) que . Enqueue ( temp . left ) ; if ( temp . right != null ) que . Enqueue ( temp . right ) ; } if ( flag ) uniqueLevels += 1 ; } Console . Write ( uniqueLevels ) ; } public static void Main ( String [ ] args ) { TreeNode root = new TreeNode ( 5 ) ; root . left = new TreeNode ( 6 ) ; root . right = new TreeNode ( 9 ) ; root . left . left = new TreeNode ( 1 ) ; root . left . right = new TreeNode ( 4 ) ; root . right . right = new TreeNode ( 7 ) ; uniqueLevels ( root ) ; } }
Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | C # implementation of above approach ; Driver code ; decreasing value by 1 because we want min heapifying k times and it starts from 0 so we have to decrease it 1 time ; Step 1 : Do extract minimum k1 times ( This step takes O ( K1 Log n ) time ) ; Step 2 : Do extract minimum k2 k1 1 times and sum all extracted elements . ( This step takes O ( ( K2 k1 ) * Log n ) time )
using System ; class GFG { static int n = 7 ; static void minheapify ( int [ ] a , int index ) { int small = index ; int l = 2 * index + 1 ; int r = 2 * index + 2 ; if ( l < n && a [ l ] < a [ small ] ) small = l ; if ( r < n && a [ r ] < a [ small ] ) small = r ; if ( small != index ) { int t = a [ small ] ; a [ small ] = a [ index ] ; a [ index ] = t ; minheapify ( a , small ) ; } } static void Main ( ) { int i = 0 ; int k1 = 3 ; int k2 = 6 ; int [ ] a = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; int ans = 0 ; for ( i = ( n / 2 ) - 1 ; i >= 0 ; i -- ) { minheapify ( a , i ) ; } k1 -- ; k2 -- ; for ( i = 0 ; i <= k1 ; i ++ ) { a [ 0 ] = a [ n - 1 ] ; n -- ; minheapify ( a , 0 ) ; } for ( i = k1 + 1 ; i < k2 ; i ++ ) { ans += a [ 0 ] ; a [ 0 ] = a [ n - 1 ] ; n -- ; minheapify ( a , 0 ) ; } Console . Write ( ans ) ; } }
Construct a List using the given Q XOR queries | C # program for the above approach ; Function to return required list after performing all the queries ; Store cumulative Bitwise XOR ; Initialize readonly list to return ; Perform each query ; The initial value of 0 ; Sort the list ; Return readonly list ; Driver Code ; Given Queries ; Function Call
using System ; using System . Collections . Generic ; class GFG { static List < int > ConstructList ( int [ , ] Q ) { int xor = 0 ; List < int > ans = new List < int > ( ) ; for ( int i = Q . GetLength ( 0 ) - 1 ; i >= 0 ; i -- ) { if ( Q [ i , 0 ] == 0 ) ans . Add ( Q [ i , 1 ] ^ xor ) ; else xor ^= Q [ i , 1 ] ; } ans . Add ( xor ) ; ans . Sort ( ) ; return ans ; } public static void Main ( String [ ] args ) { int [ , ] Q = { { 0 , 6 } , { 0 , 3 } , { 0 , 2 } , { 1 , 4 } , { 1 , 5 } } ; foreach ( int i in ConstructList ( Q ) ) Console . Write ( i + " , ▁ " ) ; } }
Bitwise operations on Subarrays of size K | C # program for maximum values of each bitwise OR operation on element of subarray of size K ; Function to convert bit array to decimal number ; Function to find maximum values of each bitwise OR operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation for removed element ; Perform operation for added_element ; Taking maximum value ; Return the result ; Driver Code ; Given array [ ] arr ; Given subarray size K ; Function Call
using System ; class GFG { static int build_num ( int [ ] bit ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] > 0 ) ans += ( 1 << i ) ; return ans ; } static int maximumOR ( int [ ] arr , int n , int k ) { int [ ] bit = new int [ 32 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } int max_or = build_num ( bit ) ; for ( int i = k ; i < n ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } max_or = Math . Max ( build_num ( bit ) , max_or ) ; } return max_or ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = arr . Length ; Console . Write ( maximumOR ( arr , n , k ) ) ; } }
XOR of every element of an Array with a given number K | C # program to find XOR of every element of an array with a given number K ; Function to construct new array ; Traverse the array and compute XOR with K ; Print new array ; Driver code
using System ; class GFG { static void constructXORArray ( int [ ] A , int n , int K ) { int [ ] B = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = A [ i ] ^ K ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( B [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 4 , 1 , 3 , 5 } ; int K = 5 ; int n = A . Length ; constructXORArray ( A , n , K ) ; int [ ] B = { 4 , 75 , 45 , 42 } ; K = 2 ; n = B . Length ; constructXORArray ( B , n , K ) ; } }
Bitwise AND of all the elements of array | C # program to find bitwise AND of all the elements in the array ; Function to calculate bitwise AND ; Initialise ans variable is arr [ 0 ] ; Traverse the array compute AND ; Return ans ; Driver Code ; Function Call to find AND
using System ; class GFG { static int find_and ( int [ ] arr ) { int ans = arr [ 0 ] ; for ( int i = 0 ; i < arr . Length ; i ++ ) { ans = ( ans & arr [ i ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 9 , 11 } ; Console . Write ( find_and ( arr ) ) ; } }