codeparrot/xlcost-text-to-code huggingface dataset:
Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values | C # program to implement the above approach ; Function to calculate Bitwise OR from given bitwise XOR and bitwise AND values ; Driver Code
using System ; class GFG { static int findBitwiseORGivenXORAND ( int X , int Y ) { return X + Y ; } public static void Main ( string [ ] args ) { int X = 5 , Y = 2 ; Console . Write ( findBitwiseORGivenXORAND ( X , Y ) ) ; } }
Check if a given value can be reached from another value in a Circular Queue by K | C # program for the above approach ; Function to return GCD of two numbers a and b ; Base Case ; Recursively Find the GCD ; Function to check of B can be reaced from A with a jump of K elements in the circular queue ; Find GCD of N and K ; If A - B is divisible by gcd then print Yes ; Otherwise ; Driver Code ; Function Call
using System ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void canReach ( int N , int A , int B , int K ) { int gcd = GCD ( N , K ) ; if ( Math . Abs ( A - B ) % gcd == 0 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( ) { int N = 5 , A = 2 , B = 1 , K = 2 ; canReach ( N , A , B , K ) ; } }
Count of subarrays having sum equal to its length | Set 2 | C # program for the above approach ; Function that counts the subarrays with sum of its elements as its length ; Store count of elements upto current element with length i ; Stores the readonly count of subarray ; Stores the prefix sum ; If size of subarray is 1 ; Iterate the array ; Find the sum ; Update frequency in map ; Print the total count ; Driver Code ; Given array [ ] arr ; Size of array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void countOfSubarray ( int [ ] arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int answer = 0 ; int sum = 0 ; mp [ 1 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; if ( mp . ContainsKey ( sum - i ) ) answer += mp [ sum - i ] ; if ( mp . ContainsKey ( sum - 1 ) ) mp [ sum - 1 ] ++ ; else mp [ sum - 1 ] = 1 ; } Console . Write ( answer - 2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 2 , 1 , 2 , - 2 , 2 , 4 } ; int N = arr . Length ; countOfSubarray ( arr , N ) ; } }
Split first N natural numbers into two sets with minimum absolute difference of their sums | C # program to implement the above approach ; Function to split the first N natural numbers into two sets having minimum absolute difference of their sums ; Stores the sum of elements of set1 ; Stores the sum of elements of set2 ; Traverse first N natural numbers ; Check if sum of elements of set1 is less than or equal to sum of elements of set2 ; Driver code
using System ; class GFG { static int minAbsDiff ( int N ) { int sumSet1 = 0 ; int sumSet2 = 0 ; for ( int i = N ; i > 0 ; i -- ) { if ( sumSet1 <= sumSet2 ) { sumSet1 += i ; } else { sumSet2 += i ; } } return Math . Abs ( sumSet1 - sumSet2 ) ; } static void Main ( ) { int N = 6 ; Console . Write ( minAbsDiff ( N ) ) ; } }
Check if a number is prime in Flipped Upside Down , Mirror Flipped and Mirror Flipped Upside Down | C # program to implement the above approach ; Function to check if N contains digits 0 , 1 , 2 , 5 , 8 only ; Extract digits of N ; Return false if any of these digits are present ; Function to check if N is prime or not ; Check for all factors ; Function to check if n is prime in all the desired forms ; Driver Code
using System ; class GFG { static bool checkDigits ( int n ) { do { int r = n % 10 ; if ( r == 3 r == 4 r == 6 r == 7 r == 9 ) return false ; n /= 10 ; } while ( n != 0 ) ; return true ; } static bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static bool isAllPrime ( int n ) { return isPrime ( n ) && checkDigits ( n ) ; } public static void Main ( String [ ] args ) { int N = 101 ; if ( isAllPrime ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Minimum Cost required to generate a balanced Bracket Sequence | C # program to implement the above approach ; Function to calculate the minimum cost required to generate a balanced bracket sequence ; Stores the count of unbalanced open brackets ; Stores the count of unbalanced closed brackets ; Stores the count of open brackets ; Stores the count of closed brackets ; If open brace is encountered ; Otherwise ; If no unbalanced open brackets are present ; Increase count of unbalanced closed brackets ; Otherwise ; Reduce count of unbalanced open brackets ; Increase count of closed brackets ; Calculate lower bound of minimum cost ; Reduce excess open or closed brackets to prevent counting them twice ; Update answer by adding minimum of removing both unbalanced open and closed brackets or inserting closed unbalanced brackets to end of String ; Print the result ; Driver Code
using System ; class GFG { static void minCost ( String str , int a , int b ) { int openUnbalanced = 0 ; int closedUnbalanced = 0 ; int openCount = 0 ; int closedCount = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' ( ' ) { openUnbalanced ++ ; openCount ++ ; } else { if ( openUnbalanced == 0 ) closedUnbalanced ++ ; else openUnbalanced -- ; closedCount ++ ; } } int result = a * ( Math . Abs ( openCount - closedCount ) ) ; if ( closedCount > openCount ) closedUnbalanced -= ( closedCount - openCount ) ; if ( openCount > closedCount ) openUnbalanced -= ( openCount - closedCount ) ; result += Math . Min ( a * ( openUnbalanced + closedUnbalanced ) , b * closedUnbalanced ) ; Console . Write ( result + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String str = " ) ) ( ) ( ( ) ( ) ( " ; int A = 1 , B = 3 ; minCost ( str , A , B ) ; } }
Count of permutations such that sum of K numbers from given range is even | C # program for the above approach ; Function to return the number of all permutations such that sum of K numbers in range is even ; Find total count of even and odd number in given range ; Iterate loop k times and update even_sum & odd_sum using previous values ; Update the prev_even and odd_sum ; Even sum ; Odd sum ; Return even_sum ; Driver Code ; Given ranges ; Length of permutation ; Function call
using System ; class GFG { public static void countEvenSum ( int low , int high , int k ) { int even_count = high / 2 - ( low - 1 ) / 2 ; int odd_count = ( high + 1 ) / 2 - low / 2 ; long even_sum = 1 ; long odd_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { long prev_even = even_sum ; long prev_odd = odd_sum ; even_sum = ( prev_even * even_count ) + ( prev_odd * odd_count ) ; odd_sum = ( prev_even * odd_count ) + ( prev_odd * even_count ) ; } Console . WriteLine ( even_sum ) ; } public static void Main ( String [ ] args ) { int low = 4 ; int high = 5 ; int K = 3 ; countEvenSum ( low , high , K ) ; } }
Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2 | C # Program to implement the above approach ; Function to count the number of N - digit numbers such that sum of every K consecutive digits are equal ; Print the answer ; Driver Code
using System ; class GFG { public static void count ( int n , int k ) { long count = ( long ) ( Math . Pow ( 10 , k ) - Math . Pow ( 10 , k - 1 ) ) ; Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int n = 2 , k = 1 ; count ( n , k ) ; } }
Sum of largest divisor of numbers upto N not divisible by given prime number P | C # program for the above approach ; Function to find the sum of largest divisors of numbers in range 1 to N not divisible by prime number P ; Total sum upto N ; If no multiple of P exist up to N ; If only P itself is in the range from 1 to N ; Sum of those that are divisible by P ; Recursively function call to find the sum for N / P ; Driver Code ; Given N and P ; Function call
using System ; class GFG { static int func ( int N , int P ) { int sumUptoN = ( N * ( N + 1 ) / 2 ) ; int sumOfMultiplesOfP ; if ( N < P ) { return sumUptoN ; } else if ( ( N / P ) == 1 ) { return sumUptoN - P + 1 ; } sumOfMultiplesOfP = ( ( N / P ) * ( 2 * P + ( N / P - 1 ) * P ) ) / 2 ; return ( sumUptoN + func ( N / P , P ) - sumOfMultiplesOfP ) ; } public static void Main ( String [ ] args ) { int N = 10 , P = 5 ; Console . WriteLine ( func ( N , P ) ) ; } }
Count of right shifts for each array element to be in its sorted position | C # program to implement the approach ; Function to find the right shifts required for each element to reach its sorted array position in [ ] A ; Stores required number of shifts for each element ; If the element is at sorted position ; Otherwise ; Calculate right shift ; Print the respective shifts ; Driver code
using System ; class GFG { public static void findShifts ( int [ ] A , int N ) { int [ ] shift = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == A [ i ] - 1 ) shift [ i ] = 0 ; else shift [ i ] = ( A [ i ] - 1 - i + N ) % N ; } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( shift [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 3 , 2 , 5 } ; int N = arr . Length ; findShifts ( arr , N ) ; } }
Construct a matrix with sum equal to the sum of diagonal elements | C # program to implement the above approach ; Function to construct matrix with diagonal sum equal to matrix sum ; If diagonal position ; Positive element ; Negative element ; Driver Code
using System ; class GFG { public static void constructmatrix ( int N ) { bool check = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) { Console . Write ( "1 ▁ " ) ; } else if ( check ) { Console . Write ( "2 ▁ " ) ; check = false ; } else { Console . Write ( " - 2 ▁ " ) ; check = true ; } } Console . WriteLine ( ) ; } } static public void Main ( ) { int N = 5 ; constructmatrix ( N ) ; } }
Minimum count of numbers required with unit digit X that sums up to N | C # Program to implement the above approach ; Function to calculate and return the minimum number of times a number with unit digit X needs to be added to get a sum N ; Calculate the number of additions required to get unit digit of N ; If unit digit of N cannot be obtained ; Function to return the minimum number required to represent N ; Stores unit digit of N ; Stores minimum addition of X required to obtain unit digit of N ; If unit digit of N cannot be obtained ; Otherwise ; If N is greater than or equal to ( X * times ) ; Minimum count of numbers that needed to represent N ; Representation not possible ; Driver Code
using System ; class GFG { static int check ( int unit_digit , int X ) { int times , digit ; for ( times = 1 ; times <= 10 ; times ++ ) { digit = ( X * times ) % 10 ; if ( digit == unit_digit ) return times ; } return - 1 ; } static int getNum ( int N , int X ) { int unit_digit ; unit_digit = N % 10 ; int times = check ( unit_digit , X ) ; if ( times == - 1 ) return times ; else { if ( N >= ( times * X ) ) return times ; else return - 1 ; } } public static void Main ( ) { int N = 58 , X = 7 ; Console . Write ( getNum ( N , X ) ) ; } }
Minimum number of points required to cover all blocks of a 2 | C # program for the above approach ; Function to find the minimum number of Points required to cover a grid ; If number of block is even ; Return the minimum points ; Driver Code ; Given size of grid ; Function Call
using System ; class GFG { static int minPoints ( int n , int m ) { int ans = 0 ; if ( ( n % 2 != 0 ) && ( m % 2 != 0 ) ) { ans = ( ( n * m ) / 2 ) + 1 ; } else { ans = ( n * m ) / 2 ; } return ans ; } public static void Main ( String [ ] args ) { int N = 5 , M = 7 ; Console . Write ( minPoints ( N , M ) ) ; } }
Largest lexicographical string with at most K consecutive elements | C # code for the above approach ; Function to find the largest lexicographical String with given constraints . ; List containing frequency of each character . ; Assigning frequency ; Empty String of String class type ; Loop to iterate over maximum priority first . ; If frequency is greater than or equal to k . ; Temporary variable to operate in - place of k . ; Concatenating with the resultant String ans . ; Handling k case by adjusting with just smaller priority element . ; Condition to verify if index j does have frequency greater than 0 ; ; If no such element is found than String can not be processed further . ; If frequency is greater than 0 and less than k . ; Here we don 't need to fix K  consecutive element criteria. ; Otherwise check for next possible element . ; Driver code
using System ; class GFG { static String getLargestString ( String s , int k ) { int [ ] frequency_array = new int [ 26 ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { frequency_array [ s [ i ] - ' a ' ] ++ ; } String ans = " " ; for ( int i = 25 ; i >= 0 ; ) { if ( frequency_array [ i ] > k ) { int temp = k ; String st = String . Join ( " " , ( char ) ( i + ' a ' ) ) ; while ( temp > 0 ) { ans += st ; temp -- ; } frequency_array [ i ] -= k ; int j = i - 1 ; while ( frequency_array [ j ] <= 0 && j >= 0 ) { j -- ; } if ( frequency_array [ j ] > 0 && j >= 0 ) { String str = String . Join ( " " , ( char ) ( j + ' a ' ) ) ; ans += str ; frequency_array [ j ] -= 1 ; } else { break ; } } else if ( frequency_array [ i ] > 0 ) { int temp = frequency_array [ i ] ; frequency_array [ i ] -= temp ; String st = String . Join ( " " , ( char ) ( i + ' a ' ) ) ; while ( temp > 0 ) { ans += st ; temp -- ; } } else { i -- ; } } return ans ; } public static void Main ( String [ ] args ) { String S = " xxxxzza " ; int k = 3 ; Console . Write ( getLargestString ( S , k ) ) ; } }
Minimum operations to make all elements equal using the second array | C # implementation to find the minimum operations make all elements equal using the second array ; Function to find the minimum operations required to make all elements of the array equal ; Minimum element of A [ ] ; Traverse through all final values ; Variable indicating whether all elements can be converted to x or not ; Total operations ; Traverse through all array elements ; All elements can 't  be converted to x ; Driver Code
using System ; using System . Linq ; class GFG { static int minOperations ( int [ ] a , int [ ] b , int n ) { int minA = a . Max ( ) ; for ( int x = minA ; x >= 0 ; x -- ) { bool check = true ; int operations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( x % b [ i ] == a [ i ] % b [ i ] ) { operations += ( a [ i ] - x ) / b [ i ] ; } else { check = false ; break ; } } if ( check ) return operations ; } return - 1 ; } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] A = { 5 , 7 , 10 , 5 , 15 } ; int [ ] B = { 2 , 2 , 1 , 3 , 5 } ; Console . WriteLine ( minOperations ( A , B , N ) ) ; } }
Find the maximum sum ( a + b ) for a given input integer N satisfying the given condition | C # implementation to find the largest value of a + b satisfying the given condition ; Function to return the maximum sum of a + b satisfying the given condition ; Initialize max_sum ; Consider all the possible pairs ; Check if the product is divisible by the sum ; Storing the maximum sum in the max_sum variable ; Return the max_sum value ; Driver code
using System ; class GFG { static int getLargestSum ( int N ) { int max_sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i + 1 ; j <= N ; j ++ ) { if ( i * j % ( i + j ) == 0 ) max_sum = Math . Max ( max_sum , i + j ) ; } } return max_sum ; } public static void Main ( string [ ] args ) { int N = 25 ; int max_sum = getLargestSum ( N ) ; Console . WriteLine ( max_sum ) ; } }
Maximize the sum of array after multiplying a prefix and suffix by | C # implementation to find the maximum sum of the array by multiplying the prefix and suffix of the array by - 1 ; Kadane 's algorithm to find  the maximum subarray sum ; Loop to find the maximum subarray array sum in the given array ; Function to find the maximum sum of the array by multiplying the prefix and suffix by - 1 ; Total intital sum ; Loop to find the maximum sum of the array ; Maximum value ; Driver Code
using System ; class GFG { static int maxSubArraySum ( int [ ] a , int size ) { int max_so_far = int . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; } static int maxSum ( int [ ] a , int n ) { int S = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) S += a [ i ] ; int X = maxSubArraySum ( a , n ) ; return 2 * X - S ; } public static void Main ( String [ ] args ) { int [ ] a = { - 1 , - 2 , - 3 } ; int n = a . Length ; int max_sum = maxSum ( a , n ) ; Console . Write ( max_sum ) ; } }
Count of interesting primes upto N | C # program to find the number of interesting primes up to N ; Function to check if a number is prime or not ; If n is divisible by any number between 2 and Math . Sqrt ( n ) , it is not prime ; Function to check if a number is perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to find the number of interesting primes less than equal to N . ; Check whether the number is prime or not ; Iterate for values of b ; Check condition for a ; Return the required answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isPrime ( int n ) { int flag = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { flag = 0 ; break ; } } return ( flag == 1 ? true : false ) ; } static bool isPerfectSquare ( int x ) { double sr = Math . Sqrt ( x ) ; return ( ( sr - Math . Floor ( sr ) ) == 0 ) ; } static int countInterestingPrimes ( int n ) { int answer = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( isPrime ( i ) ) { for ( int j = 1 ; j * j * j * j <= i ; j ++ ) { if ( isPerfectSquare ( i - j * j * j * j ) ) { answer ++ ; break ; } } } } return answer ; } public static void Main ( String [ ] args ) { int N = 10 ; Console . Write ( countInterestingPrimes ( N ) ) ; } }
Maximize the number by flipping at most K bits | C # implementation of the approach ; Function to convert decimal number n to its binary representation stored as an array [ ] arr ; Function to convert the number represented as a binary array [ ] arr into its decimal equivalent ; Function to return the maximized number by flipping atmost k bits ; Number of bits in n ; Find the binary representation of n ; To count the number of 0 s flipped ; Return the decimal equivalent of the maximized number ; Driver code
using System ; class GFG { static void decBinary ( int [ ] arr , int n ) { int k = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; while ( n > 0 ) { arr [ k -- ] = n % 2 ; n /= 2 ; } } static int binaryDec ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += arr [ i ] << ( n - i - 1 ) ; return ans ; } static int maxNum ( int n , int k ) { int l = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; int [ ] a = new int [ l ] ; decBinary ( a , n ) ; int cn = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] == 0 && cn < k ) { a [ i ] = 1 ; cn ++ ; } } return binaryDec ( a , l ) ; } public static void Main ( String [ ] args ) { int n = 4 , k = 1 ; Console . WriteLine ( maxNum ( n , k ) ) ; } }
Find the subsequence with given sum in a superincreasing sequence | C # implementation of the approach ; Function to find the required subsequence ; Current element cannot be a part of the required subsequence ; Include current element in the required subsequence So update the sum ; Print the elements of the required subsequence ; If the current element was included in the subsequence ; Driver code
using System ; class GFG { static void findSubSeq ( int [ ] arr , int n , int sum ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( sum < arr [ i ] ) arr [ i ] = - 1 ; else sum -= arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != - 1 ) Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 17 , 25 , 46 , 94 , 201 , 400 } ; int n = arr . Length ; int sum = 272 ; findSubSeq ( arr , n , sum ) ; } }
Find the most valued alphabet in the String | C # implementation of the approach ; Function to return the maximum valued alphabet ; To store the first and the last occurrence of all the characters ; Set the first and the last occurrence of all the characters to - 1 ; Update the occurrences of the characters ; Only set the first occurrence if it hasn 't already been set ; To store the result ; For every alphabet ; If current alphabet doesn 't appear  in the given String ; If the current character has the highest value so far ; Driver code
using System ; class GFG { static int MAX = 26 ; static char maxAlpha ( String str , int len ) { int [ ] first = new int [ MAX ] ; int [ ] last = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( int i = 0 ; i < len ; i ++ ) { int index = ( str [ i ] - ' a ' ) ; if ( first [ index ] == - 1 ) first [ index ] = i ; last [ index ] = i ; } int ans = - 1 , maxVal = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( first [ i ] == - 1 ) continue ; if ( ( last [ i ] - first [ i ] ) > maxVal ) { maxVal = last [ i ] - first [ i ] ; ans = i ; } } return ( char ) ( ans + ' a ' ) ; } public static void Main ( String [ ] args ) { String str = " abbba " ; int len = str . Length ; Console . Write ( maxAlpha ( str , len ) ) ; } }
Queries for number of distinct elements from a given index till last index in an array | C # implementation of the approach ; Function to perform queries to find number of distinct elements from a given index till last index in an array ; Check if current element already visited or not ; If not visited store current counter and increment it and mark check as 1 ; Otherwise if visited simply store current counter ; Perform queries ; Driver code
using System ; class GFG { static int MAX = 100001 ; static void find_distinct ( int [ ] a , int n , int q , int [ ] queries ) { int [ ] check = new int [ MAX ] ; int [ ] idx = new int [ MAX ] ; int cnt = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( check [ a [ i ] ] == 0 ) { idx [ i ] = cnt ; check [ a [ i ] ] = 1 ; cnt ++ ; } else { idx [ i ] = cnt - 1 ; } } for ( int i = 0 ; i < q ; i ++ ) { int m = queries [ i ] ; Console . Write ( idx [ m ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 , 1 , 2 , 3 , 4 , 5 } ; int n = a . Length ; int [ ] queries = { 0 , 3 , 5 , 7 } ; int q = queries . Length ; find_distinct ( a , n , q , queries ) ; } }
Convert given integer X to the form 2 ^ N | C # implementation of the approach ; Function to return the count of operations required ; To store the powers of 2 ; Temporary variable to store x ; To store the index of smaller number larger than x ; To store the count of operations ; Stores the index of number in the form of 2 ^ n - 1 ; If x is already in the form 2 ^ n - 1 then no operation is required ; If number is less than x increase the index ; Calculate all the values ( x xor 2 ^ n - 1 ) for all possible n ; Only take value which is closer to the number ; If number is in the form of 2 ^ n - 1 then break ; Return the count of operations required to obtain the number ; Driver code
using System ; class GFG { static int MAX = 24 ; static int countOp ( int x ) { int [ ] arr = new int [ MAX ] ; arr [ 0 ] = 1 ; for ( int i = 1 ; i < MAX ; i ++ ) arr [ i ] = arr [ i - 1 ] * 2 ; int temp = x ; bool flag = true ; int ans = 0 ; int operations = 0 ; bool flag2 = false ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( arr [ i ] - 1 == x ) flag2 = true ; if ( arr [ i ] > x ) { ans = i ; break ; } } if ( flag2 ) return 0 ; while ( flag ) { if ( arr [ ans ] < x ) ans ++ ; operations ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { int take = x ^ ( arr [ i ] - 1 ) ; if ( take <= arr [ ans ] - 1 ) { if ( take > temp ) temp = take ; } } if ( temp == arr [ ans ] - 1 ) { flag = false ; break ; } temp ++ ; operations ++ ; x = temp ; if ( x == arr [ ans ] - 1 ) flag = false ; } return operations ; } static public void Main ( ) { int x = 39 ; Console . WriteLine ( countOp ( x ) ) ; } }