codeparrot/xlcost-text-to-code huggingface dataset:
A Binary String Game | C # program for the above approach ; Function to return the result of the game ; length of the string ; List to maintain the lengths of consecutive '1' s in the string ; Variable that keeps a track of the current length of the block of consecutive '1' s ; Adds non - zero lengths ; This takes care of the case when the last character is '1' ; Sorts the lengths in descending order ; Scores of the 2 players ; For player 1 ; For player 2 ; In case of a tie ; Print the result ; Driver Code ; Given string S ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static String gameMax ( String S ) { int N = S . Length ; List < int > list = new List < int > ( ) ; int one = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '1' ) { one ++ ; } else { if ( one != 0 ) { list . Add ( one ) ; } one = 0 ; } } if ( one != 0 ) { list . Add ( one ) ; } list . Sort ( ) ; list . Reverse ( ) ; int score_1 = 0 , score_2 = 0 ; for ( int i = 0 ; i < list . Count ; i ++ ) { if ( list [ i ] % 2 == 1 ) { score_1 += list [ i ] ; } else { score_2 += list [ i ] ; } } if ( score_1 == score_2 ) return " - 1" ; return ( score_1 > score_2 ) ? " Player ▁ 1" : " Player ▁ 2" ; } public static void Main ( String [ ] args ) { String S = "11111101" ; Console . WriteLine ( gameMax ( S ) ) ; } }
Count of carry operations on adding two Binary numbers | C # program for the above approach ; Function to count the number of carry operations to add two binary numbers ; To Store the carry count ; Iterate till there is no carry ; Carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; Adding number of 1 's of  carry to readonly count ; Return the readonly count ; Driver Code ; Given two numbers ; Function call
using System ; class GFG { static int countSetBits ( int x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } static int carryCount ( int num1 , int num2 ) { int count = 0 ; while ( num2 != 0 ) { int carry = num1 & num2 ; num1 = num1 ^ num2 ; num2 = carry << 1 ; count += countSetBits ( num2 ) ; } return count ; } public static void Main ( String [ ] args ) { int A = 15 , B = 10 ; Console . Write ( carryCount ( A , B ) ) ; } }
Count of all possible ways to reach a target by a Knight | C # program to implement the above approach ; Function to return X ^ Y % Mod ; Base Case ; Function to return the inverse of factorial of N ; Base case ; Function to return factorial of n % Mod ; Base case ; Function to return the value of n ! / ( ( n - k ) ! * k ! ) ; Function to return the count of ways to reach ( n , m ) from ( 0 , 0 ) ; If ( N + M ) % 3 != 0 ; No possible way exists ; Calculate X and Y from the equations X + 2 Y = N and 2 X + Y == M ; Driver Code
using System ; class GFG { static int Mod = ( int ) ( 1e9 + 7 ) ; static int power ( int X , int Y , int Mod ) { if ( Y == 0 ) return 1 ; int p = power ( X , Y / 2 , Mod ) % Mod ; p = ( p * p ) % Mod ; if ( ( Y & 1 ) != 0 ) { p = ( X * p ) % Mod ; } return p ; } static int Inversefactorial ( int N ) { if ( N <= 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return power ( fact , Mod - 2 , Mod ) ; } static int factorial ( int N ) { if ( N <= 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return fact ; } static int nck ( int N , int K ) { int factN = factorial ( N ) ; int inv = Inversefactorial ( K ) ; int invFact = Inversefactorial ( N - K ) ; return ( ( ( factN * inv ) % Mod ) * invFact ) % Mod ; } static int TotalWaYs ( int N , int M ) { if ( ( ( N + M ) % 3 ) != 0 ) return 0 ; int X = N - ( N + M ) / 3 ; int Y = M - ( N + M ) / 3 ; if ( X < 0 Y < 0 ) return 0 ; return nck ( X + Y , Y ) ; } public static void Main ( String [ ] args ) { int N = 3 , M = 3 ; Console . Write ( TotalWaYs ( N , M ) ) ; } }
Lead a life problem | C # program for the above approach ; Function that calculates the profit with the earning and cost of expenses ; To store the total Profit ; Loop for n number of days ; If last day , no need to buy food ; Else buy food to gain energy for next day ; Update earning per day ; Update profit with daily spent ; Print the profit ; Driver Code ; Given days ; Given earnings ; Given cost ; Given energy e ; Function call
using System ; class GFG { static void calculateProfit ( int n , int [ ] earnings , int [ ] cost , int e ) { int profit = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int earning_per_day = 0 ; int daily_spent_food = 0 ; if ( i == ( n - 1 ) ) { earning_per_day = earnings [ i ] * e ; profit = profit + earning_per_day ; break ; } if ( cost [ i ] < earnings [ i ] ) { earning_per_day = earnings [ i ] * e ; daily_spent_food = cost [ i ] * e ; profit = profit + earning_per_day - daily_spent_food ; } } Console . Write ( profit + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] earnings = { 1 , 8 , 6 , 7 } ; int [ ] cost = { 1 , 3 , 4 , 1 } ; int e = 5 ; calculateProfit ( n , earnings , cost , e ) ; } }
Check if N numbers with Even Sum can be selected from a given Array | C # efficient program to check if N numbers with Odd sum can be selected from the given array ; Function to check if an odd sum can be made using N integers from the array ; Initialize odd and even counts ; Iterate over the array to count the no . of even and odd integers ; If element is odd ; If element is even ; Check if even_freq is more than N ; If odd_freq is odd ; Consider even count of odd ; Calculate even required ; If even count is less than required count ; Calculate even required ; If even count is less than required count ; Driver Code
using System ; class GFG { static bool checkEvenSum ( int [ ] arr , int N , int size ) { int even_freq = 0 , odd_freq = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd_freq ++ ; else even_freq ++ ; } if ( even_freq >= N ) return true ; else { if ( odd_freq % 2 == 1 ) { int taken = odd_freq - 1 ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } else { int taken = odd_freq ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 2 , 3 , 4 , 18 , 7 , 7 , 6 } ; int size = arr . Length ; int N = 5 ; if ( checkEvenSum ( arr , N , size ) ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else Console . Write ( " No " + " STRNEWLINE " ) ; } }
Minimum count of digits required to obtain given Sum | C # program to implement the above approach ; Function to print the minimum count of digits ; IF N is divisible by 9 ; Count of 9 's is the answer ; If remainder is non - zero ; Driver Code
using System ; class GFG { static void mindigits ( int n ) { if ( n % 9 == 0 ) { Console . WriteLine ( n / 9 ) ; } else { Console . WriteLine ( ( n / 9 ) + 1 ) ; } } public static void Main ( String [ ] args ) { int n1 = 24 ; int n2 = 18 ; mindigits ( n1 ) ; mindigits ( n2 ) ; } }
Count of all possible numbers not exceeding M having suffix N | C # program to implement the above approach ; Function to count the no . of digits of N ; Function to count all possible numbers having Suffix as N ; Difference of the A . P ; Count of the number of terms ; Driver code
using System ; class GFG { static int digitsOf ( int num ) { return num . ToString ( ) . Length ; } static int count ( int a , int tn ) { int diff = ( int ) Math . Pow ( 10 , digitsOf ( a ) ) ; return ( ( tn - a ) / diff ) + 1 ; } public static void Main ( String [ ] args ) { int n = 25 , m = 4500 ; Console . WriteLine ( count ( n , m ) ) ; } }
Minimum increment / decrement operations required on Array to satisfy given conditions | C # program for the above approach ; Function to find minimum number of operations to get desired array ; For odd ' i ' , sum of elements till ' i ' is positive ; If i is even and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; If i is odd and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; For odd ' i ' , sum of elements till ' i ' is negative ; Check if ' i ' is odd and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; Check if ' i ' is even and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; Return the minimum of the two ; Driver Code ; Given array [ ] arr ; Function call
using System ; class GFG { static int minOperations ( int [ ] a , int N ) { int num_of_ops1 , num_of_ops2 , sum ; num_of_ops1 = num_of_ops2 = sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 0 && sum >= 0 ) { num_of_ops1 += ( 1 + Math . Abs ( sum ) ) ; sum = - 1 ; } else if ( i % 2 == 1 && sum <= 0 ) { num_of_ops1 += ( 1 + Math . Abs ( sum ) ) ; sum = 1 ; } } sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 1 && sum >= 0 ) { num_of_ops2 += ( 1 + Math . Abs ( sum ) ) ; sum = - 1 ; } else if ( i % 2 == 0 && sum <= 0 ) { num_of_ops2 += ( 1 + Math . Abs ( sum ) ) ; sum = 1 ; } } return Math . Min ( num_of_ops1 , num_of_ops2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , - 4 , 5 , 0 , 1 } ; int N = arr . Length ; Console . Write ( minOperations ( arr , N ) ) ; } }
Check if all elements of a Circular Array can be made equal by increments of adjacent pairs | C # program to implement the above approach ; Function to check if all array elements can be made equal ; Stores the sum of even and odd array elements ; If index is odd ; Driver Code
using System ; class GFG { static bool checkEquall ( int [ ] arr , int N ) { int sumEven = 0 , sumOdd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) sumOdd += arr [ i ] ; else sumEven += arr [ i ] ; } if ( sumEven == sumOdd ) return true ; else return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 3 , 5 , 7 } ; int N = arr . Length ; if ( checkEquall ( arr , N ) ) Console . Write ( " YES " + " STRNEWLINE " ) ; else Console . Write ( " NO " + " STRNEWLINE " ) ; } }
Check if a number can be expressed as sum of two Perfect powers | C # program for the above approach ; Function that returns true if n can be written as a ^ m + b ^ n ; Taking isSum bool array for check the sum exist or not ; To store perfect squares ; Initially all sums as false ; If sum exist then push that sum into perfect square vector ; Mark all perfect powers as false ; Traverse each perfectPowers ; Calculating Sum with perfect powers array ; Driver Code ; Given number n ; Function call
using System ; using System . Collections . Generic ; class GFG { static bool isSumOfPower ( int n ) { bool [ ] isSum = new bool [ n + 1 ] ; List < int > perfectPowers = new List < int > ( ) ; perfectPowers . Add ( 1 ) ; for ( int i = 0 ; i < ( n + 1 ) ; i ++ ) { isSum [ i ] = false ; } for ( int i = 2 ; i < ( n + 1 ) ; i ++ ) { if ( isSum [ i ] == true ) { perfectPowers . Add ( i ) ; continue ; } for ( int j = i * i ; j > 0 && j < ( n + 1 ) ; j *= i ) { isSum [ j ] = true ; } } for ( int i = 0 ; i < perfectPowers . Count ; i ++ ) { isSum [ perfectPowers [ i ] ] = false ; } for ( int i = 0 ; i < perfectPowers . Count ; i ++ ) { for ( int j = i ; j < perfectPowers . Count ; j ++ ) { int sum = perfectPowers [ i ] + perfectPowers [ j ] ; if ( sum < ( n + 1 ) ) isSum [ sum ] = true ; } } return isSum [ n ] ; } public static void Main ( String [ ] args ) { int n = 9 ; if ( isSumOfPower ( n ) ) { Console . Write ( " true STRNEWLINE " ) ; } else { Console . Write ( " false STRNEWLINE " ) ; } } }
Minimum increments by index value required to obtain at least two equal Array elements | C # program to implement the above approach ; Function to update every element adding to it its index value ; Function to check if at least two elements are equal or not ; Count the frequency of arr [ i ] ; Function to calculate the number of increment operations required ; Stores the minimum number of steps ; Driver code
using System ; class GFG { static void update ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] += ( i + 1 ) ; } } static bool check ( int [ ] arr , int N ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { count ++ ; } } if ( count >= 2 ) { f = 1 ; break ; } } if ( f == 1 ) return true ; else return false ; } static void incrementCount ( int [ ] arr , int N ) { int min = 0 ; while ( check ( arr , N ) != true ) { update ( arr , N ) ; min ++ ; } Console . WriteLine ( min ) ; } public static void Main ( String [ ] args ) { int N = 3 ; int [ ] arr = { 12 , 8 , 4 } ; incrementCount ( arr , N ) ; } }
Rearrange array such that all even | C # Program to implement the above approach ; Function to check if it the array can be rearranged such such that every even indices contains an even element ; Stores the count of even elements ; Traverse array to count even numbers ; If even_no_count exceeds count of even indices ; Driver Code
using System ; class GFG { static void checkPossible ( int [ ] a ) { int even_no_count = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { if ( a [ i ] % 2 == 0 ) even_no_count ++ ; } if ( a . Length / 2 > even_no_count ) { Console . WriteLine ( " No " ) ; return ; } Console . WriteLine ( " Yes " ) ; int j = 0 ; for ( int i = 1 ; i < a . Length ; i += 2 ) { if ( a [ i ] % 2 == 0 ) continue ; else { while ( j < a . Length && a [ j ] % 2 != 0 ) j += 2 ; a [ i ] += a [ j ] ; a [ j ] = a [ i ] - a [ j ] ; a [ i ] -= a [ j ] ; } } for ( int i = 0 ; i < a . Length ; i ++ ) { Console . Write ( a [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 , 7 } ; checkPossible ( arr ) ; } }
Largest number M having bit count of N such that difference between their OR and XOR value is maximized | C # program for the above approach ; Function to find the largest number M having the same length in binary form as N such that the difference between N | M and N ^ M is maximum ; Find the most significant bit of N ; Initialize M ; Set all the bits of M ; Return the answer ; Driver Code ; Given number N ; Function call
using System ; class GFG { static int maxORminusXOR ( int N ) { int MSB = ( int ) Math . Ceiling ( Math . Log ( N ) ) ; int M = 0 ; for ( int i = 0 ; i <= MSB ; i ++ ) M += ( 1 << i ) ; return M ; } public static void Main ( String [ ] args ) { int N = 10 ; Console . Write ( maxORminusXOR ( N ) ) ; } }
Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle | C # program for the above approach ; Function to find the number of equilateral triangle formed within another triangle ; Check for the valid condition ; Number of triangles having upward peak ; Number of inverted triangles ; Total no . of K sized triangle ; Driver Code ; Given N and K ; Function Call
using System ; class GFG { static int No_of_Triangle ( int N , int K ) { if ( N < K ) return - 1 ; else { int Tri_up = 0 ; Tri_up = ( ( N - K + 1 ) * ( N - K + 2 ) ) / 2 ; int Tri_down = 0 ; Tri_down = ( ( N - 2 * K + 1 ) * ( N - 2 * K + 2 ) ) / 2 ; return Tri_up + Tri_down ; } } public static void Main ( String [ ] args ) { int N = 4 , K = 2 ; Console . Write ( No_of_Triangle ( N , K ) ) ; } }
Check if Array forms an increasing | C # program for the above approach ; Function to check if the given array forms an increasing decreasing sequence or vice versa ; Base Case ; First subarray is stricly increasing ; Check for strictly increasing condition & find the break point ; Check for strictly decreasing condition & find the break point ; If i is equal to length of array ; First subarray is strictly Decreasing ; Check for strictly increasing condition & find the break point ; Check for strictly increasing condition & find the break point ; If i is equal to length of array - 1 ; Condition if ar [ 0 ] == ar [ 1 ] ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static bool canMake ( int n , int [ ] ar ) { if ( n == 1 ) return true ; else { if ( ar [ 0 ] < ar [ 1 ] ) { int i = 1 ; while ( i < n && ar [ i - 1 ] < ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] > ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else if ( ar [ 0 ] > ar [ 1 ] ) { int i = 1 ; while ( i < n && ar [ i - 1 ] > ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] < ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else { for ( int i = 2 ; i < n ; i ++ ) { if ( ar [ i - 1 ] <= ar [ i ] ) return false ; } return true ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; if ( ! canMake ( n , arr ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Minimum Count of Bit flips required to make a Binary String Palindromic | C # program to implement the above approach ; Function to calculate the length of the binary string ; Length ; Right shift of n ; Increment the length ; Return the length ; Function to check if the bit present at i - th position is a set bit or not ; Returns true if the bit is set ; Function to count the minimum number of bit flips required ; Length of the binary form ; Number of flips ; Pointer to the LSB ; Pointer to the MSB ; Check if the bits are equal ; Decrementing the left pointer ; Incrementing the right pointer ; Returns the number of bits to flip . ; Driver Code
using System ; class GFG { static int check_length ( int n ) { int ans = 0 ; while ( n != 0 ) { n = n >> 1 ; ans ++ ; } return ans ; } static bool check_ith_bit ( int n , int i ) { return ( n & ( 1 << ( i - 1 ) ) ) != 0 ? true : false ; } static int no_of_flips ( int n ) { int len = check_length ( n ) ; int ans = 0 ; int right = 1 ; int left = len ; while ( right < left ) { if ( check_ith_bit ( n , right ) != check_ith_bit ( n , left ) ) ans ++ ; left -- ; right ++ ; } return ans ; } public static void Main ( String [ ] args ) { int n = 12 ; Console . WriteLine ( no_of_flips ( n ) ) ; } }
Split N into two integers whose addition to A and B makes them equal | C # program to implement the above approach ; Function to calculate the splitted numbers ; Calculate X ; If X is odd ; No pair is possible ; Otherwise ; Calculate X ; Calculate Y ; Driver code
using System ; class GFG { static void findPair ( int A , int B , int N ) { int X , Y ; X = N - B + A ; if ( X % 2 != 0 ) { Console . Write ( " - 1" ) ; } else { X = X / 2 ; Y = N - X ; Console . Write ( X + " ▁ " + Y ) ; } } public static void Main ( string [ ] args ) { int A = 1 ; int B = 3 ; int N = 4 ; findPair ( A , B , N ) ; } }
Find the amplitude and number of waves for the given array | C # program for the above approach ; Function to find the amplitude and number of waves for the given array ; Check for both sides adjacent elements that both must be less or both must be greater than current element ; Update amplitude with max value ; Print the Amplitude ; Driver Code ; Given array [ ] a ; Calculate number of waves ; Function Call
using System ; class GFG { static bool check ( int [ ] a , int n ) { int ma = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] > a [ i - 1 ] && a [ i + 1 ] < a [ i ] ) || ( a [ i ] < a [ i - 1 ] && a [ i + 1 ] > a [ i ] ) ) ma = Math . Max ( ma , Math . Abs ( a [ i ] - a [ i + 1 ] ) ) ; else return false ; } Console . Write ( " Amplitude ▁ = ▁ " + ma ) ; Console . WriteLine ( ) ; return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 1 , 5 , 0 , 7 , - 6 } ; int n = a . Length ; int wave = ( n - 1 ) / 2 ; if ( check ( a , n ) ) Console . Write ( " Waves ▁ = ▁ " + wave ) ; else Console . Write ( " - 1" ) ; } }
Find relative rank of each element in array | C # program to implement the above approach ; Function to find relative rank for each element in the array A [ ] ; Create Rank Array ; Stack to store numbers in non - decreasing order from right ; Push last element in stack ; Iterate from second last element to first element ; If current element is less than the top of stack and push A [ i ] in stack ; Rank is stack size - 1 for current element ; Pop elements from stack till current element is greater than the top ; Push current element in Stack ; Rank is stack size - 1 ; Print rank of all elements ; Driver Code ; Given array A [ ] ; Function call
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void findRank ( int [ ] A , int N ) { int [ ] rank = new int [ N ] ; Stack < int > s = new Stack < int > ( ) ; s . Push ( A [ N - 1 ] ) ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( A [ i ] < s . Peek ( ) ) { s . Push ( A [ i ] ) ; rank [ i ] = s . Count ( ) - 1 ; } else { while ( s . Count ( ) != 0 && A [ i ] >= s . Peek ( ) ) { s . Pop ( ) ; } s . Push ( A [ i ] ) ; rank [ i ] = s . Count ( ) - 1 ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( rank [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] A = new int [ ] { 1 , 2 , 3 , 5 , 4 } ; int N = A . Length ; findRank ( A , N ) ; } }
Count total set bits in all numbers from range L to R | C # program for the above approach ; Function that counts the set bits from 0 to N ; To store sum of set bits from 0 - N ; Until n >= to 2 ^ i ; This k will get flipped after 2 ^ i iterations ; Change is iterator from 2 ^ i to 1 ; This will loop from 0 to n for every bit position ; When change = 1 flip the bit ; Again set change to 2 ^ i ; Increment the position ; Function that counts the set bit in the range ( L , R ) ; Return the count ; Driver Code ; Given L and R ; Function Call
using System ; class GFG { static int countSetBit ( int n ) { int i = 0 ; int ans = 0 ; while ( ( 1 << i ) <= n ) { bool k = true ; int change = 1 << i ; for ( int j = 0 ; j <= n ; j ++ ) { ans += k == true ? 0 : 1 ; if ( change == 1 ) { k = ! k ; change = 1 << i ; } else { change -- ; } } i ++ ; } return ans ; } static int countSetBits ( int L , int R ) { return Math . Abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ; } public static void Main ( String [ ] args ) { int L = 3 , R = 5 ; Console . Write ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) + " STRNEWLINE " ) ; } }
Count total set bits in all numbers from range L to R | C # program for the above approach ; Function to count set bit in range ; Count Variable ; Find the set bit in Nth number ; If last bit is set ; Left sift by one bit ; Return count ; Driver Code ; Given Range L and R ; Function Call
using System ; class GFG { static int countSetBits ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { int n = i ; while ( n > 0 ) { count += ( n & 1 ) ; n = n >> 1 ; } } return count ; } public static void Main ( String [ ] args ) { int L = 3 , R = 5 ; Console . Write ( " Total ▁ set ▁ Bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) ) ; } }
Count total set bits in all numbers from range L to R | C # program for the above approach ; Function to count set bit in [ L , R ] ; Variable for count set bit in range ; Count set bit for all number in range ; Use inbuilt function ; Driver Code ; Given range L and R ; Function Call
using System ; class GFG { static int countSetBits ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { count += countSetBits ( i ) ; } return count ; } static int countSetBits ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int L = 3 , R = 5 ; Console . Write ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) ) ; } }