codeparrot/xlcost-text-to-code huggingface dataset:
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | C # Code in C # to tell if there exists a pair in array whose sum results in x . ; Function to print pairs ; initializing the rem values with 0 's. ; Perform the remainder operation only if the element is x , as numbers greater than x can 't  be used to get a sum x.  Updating the count of remainders. ; Traversing the remainder list from start to middle to find pairs ; The elements with remainders i and x - i will result to a sum of x . Once we get two elements which add up to x , we print x and break . ; Once we reach middle of remainder array , we have to do operations based on x . ; if x is even and we have more than 1 elements with remainder x / 2 , then we will have two distinct elements which add up to x . if we dont have more than 1 element , print " No " . ; When x is odd we continue the same process which we did in previous loop . ; Driver Code ; Function calling
using System ; class GFG { static void printPairs ( int [ ] a , int n , int x ) { int i ; int [ ] rem = new int [ x ] ; for ( i = 0 ; i < x ; i ++ ) { rem [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] < x ) { rem [ a [ i ] % x ] ++ ; } } for ( i = 1 ; i < x / 2 ; i ++ ) { if ( rem [ i ] > 0 && rem [ x - i ] > 0 ) { Console . Write ( " Yes " + " STRNEWLINE " ) ; break ; } } if ( i >= x / 2 ) { if ( x % 2 == 0 ) { if ( rem [ x / 2 ] > 1 ) { Console . Write ( " Yes " + " STRNEWLINE " ) ; } else { Console . Write ( " No " + " STRNEWLINE " ) ; } } else { if ( rem [ x / 2 ] > 0 && rem [ x - x / 2 ] > 0 ) { Console . Write ( " Yes " + " STRNEWLINE " ) ; } else { Console . WriteLine ( " No " + " STRNEWLINE " ) ; } } } } public static void Main ( string [ ] args ) { int [ ] A = { 1 , 4 , 45 , 6 , 10 , 8 } ; int n = 16 ; int arr_size = A . Length ; printPairs ( A , arr_size , n ) ; } }
Find remainder when a number A raised to N factorial is divided by P | C # program for the above approach ; Function to calculate factorial ; Calculating factorial ; Returning factorial ; Function to calculate resultant remainder ; Function call to calculate factorial of n ; Calculating remainder ; Returning resultant remainder ; Driver Code ; Given Input ; Function Call
using System ; public class GFG { static int fact ( int n ) { int ans = 1 ; for ( int i = 2 ; i <= n ; i ++ ) ans *= i ; return ans ; } static int remainder ( int n , int a , int p ) { int len = fact ( n ) ; int ans = 1 ; for ( int i = 1 ; i <= len ; i ++ ) ans = ( ans * a ) % p ; return ans ; } public static void Main ( string [ ] args ) { int A = 2 , N = 1 , P = 2 ; Console . WriteLine ( remainder ( N , A , P ) ) ; } }
Check if a number N can be expressed in base B | C # program for the above approach ; Function to check if a number N can be expressed in base B ; Check if n is greater than 0 ; Initialize a bool variable ; Check if digit is 0 or 1 ; Subtract the appropriate power of B from it and increment higher digit ; Driver Code ; Given number N and base B ; Function call
using System ; class GFG { static bool check ( int n , int w ) { int [ ] a = new int [ 105 ] ; int p = 0 ; while ( n > 0 ) { a [ p ++ ] = n % w ; n /= w ; } bool flag = true ; for ( int i = 0 ; i <= 100 ; i ++ ) { if ( a [ i ] == 0 a [ i ] == 1 ) continue ; else if ( a [ i ] == w a [ i ] == w - 1 ) a [ i + 1 ] ++ ; else flag = false ; } return flag ; } public static void Main ( String [ ] args ) { int B = 3 , N = 7 ; if ( check ( N , B ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Count of groups among N people having only one leader in each group | C # program for the above approach ; Function to find 2 ^ x using modular exponentiation ; Base cases ; If B is even ; If B is odd ; Function to count the number of ways to form the group having one leader ; Find 2 ^ ( N - 1 ) using modular exponentiation ; Count total ways ; Print the total ways ; Driver Code ; Given N number of peoples ; Function Call
using System ; class GFG { static long mod = 1000000007 ; static int exponentMod ( int A , int B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; long y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 ) ; y = ( y * y ) % mod ; } else { y = A % mod ; y = ( y * exponentMod ( A , B - 1 ) % mod ) % mod ; } return ( int ) ( ( y + mod ) % mod ) ; } static void countWays ( int N ) { long select = exponentMod ( 2 , N - 1 ) ; long ways = ( ( N % mod ) * ( select % mod ) ) ; ways %= mod ; Console . Write ( ways ) ; } public static void Main ( String [ ] args ) { int N = 5 ; countWays ( N ) ; } }
Expected number of moves to reach the end of a board | Matrix Exponentiation | C # implementation of the approach ; Function to multiply two 7 * 7 matrix ; Function to perform matrix exponentiation ; 7 * 7 identity matrix ; Loop to find the power ; Function to return the required count ; Base cases ; Multiplier matrix ; Finding the required multiplier i . e mul ^ ( X - 6 ) ; Final answer ; Driver code
using System ; class GFG { static readonly int maxSize = 50 ; static double [ , ] matrix_product ( double [ , ] a , double [ , ] b ) { double [ , ] c = new double [ 7 , 7 ] ; for ( int i = 0 ; i < 7 ; i ++ ) for ( int j = 0 ; j < 7 ; j ++ ) for ( int k = 0 ; k < 7 ; k ++ ) c [ i , j ] += a [ i , k ] * b [ k , j ] ; return c ; } static double [ , ] mul_expo ( double [ , ] mul , int p ) { double [ , ] s = { { 1 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } } ; while ( p != 1 ) { if ( p % 2 == 1 ) s = matrix_product ( s , mul ) ; mul = matrix_product ( mul , mul ) ; p /= 2 ; } return matrix_product ( mul , s ) ; } static double expectedSteps ( int x ) { if ( x == 0 ) return 0 ; if ( x <= 6 ) return 6 ; double [ , ] mul = { { ( double ) 7 / 6 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { ( double ) - 1 / 6 , 0 , 0 , 0 , 0 , 0 , 0 } } ; mul = mul_expo ( mul , x - 6 ) ; return ( mul [ 0 , 0 ] + mul [ 1 , 0 ] + mul [ 2 , 0 ] + mul [ 3 , 0 ] + mul [ 4 , 0 ] + mul [ 5 , 0 ] ) * 6 ; } public static void Main ( String [ ] args ) { int n = 10 ; Console . Write ( " { 0 : f5 } " , expectedSteps ( n - 1 ) ) ; } }
Place the prisoners into cells to maximize the minimum difference between any two | C # implementation of the approach ; Function that returns true if the prisoners can be placed such that the minimum distance between any two prisoners is at least sep ; Considering the first prisoner is placed at 1 st cell ; If the first prisoner is placed at the first cell then the last_prisoner_placed will be the first prisoner placed and that will be in cell [ 0 ] ; Checking if the prisoner can be placed at ith cell or not ; If all the prisoners got placed then return true ; Function to return the maximized distance ; Sort the array so that binary search can be applied on it ; Minimum possible distance for the search space ; Maximum possible distance for the search space ; To store the result ; Binary search ; If the prisoners can be placed such that the minimum distance between any two prisoners is at least mid ; Update the answer ; Driver code
using System ; using System . Collections ; class GFG { static bool canPlace ( int [ ] a , int n , int p , int sep ) { int prisoners_placed = 1 ; int last_prisoner_placed = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int current_cell = a [ i ] ; if ( current_cell - last_prisoner_placed >= sep ) { prisoners_placed ++ ; last_prisoner_placed = current_cell ; if ( prisoners_placed == p ) { return true ; } } } return false ; } static int maxDistance ( int [ ] cell , int n , int p ) { Array . Sort ( cell ) ; int start = 0 ; int end = cell [ n - 1 ] - cell [ 0 ] ; int ans = 0 ; while ( start <= end ) { int mid = start + ( ( end - start ) / 2 ) ; if ( canPlace ( cell , n , p , mid ) ) { ans = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } return ans ; } public static void Main ( ) { int [ ] cell = { 1 , 2 , 8 , 4 , 9 } ; int n = cell . Length ; int p = 3 ; Console . WriteLine ( maxDistance ( cell , n , p ) ) ; } }
Find N in the given matrix that follows a pattern | C # implementation of the approach ; Function to return the row and the column of the given integer ; Binary search for the row number ; Condition to get the maximum x that satisfies the criteria ; Binary search for the column number ; Condition to get the maximum y that satisfies the criteria ; Get the row and the column number ; Return the pair ; Driver code
using System ; class GFG { static int [ ] solve ( int n ) { int low = 1 , high = ( int ) 1e4 , x = n , p = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( x - sum >= 1 ) { p = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } int start = 1 , end = ( int ) 1e4 , y = 1 , q = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( y + sum <= n ) { q = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } x = x - ( p * ( p + 1 ) ) / 2 ; y = y + ( q * ( q + 1 ) ) / 2 ; int r = x ; int c = q + 1 - n + y ; int [ ] ans = { r , c } ; return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] p = solve ( n ) ; Console . WriteLine ( p [ 0 ] + " ▁ " + p [ 1 ] ) ; } }
Find the count of distinct numbers in a range | C # Program to find the distinct elements in a range ; Function to perform queries in a range ; No overlap ; Totally Overlap ; Partial Overlap ; Finding the Answer for the left Child ; Finding the Answer for the right Child ; Combining the BitMasks ; Function to perform update operation in the Segment seg ; Forming the BitMask ; Updating the left Child ; Updating the right Child ; Updating the BitMask ; Building the Segment Tree ; Building the Initial BitMask ; Building the left seg tree ; Building the right seg tree ; Forming the BitMask ; Utility Function to answer the queries ; Counting the set bits which denote the distinct elements ; Updating the value ; Driver Code
using System ; class GFG { static long query ( int start , int end , int left , int right , int node , long [ ] seg ) { if ( end < left start > right ) { return 0 ; } else if ( start >= left && end <= right ) { return seg [ node ] ; } else { int mid = ( start + end ) / 2 ; long leftChild = query ( start , mid , left , right , 2 * node , seg ) ; long rightChild = query ( mid + 1 , end , left , right , 2 * node + 1 , seg ) ; return ( leftChild rightChild ) ; } } static void update ( int left , int right , int index , int Value , int node , int [ ] ar , long [ ] seg ) { if ( left == right ) { ar [ index ] = Value ; seg [ node ] = ( 1L << Value ) ; return ; } int mid = ( left + right ) / 2 ; if ( index > mid ) { update ( mid + 1 , right , index , Value , 2 * node + 1 , ar , seg ) ; } else { update ( left , mid , index , Value , 2 * node , ar , seg ) ; } seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } static void build ( int left , int right , int node , int [ ] ar , long [ ] seg ) { if ( left == right ) { seg [ node ] = ( 1L << ar [ left ] ) ; return ; } int mid = ( left + right ) / 2 ; build ( left , mid , 2 * node , ar , seg ) ; build ( mid + 1 , right , 2 * node + 1 , ar , seg ) ; seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } static void getDistinctCount ( int [ , ] queries , int [ ] ar , long [ ] seg , int n ) { for ( int i = 0 ; i < queries . GetLength ( 0 ) ; i ++ ) { int op = queries [ i , 0 ] ; if ( op == 2 ) { int l = queries [ i , 1 ] , r = queries [ i , 2 ] ; long tempMask = query ( 0 , n - 1 , l - 1 , r - 1 , 1 , seg ) ; int countOfBits = 0 ; for ( int s = 63 ; s >= 0 ; s -- ) { if ( ( tempMask & ( 1L << s ) ) > 0 ) { countOfBits ++ ; } } Console . WriteLine ( countOfBits ) ; } else { int index = queries [ i , 1 ] ; int val = queries [ i , 2 ] ; update ( 0 , n - 1 , index - 1 , val , 1 , ar , seg ) ; } } } public static void Main ( String [ ] args ) { int n = 7 ; int [ ] ar = { 1 , 2 , 1 , 3 , 1 , 2 , 1 } ; long [ ] seg = new long [ 4 * n ] ; build ( 0 , n - 1 , 1 , ar , seg ) ; int [ , ] queries = { { 2 , 1 , 4 } , { 1 , 4 , 2 } , { 1 , 5 , 2 } , { 2 , 4 , 6 } , { 2 , 1 , 7 } } ; getDistinctCount ( queries , ar , seg , n ) ; } }
Smallest subarray with GCD as 1 | Segment Tree | C # implementation of the approach ; Array to store segment - tree ; Function to build segment - tree to answer range GCD queries ; Base - case ; Mid element of the range ; Merging the result of left and right sub - tree ; Function to perform range GCD queries ; Base - cases ; Mid - element ; Calling left and right child ; Function to find the required length ; Building the segment tree ; Two pointer variables ; To store the final answer ; Looping ; Incrementing j till we don 't get  a gcd value of 1 ; Updating the final answer ; Incrementing i ; Updating j ; Returning the final answer ; Driver code
using System ; class GFG { static int maxLen = 30 ; static int [ ] seg = new int [ 3 * maxLen ] ; static int build ( int l , int r , int ind , int [ ] arr ) { if ( l == r ) return seg [ ind ] = arr [ l ] ; int mid = ( l + r ) / 2 ; return seg [ ind ] = __gcd ( build ( l , mid , 2 * ind + 1 , arr ) , build ( mid + 1 , r , 2 * ind + 2 , arr ) ) ; } static int query ( int l , int r , int l1 , int r1 , int ind ) { if ( l1 <= l && r <= r1 ) return seg [ ind ] ; if ( l > r1 r < l1 ) return 0 ; int mid = ( l + r ) / 2 ; return __gcd ( query ( l , mid , l1 , r1 , 2 * ind + 1 ) , query ( mid + 1 , r , l1 , r1 , 2 * ind + 2 ) ) ; } static int findLen ( int [ ] arr , int n ) { build ( 0 , n - 1 , 0 , arr ) ; int i = 0 , j = 0 ; int ans = int . MaxValue ; while ( i < n ) { while ( j < n && query ( 0 , n - 1 , i , j , 0 ) != 1 ) j ++ ; if ( j == n ) break ; ans = Math . Min ( ( j - i + 1 ) , ans ) ; i ++ ; j = Math . Max ( j , i ) ; } if ( ans == int . MaxValue ) return - 1 ; else return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 2 } ; int n = arr . Length ; Console . WriteLine ( findLen ( arr , n ) ) ; } }
Find an N x N grid whose xor of every row and column is equal | C # implementation of the approach ; Function to find the n x n matrix that satisfies the given condition ; Initialize x to 0 ; Divide the n x n matrix into n / 4 matrices for each of the n / 4 rows where each matrix is of size 4 x 4 ; Print the generated matrix ; Driver code
using System ; class GFG { static void findGrid ( int n ) { int [ , ] arr = new int [ n , n ] ; int x = 0 ; for ( int i = 0 ; i < n / 4 ; i ++ ) { for ( int j = 0 ; j < n / 4 ; j ++ ) { for ( int k = 0 ; k < 4 ; k ++ ) { for ( int l = 0 ; l < 4 ; l ++ ) { arr [ i * 4 + k , j * 4 + l ] = x ; x ++ ; } } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( arr [ i , j ] + " ▁ " ) ; } Console . WriteLine ( " ▁ " ) ; } } public static void Main ( String [ ] args ) { int n = 4 ; findGrid ( n ) ; } }
Cartesian tree from inorder traversal | Segment Tree | C # implementation of the approach ; Node of the BST ; Array to store segment tree ; Function to create segment - tree to answer range - max query ; Base case ; Maximum index in left range ; Maximum index in right range ; If value at l1 > r1 ; Else ; Returning the maximum in range ; Function to answer range max query ; Base cases ; Maximum in left range ; Maximum in right range ; l1 = - 1 means left range was out - side required range ; Returning the maximum among two ranges ; Function to print the inorder traversal of the binary tree ; Base case ; Traversing the left sub - tree ; Printing current node ; Traversing the right sub - tree ; Function to build cartesian tree ; Base case ; Maximum in the range ; Creating current node ; Creating left sub - tree ; Creating right sub - tree ; Returning current node ; Driver code ; In - order traversal of cartesian tree ; Size of the array ; Building the segment tree ; Building && printing cartesian tree
using System ; class GFG { static int maxLen = 30 ; public class node { public int data ; public node left ; public node right ; public node ( int data ) { left = null ; right = null ; this . data = data ; } } ; static int [ ] segtree = new int [ maxLen * 4 ] ; static int buildTree ( int l , int r , int i , int [ ] arr ) { if ( l == r ) { segtree [ i ] = l ; return l ; } int l1 = buildTree ( l , ( l + r ) / 2 , 2 * i + 1 , arr ) ; int r1 = buildTree ( ( l + r ) / 2 + 1 , r , 2 * i + 2 , arr ) ; if ( arr [ l1 ] > arr [ r1 ] ) segtree [ i ] = l1 ; else segtree [ i ] = r1 ; return segtree [ i ] ; } static int rangeMax ( int l , int r , int rl , int rr , int i , int [ ] arr ) { if ( r < rl l > rr ) return - 1 ; if ( l >= rl && r <= rr ) return segtree [ i ] ; int l1 = rangeMax ( l , ( l + r ) / 2 , rl , rr , 2 * i + 1 , arr ) ; int r1 = rangeMax ( ( l + r ) / 2 + 1 , r , rl , rr , 2 * i + 2 , arr ) ; if ( l1 == - 1 ) return r1 ; if ( r1 == - 1 ) return l1 ; if ( arr [ l1 ] > arr [ r1 ] ) return l1 ; else return r1 ; } static void inorder ( node curr ) { if ( curr == null ) return ; inorder ( curr . left ) ; Console . Write ( curr . data + " ▁ " ) ; inorder ( curr . right ) ; } static node createCartesianTree ( int l , int r , int [ ] arr , int n ) { if ( r < l ) return null ; int m = rangeMax ( 0 , n - 1 , l , r , 0 , arr ) ; node curr = new node ( arr [ m ] ) ; curr . left = createCartesianTree ( l , m - 1 , arr , n ) ; curr . right = createCartesianTree ( m + 1 , r , arr , n ) ; return curr ; } public static void Main ( ) { int [ ] arr = { 8 , 11 , 21 , 100 , 5 , 70 , 55 } ; int n = arr . Length ; buildTree ( 0 , n - 1 , 0 , arr ) ; inorder ( createCartesianTree ( 0 , n - 1 , arr , n ) ) ; } }
Kth smallest element in the array using constant space when array can 't be modified | C # implementation of the approach ; Function to return the kth smallest element from the array ; Minimum and maximum element from the array ; Modified binary search ; To store the count of elements from the array which are less than mid and the elements which are equal to mid ; If mid is the kth smallest ; If the required element is less than mid ; If the required element is greater than mid ; Driver code
using System ; using System . Linq ; class GFG { static int kthSmallest ( int [ ] arr , int k , int n ) { int low = arr . Min ( ) ; int high = arr . Max ( ) ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int countless = 0 , countequal = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( arr [ i ] < mid ) ++ countless ; else if ( arr [ i ] == mid ) ++ countequal ; } if ( countless < k && ( countless + countequal ) >= k ) { return mid ; } else if ( countless >= k ) { high = mid - 1 ; } else if ( countless < k && countless + countequal < k ) { low = mid + 1 ; } } return int . MinValue ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 10 , 4 , 3 , 20 , 15 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( kthSmallest ( arr , k , n ) ) ; } }
Interactive Problems in Competitive Programming |  ; Iterating from lower_bound to upper_bound ; Input the response from the judge
using System ; class GFG { public static void Main ( string [ ] args ) { int lower_bound = 2 ; int upper_bound = 10 ; for ( int i = lower_bound ; i <= upper_bound ; i ++ ) { Console . WriteLine ( i ) ; int response = int . Parse ( Console . ReadLine ( ) ) ; if ( response == 0 ) { Console . WriteLine ( " Number ▁ guessed ▁ is ▁ : " + i ) ; break ; } } } }
Interactive Problems in Competitive Programming |  ; Applying Binary Search interactively ; Print the guessed number ; Input the response from the judge
using System ; class GFG { static void Main ( ) { int lower_bound = 2 ; int upper_bound = 10 ; while ( lower_bound <= upper_bound ) { int mid = ( lower_bound + upper_bound ) / 2 ; Console . WriteLine ( mid ) ; int response = Convert . ToInt32 ( Console . ReadLine ( ) ) ; if ( response == - 1 ) { lower_bound = mid + 1 ; } else if ( response == 1 ) { upper_bound = mid - 1 ; } else if ( response == 0 ) { Console . WriteLine ( " Number ▁ guessed ▁ is ▁ : " + mid ) ; break ; } } } }
Find 2 ^ ( 2 ^ A ) % B | C # implementation of the approach ; Function to return 2 ^ ( 2 ^ A ) % B ; Base case , 2 ^ ( 2 ^ 1 ) % B = 4 % B ; Driver code ; Print 2 ^ ( 2 ^ A ) % B
class GFG { static long F ( long A , long B ) { if ( A == 1 ) return ( 4 % B ) ; else { long temp = F ( A - 1 , B ) ; return ( temp * temp ) % B ; } } static void Main ( ) { long A = 25 , B = 50 ; System . Console . WriteLine ( F ( A , B ) ) ; } }
Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | C # implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code
using System ; class GFG { const int MOD = 1000000007 ; static int rangeSum ( int l , int r ) { int a = 1 , b = 9 , res = 0 ; for ( int i = 1 ; i <= 10 ; i ++ ) { int L = Math . Max ( l , a ) ; int R = Math . Min ( r , b ) ; if ( L <= R ) { int sum = ( L + R ) * ( R - L + 1 ) / 2 ; res += ( i * i ) * ( sum % MOD ) ; res %= MOD ; } a = a * 10 ; b = b * 10 + 9 ; } return res ; } public static void Main ( ) { int l = 98 , r = 102 ; Console . WriteLine ( rangeSum ( l , r ) ) ; } }
Generate a random permutation of elements from range [ L , R ] ( Divide and Conquer ) | C # implementation of the approach ; To store the random permutation ; Utility function to print the generated permutation ; Function to return a random number between x and y ; Recursive function to generate the random permutation ; Base condition ; Random number returned from the function ; Inserting random number in vector ; Recursion call for [ l , n - 1 ] ; Recursion call for [ n + 1 , r ] ; Driver code ; Generate permutation ; Print the generated permutation
using System ; using System . Collections . Generic ; class GFG { static List < int > permutation = new List < int > ( ) ; static void printPermutation ( ) { foreach ( int i in permutation ) Console . Write ( i + " ▁ " ) ; } static int give_random_number ( int l , int r ) { Random rnd = new Random ( ) ; int num = rnd . Next ( l , r ) ; int x = ( int ) ( num % ( r - l + 1 ) + l ) ; return x ; } static void generate_random_permutation ( int l , int r ) { if ( l > r ) return ; int n = give_random_number ( l , r ) ; permutation . Add ( n ) ; generate_random_permutation ( l , n - 1 ) ; generate_random_permutation ( n + 1 , r ) ; } public static void Main ( String [ ] args ) { int l = 5 ; int r = 15 ; generate_random_permutation ( l , r ) ; printPermutation ( ) ; } }
Minimum number N such that total set bits of all numbers from 1 to N is at | C # implementation of the above approach ; Function to count sum of set bits of all numbers till N ; Function to find the minimum number ; Binary search for the lowest number ; Find mid number ; Check if it is atleast x ; Driver Code
using System ; class solution { static int INF = 99999 ; static int size = 10 ; static int getSetBitsFromOneToN ( int N ) { int two = 2 , ans = 0 ; int n = N ; while ( n != 0 ) { ans += ( N / two ) * ( two >> 1 ) ; if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 ; two <<= 1 ; n >>= 1 ; } return ans ; } static int findMinimum ( int x ) { int low = 0 , high = 100000 ; int ans = high ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( getSetBitsFromOneToN ( mid ) >= x ) { ans = Math . Min ( ans , mid ) ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void Main ( ) { int x = 20 ; Console . WriteLine ( findMinimum ( x ) ) ; } }