codeparrot/xlcost-text-to-code huggingface dataset:
How to efficiently sort a big list dates in 20 's | C # program to sort an array of dates using Radix Sort ; Utilitiy function to obtain maximum date or month or year ; A function to do counting sort of [ ] arr according to given q i . e . ( 0 for day , 1 for month , 2 for year ) ; to extract last digit divide by p then % 10 Store count of occurrences in cnt [ ] ; Build the output array ; Copy the output array to [ ] arr , so that [ ] arr now contains sorted numbers according to current digit ; update p to get the next digit ; The main function that sorts array of dates using Radix Sort ; First sort by day ; Then by month ; Finally by year ; A utility function to print an array ; Driver Code ; Function Call
using System ; public class GFG { static int getMax ( int [ , ] arr , int n , int q ) { int maxi = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { maxi = Math . Max ( maxi , arr [ i , q ] ) ; } return maxi ; } static void sortDatesUtil ( int [ , ] arr , int n , int q ) { int maxi = getMax ( arr , n , q ) ; int p = 1 ; while ( maxi > 0 ) { int [ ] cnt = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { cnt [ ( arr [ i , q ] / p ) % 10 ] ++ ; } for ( int i = 1 ; i < 10 ; i ++ ) { cnt [ i ] += cnt [ i - 1 ] ; } int [ , ] ans = new int [ n , 3 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int lastDigit = ( arr [ i , q ] / p ) % 10 ; for ( int j = 0 ; j < 3 ; j ++ ) { ans [ cnt [ lastDigit ] - 1 , j ] = arr [ i , j ] ; } cnt [ lastDigit ] -- ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { arr [ i , j ] = ans [ i , j ] ; } } p *= 10 ; maxi /= 10 ; } } static void sortDates ( int [ , ] dates , int n ) { sortDatesUtil ( dates , n , 0 ) ; sortDatesUtil ( dates , n , 1 ) ; sortDatesUtil ( dates , n , 2 ) ; } static void printArr ( int [ , ] arr , int n ) { for ( int i = 0 ; i < 6 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { Console . Write ( arr [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] dates = { { 20 , 1 , 2014 } , { 25 , 3 , 2010 } , { 3 , 12 , 2000 } , { 18 , 11 , 2000 } , { 19 , 4 , 2015 } , { 9 , 7 , 2005 } } ; int n = dates . GetLength ( 0 ) ; sortDates ( dates , n ) ; Console . Write ( " STRNEWLINE Sorted ▁ Dates STRNEWLINE " ) ; printArr ( dates , n ) ; } }
Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K | C # program for the above approach ; Function to count the operations required to convert matrix A to B ; Store the sizes of matrix ; Stores count of flips required ; Traverse the iven matrix ; Check if the matrix values are not equal ; Increment the count of moves ; Check if the current square sized exists or not ; Flip all the bits in this square sized submatrix ; Count of operations required ; Driver Code
using System ; class GFG { static int minMoves ( char [ , ] a , char [ , ] b , int K ) { int n = a . Length ; int m = a . GetLength ( 0 ) ; int cntOperations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i , j ] != b [ i , j ] ) { cntOperations ++ ; if ( i + K - 1 >= n j + K - 1 >= m ) { return - 1 ; } for ( int p = 0 ; p <= K - 1 ; p ++ ) { for ( int q = 0 ; q <= K - 1 ; q ++ ) { if ( a [ i + p , j + q ] == '0' ) { a [ i + p , j + q ] = '1' ; } else { a [ i + p , j + q ] = '0' ; } } } } } } return cntOperations ; } public static void Main ( ) { char [ , ] A = new char [ , ] { { '1' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; char [ , ] B = new char [ , ] { { '0' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; int K = 3 ; Console . WriteLine ( minMoves ( A , B , K ) ) ; } }
Largest value of K that a set of all possible subset | C # program for the above approach ; Function to find maximum count of consecutive integers from 0 in set S of all possible subset - sum ; Stores the maximum possible integer ; Sort the given array in non - decrasing order ; Iterate the given array ; If arr [ i ] <= X + 1 , then update X otherwise break the loop ; Return Answer ; Driver Code
using System ; class GFG { public static int maxConsecutiveCnt ( int [ ] arr ) { int X = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] <= ( X + 1 ) ) { X = X + arr [ i ] ; } else { break ; } } return X + 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 4 } ; Console . Write ( maxConsecutiveCnt ( arr ) ) ; } }
Largest square sub | C # program for the above approach ; Define the prefix sum arrays globally ; Diagonal sum ; Check each row ; Check each column ; Check anti - diagonal ; Store the size of the given grid ; Compute the prefix sum for the rows ; Compute the prefix sum for the columns ; Check for all possible square submatrix ; Driver Code
using System ; class GFG { public static int [ , ] prefix_sum_row = new int [ 50 , 51 ] ; public static int [ , ] prefix_sum_col = new int [ 51 , 50 ] ; public static bool is_valid ( int r , int c , int size , int [ , ] grid ) { int r_end = r + size , c_end = c + size ; int sum = 0 ; for ( int i = r , j = c ; i < r_end ; i ++ , j ++ ) { sum += grid [ i , j ] ; } for ( int i = r ; i < r_end ; i ++ ) { if ( prefix_sum_row [ i , c_end ] - prefix_sum_row [ i , c ] != sum ) { return false ; } } for ( int i = c ; i < c_end ; i ++ ) { if ( prefix_sum_col [ r_end , i ] - prefix_sum_col [ r , i ] != sum ) { return false ; } } int ad_sum = 0 ; for ( int i = r , j = c_end - 1 ; i < r_end ; i ++ , j -- ) { ad_sum += grid [ i , j ] ; } return ad_sum == sum ; } public static int largestSquareValidMatrix ( int [ , ] grid ) { int m = grid . GetLength ( 0 ) , n = grid . GetLength ( 1 ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { prefix_sum_row [ i , j ] = prefix_sum_row [ i , j - 1 ] + grid [ i , j - 1 ] ; } } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { prefix_sum_col [ i , j ] = prefix_sum_col [ i - 1 , j ] + grid [ i - 1 , j ] ; } } for ( int size = Math . Min ( m , n ) ; size > 1 ; size -- ) { for ( int i = 0 ; i <= m - size ; i ++ ) { for ( int j = 0 ; j <= n - size ; j ++ ) { if ( is_valid ( i , j , size , grid ) ) { return size ; } } } } return 1 ; } public static void Main ( ) { int [ , ] grid = { { 7 , 1 , 4 , 5 , 6 } , { 2 , 5 , 1 , 6 , 4 } , { 1 , 5 , 4 , 3 , 2 } , { 1 , 2 , 7 , 3 , 4 } } ; Console . WriteLine ( largestSquareValidMatrix ( grid ) ) ; } }
Minimize product of first 2 ^ Kâ €“ 1 Natural Numbers by swapping bits for any pair any number of times | C # program for the above approach ; Function to find the minimum positive product after swapping bits at the similar position for any two numbers ; Stores the resultant number ; range / 2 numbers will be 1 and range / 2 numbers will be range - 1 ; Multiply with the final number ; Return the answer ; Driver Code
using System ; class GFG { static int minimumPossibleProduct ( int K ) { int res = 1 ; int range = ( 1 << K ) - 1 ; for ( int i = 0 ; i < K ; i ++ ) { res *= ( range - 1 ) ; } res *= range ; return res ; } public static void Main ( string [ ] args ) { int K = 3 ; Console . WriteLine ( minimumPossibleProduct ( K ) ) ; } }
Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle | C # program for the above approach ; Function to find the maximum size of the subset of the given array such that a triangle can be formed from any three integers of the subset as sides ; Sort arr [ ] in increasing order ; Stores the maximum size of a valid subset of the given array ; Stores the starting index of the current window ; Stores the last index of the current window ; Iterate over the array arr [ ] ; Increment j till the value of arr [ i ] + arr [ i + 1 ] > arr [ j ] holds true ; Update the value of maxSize ; Return Answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int maximizeSubset ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int maxSize = 0 ; int i = 0 ; int j = i + 2 ; while ( i < N - 2 ) { if ( j >= N i + 1 >= N ) break ; while ( j < N && arr [ i ] + arr [ i + 1 ] > arr [ j ] ) { j ++ ; } maxSize = Math . Max ( maxSize , j - i ) ; i ++ ; j = Math . Max ( j , i + 2 ) ; } return maxSize ; } public static void Main ( ) { int [ ] arr = { 2 , 7 , 4 , 1 , 6 , 9 , 5 , 3 } ; int N = arr . Length ; Console . Write ( maximizeSubset ( arr , N ) ) ; } }
Maximum possible value of array elements that can be made based on given capacity conditions | C # program for the above approach ; Function to find the maximum element after shifting operations in arr [ ] ; Stores the sum of array element ; Stores the maximum element in cap [ ] ; Iterate to find maximum element ; Return the resultant maximum value ; Driver Code
using System ; class GFG { public static int maxShiftArrayValue ( int [ ] arr , int [ ] cap , int N ) { int sumVals = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sumVals += arr [ i ] ; } int maxCapacity = 0 ; for ( int i = 0 ; i < N ; i ++ ) { maxCapacity = Math . Max ( cap [ i ] , maxCapacity ) ; } return Math . Min ( maxCapacity , sumVals ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 } ; int [ ] cap = { 5 , 6 } ; int N = arr . Length ; Console . WriteLine ( maxShiftArrayValue ( arr , cap , N ) ) ; } }
Minimum size of Array possible with given sum and product values | C # program for the above approach ; Function to find the minimum size of array with sum S and product P ; Base Case ; Iterate through all values of S and check the mentioned condition ; Otherwise , print " - 1" ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int minimumSizeArray ( int S , int P ) { if ( S == P ) { return 1 ; } for ( int i = 2 ; i <= S ; i ++ ) { double d = i ; if ( ( S / d ) >= Math . Pow ( P , 1.0 / d ) ) { return i ; } } return - 1 ; } public static void Main ( ) { int S = 5 , P = 6 ; Console . Write ( minimumSizeArray ( S , P ) ) ; } }
Count of integral points that lie at a distance D from origin | C # program for the above approach ; Function to find the total valid integer coordinates at a distance D from origin ; Stores the count of valid points ; Iterate over possibel x coordinates ; Find the respective y coordinate with the pythagoras theorem ; Adding 4 to compensate the coordinates present on x and y axes . ; Return the answer ; Driver Code
using System ; public class GFG { static int countPoints ( int D ) { int count = 0 ; for ( int x = 1 ; x * x < D * D ; x ++ ) { int y = ( int ) Math . Sqrt ( ( D * D - x * x ) ) ; if ( x * x + y * y == D * D ) { count += 4 ; } } count += 4 ; return count ; } public static void Main ( ) { int D = 5 ; Console . Write ( countPoints ( D ) ) ; } }
Sum of the shortest distance between all 0 s to 1 in given binary string | C # program for the above approach ; Function to find the total sum of the shortest distance between every 0 to 1 in a given binary string ; Stores the prefix distance and suffix distance from 0 to 1 ; Stores the current distance from 1 to 0 ; Marks the 1 ; If current character is 1 ; Mark haveOne to true ; Assign the cnt to 0 ; Assign prefixDistance [ i ] as 0 ; If haveOne is true ; Update the cnt ; Update prefixDistance [ i ] ; Assign prefixDistance [ i ] as INT_MAX ; Assign haveOne as false ; If current character is 1 ; Mark haveOne to true ; Assign the cnt to 0 ; Assign the suffixDistance [ i ] as 0 ; If haveOne is true ; Update the cnt ; Update suffixDistance [ i ] as cnt ; Assign suffixDistance [ i ] as INT_MAX ; Stores the total sum of distances between 0 to nearest 1 ; If current character is 0 ; Update the value of sum ; Print the value of the sum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findTotalDistance ( string S , int N ) { int [ ] prefixDistance = new int [ N ] ; int [ ] suffixDistance = new int [ N ] ; int cnt = 0 ; bool haveOne = false ; for ( int i = 0 ; i < N ; ++ i ) { if ( S [ i ] == '1' ) { haveOne = true ; cnt = 0 ; prefixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; prefixDistance [ i ] = cnt ; } else prefixDistance [ i ] = Int32 . MaxValue ; } haveOne = false ; for ( int i = N - 1 ; i >= 0 ; -- i ) { if ( S [ i ] == '1' ) { haveOne = true ; cnt = 0 ; suffixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; suffixDistance [ i ] = cnt ; } else suffixDistance [ i ] = Int32 . MaxValue ; } int sum = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( S [ i ] == '0' ) { sum += Math . Min ( prefixDistance [ i ] , suffixDistance [ i ] ) ; } } Console . Write ( sum ) ; } public static void Main ( ) { string S = "100100" ; int N = S . Length ; findTotalDistance ( S , N ) ; } }
Minimize value of | A | C # program for the above approach ; Function to find the minimum possible value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z for given A , B and C ; Stores the minimum value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z ; Iterate over all values of i in the range [ 1 , 2 * C ] ; Iterate over all values of j such that i * j <= 2 * c ; Update the value of ans ; Return answer ; Driver Code
using System ; class GFG { public static int minimizeCost ( int A , int B , int C ) { int ans = A + B + C ; for ( int i = 1 ; i <= 2 * C ; i ++ ) { int j = 0 ; while ( i * j <= 2 * C ) { ans = Math . Min ( ans , Math . Abs ( A - i ) + Math . Abs ( B - j ) + Math . Abs ( i * j - C ) ) ; j ++ ; } } return ans ; } public static void Main ( String [ ] args ) { int A = 19 , B = 28 , C = 522 ; Console . Write ( minimizeCost ( A , B , C ) ) ; } }
Average value of set bit count in given Binary string after performing all possible choices of K operations | C # program for the above approach ; Function to calculate the average number of Set bits after after given operations ; Stores the average number of set bits after current operation ; Stores the average number of set bits after current operation ; Iterate through the array [ ] arr and update the values of p and q ; Store the value of p and q of the previous state before their updation ; Update average number of set bits after performing the ith operation ; Update average number of off bits after performing the ith operation ; Return Answer ; Driver Code
using System ; public class GFG { static double averageSetBits ( int N , int K , int [ ] arr ) { double p = N ; double q = 0 ; for ( int i = 0 ; i < K ; i ++ ) { double _p = p , _q = q ; p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N ; q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N ; } return p ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] arr = { 1 , 2 , 3 } ; int K = arr . Length ; Console . WriteLine ( String . Format ( " { 0 : F10 } " , averageSetBits ( N , K , arr ) ) ) ; } }
Find maximum sum of subsequence after flipping signs of at most K elements in given Array | C ++ implementation for the above approach ; Function to calculate the max sum of subsequence ; Variable to store the max sum ; Sort the array ; Iterate over the array ; Flip sign ; Decrement k ; Traverse over the array ; Add only positive elements ; Return the max sum ; Driver Code ; Given array ; Variable to store number of flips are allowed ; Function call to find the maximum sum of subsequence
using System ; class GFG { static int maxSubseq ( int [ ] arr , int N , int K ) { int sum = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( K == 0 ) break ; if ( arr [ i ] < 0 ) { arr [ i ] = - arr [ i ] ; K -- ; } } for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] > 0 ) sum += arr [ i ] ; return sum ; } public static void Main ( string [ ] args ) { int [ ] arr = { 6 , - 10 , - 1 , 0 , - 4 , 2 } ; int K = 2 ; int N = arr . Length ; Console . Write ( maxSubseq ( arr , N , K ) ) ; } }
K | C # implementation for the above approach ; Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code
using System ; class GFG { static long KthSolution ( long X , long K ) { long ans = 0 ; for ( int i = 0 ; i < 64 ; i ++ ) { if ( ( X & ( 1L L << i ) ) == 0 ) { if ( ( K & 1 ) > 0 ) { ans |= ( 1L L << i ) ; } K >>= 1 ; if ( K == 0 ) { break ; } } } return ans ; } public static void Main ( ) { long X = 5 , K = 5 ; Console . Write ( KthSolution ( X , K ) ) ; } }
Rearrange sorted array such that all odd indices elements comes before all even indices element | C # program for the above approach ; Function to print the array ; Function to rearrange the array such that odd indexed elements come before even indexed elements ; Reduces the size of array by one because last element does not need to be changed in case N = odd ; Initialize the variables ; Iterate over the range ; Add the modified element at the odd position ; Iterate over the range ; Add the modified element at the even position ; Iterate over the range ; Divide by thr maximum element ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void printTheArray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void rearrange ( int [ ] arr , int N ) { if ( ( N & 1 ) != 0 ) N -- ; int odd_idx = 1 , even_idx = 0 ; int i , max_elem = arr [ N - 1 ] + 1 ; for ( i = 0 ; i < N / 2 ; i ++ ) { arr [ i ] += ( arr [ odd_idx ] % max_elem ) * max_elem ; odd_idx += 2 ; } for ( ; i < N ; i ++ ) { arr [ i ] += ( arr [ even_idx ] % max_elem ) * max_elem ; even_idx += 2 ; } for ( i = 0 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] / max_elem ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 16 , 18 , 19 } ; int N = arr . Length ; rearrange ( arr , N ) ; printTheArray ( arr , N ) ; } }
Find range of values for S in given Array with values satisfying [ arr [ i ] = floor ( ( i * S ) / K ) ] | C # program for the above approach ; Function to find the range of values for S in a given array that satisfies the given condition ; Stores the left range value ; Stores the right range value ; Find the current left range value for S ; Find the current right range value for S ; Updating L value ; Updating R value ; Driver Code
using System ; class GFG { static void findRange ( int [ ] arr , int N , int K ) { int L = Int32 . MinValue ; int R = Int32 . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { int l = ( int ) Math . Ceiling ( 1.0 * arr [ i ] * K / ( i + 1 ) ) ; int r = ( int ) Math . Ceiling ( ( 1.0 + arr [ i ] ) * K / ( i + 1 ) ) - 1 ; L = Math . Max ( L , l ) ; R = Math . Min ( R , r ) ; } Console . WriteLine ( L + " ▁ " + R ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 , 9 , 11 } ; int K = 10 ; int N = arr . Length ; findRange ( arr , N , K ) ; } }
Find an anagram of given String having different characters at corresponding indices | C # program for the above approach ; Function to find anagram of string such that characters at the same indices are different ; Copying our original string for comparison ; Declaring the two pointers ; Checking the given condition ; When string length is odd ; The mid element ; If the characters are the same , then perform the swap operation as illustrated ; Check if the corresponding indices has the same character or not ; If string follows required condition ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findAnagram ( string s ) { string check = s ; int i = 0 , j = s . Length - 1 ; while ( i < s . Length && j >= 0 ) { if ( s [ i ] != s [ j ] && check [ i ] != s [ j ] && check [ j ] != s [ i ] ) { char temp = s [ i ] ; s = s . Substring ( 0 , i ) + s [ j ] + s . Substring ( i + 1 ) ; s = s . Substring ( 0 , j ) + temp + s . Substring ( j + 1 ) ; i ++ ; j = s . Length - 1 ; } else { j -- ; } } if ( s . Length % 2 != 0 ) { int mid = s . Length / 2 ; if ( check [ mid ] == s [ mid ] ) { for ( i = 0 ; i < s . Length ; i ++ ) { if ( check [ i ] != s [ mid ] && s [ i ] != s [ mid ] ) { char temp = s [ i ] ; s = s . Substring ( 0 , i ) + s [ mid ] + s . Substring ( i + 1 ) ; s = s . Substring ( 0 , mid ) + temp + s . Substring ( mid + 1 ) ; break ; } } } } bool ok = true ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( check [ i ] == s [ i ] ) { ok = false ; break ; } } if ( ok ) Console . Write ( s ) ; else Console . Write ( - 1 ) ; } public static void Main ( ) { string S = " geek " ; findAnagram ( S ) ; } }
Lexicographically smallest permutation of [ 1 , N ] based on given Binary string | C # program for the above approach ; Function to generate the lexicographically smallest permutation according to the given criteria ; Stores the resultant permutation ; Initialize the first elements to 1 ; Traverse the given string S ; Number greater than last number ; Number equal to the last number ; Correct all numbers to the left of the current index ; Printing the permutation ; Driver Code
using System ; class GFG { static void constructPermutation ( string S , int N ) { int [ ] ans = new int [ N ] ; ans [ 0 ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) { if ( S [ i - 1 ] == '0' ) { ans [ i ] = i + 1 ; } else { ans [ i ] = ans [ i - 1 ] ; } for ( int j = 0 ; j < i ; ++ j ) { if ( ans [ j ] >= ans [ i ] ) { ans [ j ] ++ ; } } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( ans [ i ] ) ; if ( i != N - 1 ) { Console . Write ( " ▁ " ) ; } } } public static void Main ( ) { string S = "100101" ; constructPermutation ( S , S . Length + 1 ) ; } }
Kth smallest positive integer Y such that its sum with X is same as its bitwise OR with X | package whatever do not write package name here ; Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code
using System ; public class GFG { static long KthSolution ( long X , long K ) { int ans = 0 ; for ( int i = 0 ; i < 64 ; i ++ ) { if ( ( X & ( 1 << i ) ) == 0 ) { if ( ( K & 1 ) > 0 ) { ans |= ( 1 << i ) ; } K >>= 1 ; if ( K == 0 ) { break ; } } } return ans ; } public static void Main ( String [ ] args ) { long X = 10 ; long K = 5 ; Console . WriteLine ( KthSolution ( X , K ) ) ; } }
Count of distinct N | C # program for the approach ; Function to find the count of distinct arrays of size n having elements in range [ 1 , k ] and all adjacent elements ( P , Q ) follows ( P <= Q ) or ( P % Q > 0 ) ; Stores the divisors of all integers in the range [ 1 , k ] ; Calculate the divisors of all integers using the Sieve ; Stores the dp states such that dp [ i ] [ j ] with i elements having j as the last element of array ; Initialize the dp array ; Calculate the dp states using the derived relation ; Calculate the sum for len - 1 ; Subtract dp [ len - 1 ] [ j ] for each factor of j from [ 1 , K ] ; Calculate the final result ; Return the resultant sum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countArrays ( int n , int k ) { List < int > [ ] divisors = new List < int > [ k + 1 ] ; for ( int i = 0 ; i < divisors . Length ; i ++ ) divisors [ i ] = new List < int > ( ) ; for ( int i = 1 ; i <= k ; i ++ ) { for ( int j = 2 * i ; j <= k ; j += i ) { divisors [ j ] . Add ( i ) ; } } int [ , ] dp = new int [ n + 1 , k + 1 ] ; for ( int j = 1 ; j <= k ; j ++ ) { dp [ 1 , j ] = 1 ; } int sum ; for ( int x = 2 ; x <= n ; x ++ ) { sum = 0 ; for ( int j = 1 ; j <= k ; j ++ ) { sum += dp [ x - 1 , j ] ; } for ( int y = 1 ; y <= k ; y ++ ) { dp [ x , y ] = sum ; foreach ( int d in divisors [ y ] ) { dp [ x , y ] = ( dp [ x , y ] - dp [ x - 1 , d ] ) ; } } } sum = 0 ; for ( int j = 1 ; j <= k ; j ++ ) { sum += dp [ n , j ] ; } return sum ; } public static void Main ( ) { int N = 2 , K = 3 ; Console . Write ( countArrays ( N , K ) ) ; } }