codeparrot/xlcost-text-to-code huggingface dataset:
Fibonacci Power |  ; Iterative function to compute modular power ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Helper function that multiplies 2 matrices F and M of size 2 * 2 , and puts the multiplication result back to F [ , ] ; Helper function that calculates F [ , ] raise to the power n and puts the result in F [ , ] Note that this function is designed only for fib ( ) and won 't work as general power function ; Function that returns nth Fibonacci number ; Driver Code
using System ; class GFG { static int mod = 1000000007 ; static long modularexpo ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void multiply ( long [ , ] F , long [ , ] M , long m ) { long x = ( ( F [ 0 , 0 ] * M [ 0 , 0 ] ) % m + ( F [ 0 , 1 ] * M [ 1 , 0 ] ) % m ) % m ; long y = ( ( F [ 0 , 0 ] * M [ 0 , 1 ] ) % m + ( F [ 0 , 1 ] * M [ 1 , 1 ] ) % m ) % m ; long z = ( ( F [ 1 , 0 ] * M [ 0 , 0 ] ) % m + ( F [ 1 , 1 ] * M [ 1 , 0 ] ) % m ) % m ; long w = ( ( F [ 1 , 0 ] * M [ 0 , 1 ] ) % m + ( F [ 1 , 1 ] * M [ 1 , 1 ] ) % m ) % m ; F [ 0 , 0 ] = x ; F [ 0 , 1 ] = y ; F [ 1 , 0 ] = z ; F [ 1 , 1 ] = w ; } static void power ( long [ , ] F , long n , long m ) { if ( n == 0 n == 1 ) return ; long [ , ] M = { { 1 , 1 } , { 1 , 0 } } ; power ( F , n / 2 , m ) ; multiply ( F , F , m ) ; if ( n % 2 != 0 ) multiply ( F , M , m ) ; } static long fib ( long n , long m ) { long [ , ] F = { { 1 , 1 } , { 1 , 0 } } ; if ( n == 0 ) return 0 ; power ( F , n - 1 , m ) ; return F [ 0 , 0 ] ; } public static void Main ( String [ ] args ) { long n = 4 ; long Base = fib ( n , mod ) % mod ; long expo = fib ( n , mod - 1 ) % ( mod - 1 ) ; long result = modularexpo ( Base , expo , mod ) % mod ; Console . WriteLine ( result ) ; } }
Count number of ways to divide an array into two halves with same sum | C # program to count the number of ways to divide an array into two halves with the same sum ; Function to count the number of ways to divide an array into two halves with same sum ; if length of array is 1 answer will be 0 as we have to split it into two non - empty halves ; variables to store total sum , current sum and count ; finding total sum ; checking if sum equals total_sum / 2 ; Driver Code
using System ; class GFG { static int cntWays ( int [ ] arr , int n ) { if ( n == 1 ) { return 0 ; } int tot_sum = 0 , sum = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { tot_sum += arr [ i ] ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] ; if ( sum == tot_sum / 2 ) { ans ++ ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , - 1 , 1 , - 1 , 1 , - 1 } ; int n = arr . Length ; Console . WriteLine ( cntWays ( arr , n ) ) ; } }
Find N distinct numbers whose bitwise Or is equal to K | C # implementation of the approach ; Function to pre - calculate all the powers of 2 upto MAX ; Function to return the count of set bits in x ; To store the count of set bits ; Function to add num to the answer by setting all bit positions as 0 which are also 0 in K ; Bit i is 0 in K ; Function to find and print N distinct numbers whose bitwise OR is K ; Choosing K itself as one number ; Find the count of set bits in K ; Impossible to get N distinct integers ; Add i to the answer after setting all the bits as 0 which are 0 in K ; If N distinct numbers are generated ; Print the generated numbers ; Driver code ; Pre - calculate all the powers of 2
using System ; using System . Collections . Generic ; class GFG { static int MAX = 32 ; static long [ ] pow2 = new long [ MAX ] ; static bool [ ] visited = new bool [ MAX ] ; static List < long > ans = new List < long > ( ) ; static void power_2 ( ) { long ans = 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { pow2 [ i ] = ans ; ans *= 2 ; } } static int countSetBits ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } static void add ( long num ) { long value = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( visited [ i ] ) continue ; else { if ( num % 2 == 1 ) { value += ( 1 << i ) ; } num /= 2 ; } } ans . Add ( value ) ; } static void solve ( long n , long k ) { ans . Add ( k ) ; int countk = countSetBits ( k ) ; if ( pow2 [ countk ] < n ) { Console . WriteLine ( - 1 ) ; return ; } int count = 0 ; for ( long i = 0 ; i < pow2 [ countk ] - 1 ; i ++ ) { add ( i ) ; count ++ ; if ( count == n ) break ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( ) { long n = 3 , k = 5 ; power_2 ( ) ; solve ( n , k ) ; } }
Count of all possible values of X such that A % X = B | C # implementation of the approach ; Function to return the count of all possible values for x such that ( A % x ) = B ; Case 1 ; Case 2 ; Case 3 ; Find the number of divisors of x which are greater than b ; Driver code
using System ; class GFG { static int countX ( int a , int b ) { if ( b > a ) return 0 ; else if ( a == b ) return - 1 ; else { int x = a - b , ans = 0 ; for ( int i = 1 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { int d1 = i , d2 = b - 1 ; if ( i * i != x ) d2 = x / i ; if ( d1 > b ) ans ++ ; if ( d2 > b ) ans ++ ; } } return ans ; } } static public void Main ( ) { int a = 21 , b = 5 ; Console . WriteLine ( countX ( a , b ) ) ; } }
Find the sum of numbers from 1 to n excluding those which are powers of K | C # implementation of the approach ; Function to return the sum of all the powers of k from the range [ 1 , n ] ; To store the sum of the series ; While current power of k <= n ; Add current power to the sum ; Next power of k ; Return the sum of the series ; Find to return the sum of the elements from the range [ 1 , n ] excluding those which are powers of k ; Sum of all the powers of k from [ 1 , n ] ; Sum of all the elements from [ 1 , n ] ; Return the required sum ; Driver code
using System ; class GFG { static long sumPowersK ( long n , long k ) { long sum = 0 , num = 1 ; while ( num <= n ) { sum += num ; num *= k ; } return sum ; } static long getSum ( long n , long k ) { long pwrK = sumPowersK ( n , k ) ; long sumAll = ( n * ( n + 1 ) ) / 2 ; return ( sumAll - pwrK ) ; } public static void Main ( ) { long n = 10 , k = 3 ; Console . WriteLine ( getSum ( n , k ) ) ; } }
Maximum number of people that can be killed with strength P | C # implementation of the approach ; Function to return the maximum number of people that can be killed ; Loop will break when the ith person cannot be killed ; Driver code
using System ; class GFG { static int maxPeople ( int p ) { int tmp = 0 , count = 0 ; for ( int i = 1 ; i * i <= p ; i ++ ) { tmp = tmp + ( i * i ) ; if ( tmp <= p ) count ++ ; else break ; } return count ; } public static void Main ( ) { int p = 14 ; Console . WriteLine ( maxPeople ( p ) ) ; } }
Maximum number of people that can be killed with strength P | C # implementation of the approach ; Function to return the maximum number of people that can be killed ; Storing the sum beforehand so that it can be used in each query ; lower_bound returns an iterator pointing to the first element greater than or equal to your val ; Previous value ; Returns the index in array upto which killing is possible with strength P ; Driver code
using System ; public class GFG { static int kN = 1000000 ; static int maxPeople ( int p ) { long [ ] sums = new long [ kN ] ; sums [ 0 ] = 0 ; for ( int i = 1 ; i < kN ; i ++ ) sums [ i ] = ( long ) ( i * i ) + sums [ i - 1 ] ; int it = lower_bound ( sums , 0 , kN , p ) ; if ( it > p ) { -- it ; } return it ; } private static int lower_bound ( long [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } public static void Main ( String [ ] args ) { int p = 14 ; Console . WriteLine ( maxPeople ( p ) ) ; } }
Maximum number of people that can be killed with strength P | C # implementation of the approach ; Helper function which returns the sum of series ( 1 ^ 2 + 2 ^ 2 + ... + n ^ 2 ) ; maxPeople function which returns appropriate value using Binary Search in O ( logn ) ; Set the lower and higher values ; Calculate the mid using low and high ; Compare value with n ; Return the ans ; Driver code
using System ; class GFG { static long squareSeries ( long n ) { return ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; } static long maxPeople ( long n ) { long low = 0 ; long high = 1000000L ; long ans = 0L ; while ( low <= high ) { long mid = low + ( ( high - low ) / 2 ) ; long value = squareSeries ( mid ) ; if ( value <= n ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { long p = 14 ; Console . Write ( maxPeople ( p ) ) ; } }
Find the index which is the last to be reduced to zero after performing a given operation | C # implementation of the approach ; Function that returns the index which will be the last to become zero after performing given operation ; Initialize the result ; Finding the ceil value of each index ; Finding the index with maximum ceil value ; Driver code
using System ; class GFG { static int findIndex ( int [ ] a , int n , int k ) { int index = - 1 , max_ceil = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + k - 1 ) / k ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= max_ceil ) { max_ceil = a [ i ] ; index = i ; } } return index ; } static public void Main ( ) { int [ ] arr = { 31 , 12 , 25 , 27 , 32 , 19 } ; int K = 5 ; int N = arr . Length ; Console . WriteLine ( findIndex ( arr , N , K ) ) ; } }
Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree | C # implementation of the approach ; Function to solve queries of the maximum xor value between the nodes in a given level L of a perfect binary tree and a given value X ; Initialize result ; Initialize array to store bits ; Initialize a copy of X and size of array ; Storing the bits of X in the array a [ ] ; Filling the array b [ ] ; Initializing variable which gives maximum xor ; Getting the maximum xor value ; Return the result ; Driver code ; Perform queries
using System ; class GFG { static int MAXN = 60 ; static int solveQuery ( int L , int X ) { int res ; int [ ] a = new int [ MAXN ] ; int [ ] b = new int [ L ] ; int refer = X , size_a = 0 ; while ( refer > 0 ) { a [ size_a ] = refer % 2 ; refer /= 2 ; size_a ++ ; } for ( int i = 0 ; i < Math . Min ( size_a , L ) ; i ++ ) { if ( a [ i ] == 1 ) b [ i ] = 0 ; else b [ i ] = 1 ; } for ( int i = Math . Min ( size_a , L ) ; i < L ; i ++ ) b [ i ] = 1 ; b [ L - 1 ] = 1 ; int temp = 0 , p = 1 ; for ( int i = 0 ; i < L ; i ++ ) { temp += b [ i ] * p ; p *= 2 ; } res = temp ^ X ; return res ; } static public void Main ( ) { int [ , ] queries = { { 2 , 5 } , { 3 , 15 } } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( solveQuery ( queries [ i , 0 ] , queries [ i , 1 ] ) ) ; } }
Count index pairs which satisfy the given condition | C # implementation of the approach ; Function to return the count of required index pairs ; To store the required count ; Array to store the left elements upto which current element is maximum ; Iterating through the whole permutation except first and last element ; If current element can be maximum in a subsegment ; Current maximum ; Iterating for smaller values then current maximum on left of it ; Storing left borders of the current maximum ; Iterating for smaller values then current maximum on right of it ; Condition satisfies ; Return count of subsegments ; Driver Code
using System ; class GFG { static int Count_Segment ( int [ ] p , int n ) { int count = 0 ; int [ ] upto = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) upto [ i ] = 0 ; int j = 0 , curr = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( p [ i ] > p [ i - 1 ] && p [ i ] > p [ i + 1 ] ) { curr = p [ i ] ; j = i - 1 ; while ( j >= 0 && p [ j ] < curr ) { upto [ p [ j ] ] = curr ; j = j - 1 ; } j = i + 1 ; while ( j < n && p [ j ] < curr ) { if ( upto [ curr - p [ j ] ] == curr ) count += 1 ; j = j + 1 ; } } } return count ; } static public void Main ( ) { int [ ] p = { 3 , 4 , 1 , 5 , 2 } ; int n = p . Length ; Console . WriteLine ( Count_Segment ( p , n ) ) ; } }
Check whether a number can be represented as sum of K distinct positive integers | C # implementation of the approach ; Function that returns true if n can be represented as the sum of exactly k distinct positive integers ; If n can be represented as 1 + 2 + 3 + ... + ( k - 1 ) + ( k + x ) ; Driver code
using System ; class GFG { static bool solve ( int n , int k ) { if ( n >= ( k * ( k + 1 ) ) / 2 ) { return true ; } return false ; } static public void Main ( ) { int n = 12 , k = 4 ; if ( solve ( n , k ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check whether product of integers from a to b is positive , negative or zero | C # implementation of the approach ; Function to check whether the product of integers of the range [ a , b ] is positive , negative or zero ; If both a and b are positive then the product will be positive ; If a is negative and b is positive then the product will be zero ; If both a and b are negative then we have to find the count of integers in the range ; Total integers in the range ; If n is even then the resultant product is positive ; If n is odd then the resultant product is negative ; Driver code
using System ; class GFG { static void solve ( long a , long b ) { if ( a > 0 && b > 0 ) { Console . WriteLine ( " Positive " ) ; } else if ( a <= 0 && b >= 0 ) { Console . WriteLine ( " Zero " ) ; } else { long n = Math . Abs ( a - b ) + 1 ; if ( n % 2 == 0 ) { Console . WriteLine ( " Positive " ) ; } else { Console . WriteLine ( " Negative " ) ; } } } public static void Main ( ) { int a = - 10 , b = - 2 ; solve ( a , b ) ; } }
Bitwise AND of sub | C # implementation of the approach ; Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; Driver code
using System ; class GFG { static int closetAND ( int [ ] arr , int n , int k ) { int ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . Min ( ans , Math . Abs ( k - X ) ) ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 4 , 7 , 10 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( closetAND ( arr , n , k ) ) ; } }
Program to find the rate percentage from compound interest of consecutive years | C # implementation of the approach ; Function to return the required rate percentage ; Driver code
using System ; class GFG { static int Rate ( int N1 , int N2 ) { float rate = ( N2 - N1 ) * 100 / N1 ; return ( int ) rate ; } static public void Main ( ) { int N1 = 100 , N2 = 120 ; Console . WriteLine ( Rate ( N1 , N2 ) + " ▁ % " ) ; } }
Find prime number K in an array such that ( A [ i ] % K ) is maximum | C # implementation of the approach ; Function to return the required prime number from the array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; To store the maximum prime number ; If current element is prime then update the maximum prime ; Return the maximum prime number from the array ; Driver code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int getPrime ( int [ ] arr , int n ) { int max_val = arr . Max ( ) ; List < Boolean > prime = new List < Boolean > ( max_val + 1 ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) prime . Insert ( i , true ) ; prime . Insert ( 1 , false ) ; prime . Insert ( 2 , false ) ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime . Insert ( i , false ) ; } } int maximum = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { maximum = Math . Max ( maximum , arr [ i ] ) ; } } return maximum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 10 , 15 , 7 , 6 , 8 , 13 } ; int n = arr . Length ; Console . WriteLine ( getPrime ( arr , n ) ) ; } }
Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [ 2 , N ] | C # implementation of the approach ; Function to return the smallest number which on dividing with any element from the range [ 2 , N ] leaves a remainder of 1 ; Find the LCM of the elements from the range [ 2 , N ] ; Return the required number ; Driver code
using System ; class GFG { static long getMinNum ( int N ) { int lcm = 1 ; for ( int i = 2 ; i <= N ; i ++ ) lcm = ( ( i * lcm ) / ( __gcd ( i , lcm ) ) ) ; return ( lcm + 1 ) ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void Main ( ) { int N = 5 ; Console . WriteLine ( getMinNum ( N ) ) ; } }
Maximum number of edges in Bipartite graph | C # implementation of the approach ; Function to return the maximum number of edges possible in a Bipartite graph with N vertices ; Driver code
using System ; class GFG { static double maxEdges ( double N ) { double edges = 0 ; edges = Math . Floor ( ( N * N ) / 4 ) ; return edges ; } static public void Main ( ) { double N = 5 ; Console . WriteLine ( maxEdges ( N ) ) ; } }
Check if the robot is within the bounds of the grid after given moves | C # implementation of the approach ; Function that returns true if the robot is safe ; If current move is " L " then increase the counter of coll ; If value of coll is equal to column then break ; If current move is " R " then increase the counter of colr ; If value of colr is equal to column then break ; If current move is " U " then increase the counter of rowu ; If value of rowu is equal to row then break ; If current move is " D " then increase the counter of rowd ; If value of rowd is equal to row then break ; If robot is within the bounds of the grid ; Unsafe ; Driver code
using System ; class GFG { static bool isSafe ( int N , int M , char [ ] str ) { int coll = 0 , colr = 0 , rowu = 0 , rowd = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' L ' ) { coll ++ ; if ( colr > 0 ) { colr -- ; } if ( coll == M ) { break ; } } else if ( str [ i ] == ' R ' ) { colr ++ ; if ( coll > 0 ) { coll -- ; } if ( colr == M ) { break ; } } else if ( str [ i ] == ' U ' ) { rowu ++ ; if ( rowd > 0 ) { rowd -- ; } if ( rowu == N ) { break ; } } else if ( str [ i ] == ' D ' ) { rowd ++ ; if ( rowu > 0 ) { rowu -- ; } if ( rowd == N ) { break ; } } } if ( Math . Abs ( rowd ) < N && Math . Abs ( rowu ) < N && Math . Abs ( coll ) < M && Math . Abs ( colr ) < M ) { return true ; } return false ; } public static void Main ( String [ ] args ) { int N = 1 , M = 1 ; String str = " R " ; if ( isSafe ( N , M , str . ToCharArray ( ) ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find permutation of first N natural numbers that satisfies the given condition | C # implementation of the approach ; Function to find permutation ( p ) of first N natural numbers such that there are exactly K elements of permutation such that GCD ( p [ i ] , i ) > 1 ; First place all the numbers in their respective places ; Modify for first n - k integers ; In first index place n - k ; Print the permutation ; Driver code
using System ; class GFG { static void Permutation ( int n , int k ) { int [ ] p = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) p [ i ] = i ; for ( int i = 1 ; i < n - k ; i ++ ) p [ i + 1 ] = i ; p [ 1 ] = n - k ; for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( p [ i ] + " ▁ " ) ; } static public void Main ( ) { int n = 5 , k = 2 ; Permutation ( n , k ) ; } }
Count number of 1 s in the array after N moves | C # implementation of the above approach ; Function to count number of 1 's in the  array after performing N moves ; If index is multiple of move number ; arr [ j - 1 ] = 1 ; Convert 0 to 1 ; arr [ j - 1 ] = 0 ; Convert 1 to 0 ; Count number of 1 's ; count ++ ; count number of 1 's ; Driver Code ; Initialize all elements to 0
using System ; class GFG { static int countOnes ( int [ ] arr , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { if ( j % i == 0 ) { if ( arr [ j - 1 ] == 0 ) { } else { } } } } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = new int [ 10 ] ; int ans = countOnes ( arr , N ) ; Console . WriteLine ( ans ) ; } }
Number of positions such that adding K to the element is greater than sum of all other elements | C # implementation of the approach ; Function that will find out the valid position ; find sum of all the elements ; adding K to the element and check whether it is greater than sum of all other elements ; Driver code
using System ; class GFG { static int validPosition ( int [ ] arr , int N , int K ) { int count = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] + K ) > ( sum - arr [ i ] ) ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 6 , 7 } ; int K = 4 ; int N = arr . Length ; Console . WriteLine ( validPosition ( arr , N , K ) ) ; } }
Count unique numbers that can be generated from N by adding one and removing trailing zeros | C # implementation of the approach ; Function to count the unique numbers ; If the number has already been visited ; Insert the number to the set ; First step ; Second step remove trailing zeros ; Recur again for the new number ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void count_unique ( HashSet < int > s , int n ) { if ( s . Contains ( n ) ) return ; s . Add ( n ) ; n += 1 ; while ( n % 10 == 0 ) { n = n / 10 ; } count_unique ( s , n ) ; } public static void Main ( String [ ] args ) { int n = 10 ; HashSet < int > s = new HashSet < int > ( ) ; count_unique ( s , n ) ; Console . WriteLine ( s . Count ) ; } }
Find element with the maximum set bits in an array | C # implementation of the approach ; Function to return the element from the array which has the maximum set bits ; To store the required element and the maximum set bits so far ; Count of set bits in the current element ; Update the max ; Driver code
using System ; class GFG { static int maxBitElement ( int [ ] arr , int n ) { int num = 0 , max = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = BitCount ( arr [ i ] ) ; if ( cnt > max ) { max = cnt ; num = arr [ i ] ; } } return num ; } static int BitCount ( int n ) { int count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 } ; int n = arr . Length ; Console . Write ( maxBitElement ( arr , n ) ) ; } }