codeparrot/xlcost-text-to-code huggingface dataset:
Minimum time required to reach a given score | C # program for the above approach ; Function to calculate minimum time required to achieve given score target ; Store the frequency of elements ; Traverse the array p [ ] ; Update the frequency ; Stores the minimim time required ; Store the current score at any time instant t ; Iterate until sum is at least equal to target ; Increment time with every iteration ; Traverse the map ; Increment the points ; Print the time required ; Driver Code ; Function Call
using System . Collections . Generic ; using System ; using System . Linq ; class GFG { static void findMinimumTime ( int [ ] p , int n , int target ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( um . ContainsKey ( p [ i ] ) == true ) um [ p [ i ] ] += 1 ; else um [ p [ i ] ] = 1 ; } int time = 0 ; int sum = 0 ; var val = um . Keys . ToList ( ) ; while ( sum < target ) { sum = 0 ; time ++ ; foreach ( var key in val ) { sum += um [ key ] * ( time / key ) ; } } Console . WriteLine ( time ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 3 , 4 } ; int N = arr . Length ; int target = 10 ; findMinimumTime ( arr , N , target ) ; } }
Construct MEX array from the given array | C # program for the above approach ; Function to construct array B [ ] that stores MEX of array A [ ] excluding A [ i ] ; Stores elements present in arr [ ] ; Mark all values 1 , if present ; Initialize variable to store MEX ; Find MEX of arr [ ] ; Stores MEX for all indices ; Traverse the given array ; Update MEX ; MEX default ; Print the array B ; Driver Code ; Given array arr [ ] ; Size of array ; Function call
using System ; class GFG { static int MAXN = 100001 ; static void constructMEX ( int [ ] arr , int N ) { int [ ] hash = new int [ MAXN ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ i ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] = 1 ; } int MexOfArr = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { if ( hash [ i ] == 0 ) { MexOfArr = i ; break ; } } int [ ] B = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < MexOfArr ) B [ i ] = arr [ i ] ; else B [ i ] = MexOfArr ; } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( B [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 5 , 3 } ; int N = arr . Length ; constructMEX ( arr , N ) ; } }
Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum | C # program to implement the above approach ; Utility pair ; Function to print the smallest element left in the array and the pairs by given operation ; Stores array elements and return the minimum element of [ ] arr in O ( 1 ) ; Stores all the pairs that can be selected by the given operations ; Traverse the array [ ] arr ; Traverse pq while count of elements left in pq greater than 1 ; Stores top element of pq ; Pop top element of pq ; Stores top element of pq ; Pop top element of pq ; Insert ( X + Y + 1 ) / 2 in pq ; Insert the pair ( X , Y ) in pairsArr [ ] ; Print the element left in pq by performing the given operations ; Stores count of elements in pairsArr [ ] ; Print all the pairs that can be selected in given operations ; If i is the first index of pairsArr [ ] ; Print current pairs of pairsArr [ ] ; If i is not the last index of pairsArr [ ] ; If i is the last index of pairsArr [ ] ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void smallestNumberLeftInPQ ( int [ ] arr , int N ) { List < int > pq = new List < int > ( ) ; List < pair > pairsArr = new List < pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . Add ( arr [ i ] ) ; } pq . Sort ( ) ; pq . Reverse ( ) ; while ( pq . Count > 1 ) { int X = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; int Y = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; pq . Add ( ( X + Y + 1 ) / 2 ) ; pq . Sort ( ) ; pq . Reverse ( ) ; pairsArr . Add ( new pair ( X , Y ) ) ; } Console . Write ( " { " + pq [ 0 ] + " } , ▁ " ) ; int sz = pairsArr . Count ; for ( int i = 0 ; i < sz ; i ++ ) { if ( i == 0 ) { Console . Write ( " { ▁ " ) ; } Console . Write ( " ( " + pairsArr [ i ] . first + " , ▁ " + pairsArr [ i ] . second + " ) " ) ; if ( i != sz - 1 ) { Console . Write ( " , ▁ " ) ; } if ( i == sz - 1 ) { Console . Write ( " ▁ } " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 } ; int N = arr . Length ; smallestNumberLeftInPQ ( arr , N ) ; } }
Minimum removals required such that sum of remaining array modulo M is X | C # program for the above approach ; Function to find the minimum elements having sum x ; Initialize dp table ; Pre - compute subproblems ; If mod is smaller than element ; Minimum elements with sum j upto index i ; Return answer ; Function to find minimum number of removals to make sum % M in remaining array is equal to X ; Sum of all elements ; Sum to be removed ; Print answer ; Driver Code ; Given array ; Given size ; Given mod and x ; Function call
using System ; class GFG { static int findSum ( int [ ] S , int n , int x ) { int [ , ] table = new int [ n + 1 , x + 1 ] ; for ( int i = 1 ; i <= x ; i ++ ) { table [ 0 , i ] = int . MaxValue - 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= x ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i , j ] = table [ i - 1 , j ] ; } else { table [ i , j ] = Math . Min ( table [ i - 1 , j ] , table [ i , j - S [ i - 1 ] ] + 1 ) ; } } } return ( table [ n , x ] > n ) ? - 1 : table [ n , x ] ; } static void minRemovals ( int [ ] arr , int n , int m , int x ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int requied_Sum = 0 ; if ( sum % m < x ) requied_Sum = m + sum % m - x ; else requied_Sum = sum % m - x ; Console . Write ( findSum ( arr , n , requied_Sum ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 2 } ; int n = arr . Length ; int m = 4 , x = 2 ; minRemovals ( arr , n , m , x % m ) ; } }
Count minimum character replacements required such that given string satisfies the given conditions | C # program for the above approach ; Function that finds the minimum count of steps required to make the string special ; Stores the frequency of the left & right half of string ; Find frequency of left half ; Find frequency of left half ; Make all characters equal to character c ; Case 1 : For s [ i ] < s [ j ] ; Subtract all the characters on left side that are <= d ; Adding all characters on the right side that same as d ; Find minimum value of count ; Similarly for Case 2 : s [ i ] > s [ j ] ; Return the minimum changes ; Driver Code ; Given string S ; Function Call
using System ; class GFG { public static int minChange ( String s , int n ) { int [ ] L = new int [ 26 ] ; int [ ] R = new int [ 26 ] ; for ( int i = 0 ; i < n / 2 ; i ++ ) { char ch = s [ i ] ; L [ ch - ' a ' ] ++ ; } for ( int i = n / 2 ; i < n ; i ++ ) { char ch = s [ i ] ; R [ ch - ' a ' ] ++ ; } int count = n ; for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) { count = Math . Min ( count , n - L [ ch - ' a ' ] - R [ ch - ' a ' ] ) ; } int change = n / 2 ; for ( int d = 0 ; d + 1 < 26 ; d ++ ) { change -= L [ d ] ; change += R [ d ] ; count = Math . Min ( count , change ) ; } change = n / 2 ; for ( int d = 0 ; d + 1 < 26 ; d ++ ) { change -= R [ d ] ; change += L [ d ] ; count = Math . Min ( change , count ) ; } return count ; } public static void Main ( String [ ] args ) { String S = " aababc " ; int N = S . Length ; Console . WriteLine ( minChange ( S , N ) ) ; } }
Rearrange string to obtain Longest Palindromic Substring | C # program to implement the above approach ; Function to rearrange the string to get the longest palindromic substring ; Stores the length of str ; Store the count of occurrence of each character ; Traverse the string , str ; Count occurrence of each character ; Store the left half of the longest palindromic substring ; Store the right half of the longest palindromic substring ; Traverse the array , hash [ ] ; Append half of the characters to res1 ; Append half of the characters to res2 ; reverse string res2 to make res1 + res2 palindrome ; Store the remaining characters ; Check If any odd character appended to the middle of the resultant string or not ; Append all the character which occurs odd number of times ; If count of occurrence of characters is odd ; Driver code
using System ; using System . Text ; class GFG { static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } static String longestPalinSub ( String str ) { int N = str . Length ; int [ ] hash = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ str [ i ] ] ++ ; } StringBuilder res1 = new StringBuilder ( ) ; StringBuilder res2 = new StringBuilder ( ) ; for ( int i = 0 ; i < 256 ; i ++ ) { for ( int j = 0 ; j < hash [ i ] / 2 ; j ++ ) { res1 . Append ( ( char ) i ) ; } for ( int j = ( hash [ i ] + 1 ) / 2 ; j < hash [ i ] ; j ++ ) { res2 . Append ( ( char ) i ) ; } } String tmp = reverse ( res2 . ToString ( ) ) ; StringBuilder res3 = new StringBuilder ( ) ; bool f = false ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( hash [ i ] % 2 == 1 ) { if ( ! f ) { res1 . Append ( ( char ) i ) ; f = true ; } else { res3 . Append ( ( char ) i ) ; } } } return ( res1 . ToString ( ) + tmp . ToString ( ) + res3 . ToString ( ) ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; Console . WriteLine ( longestPalinSub ( str ) ) ; } }
Maximize subsequences having array elements not exceeding length of the subsequence | C # program for the above approach ; Function to calculate the number of subsequences that can be formed ; Stores the number of subsequences ; Iterate over the map ; Count the number of subsequences that can be formed from x . first ; Number of occurrences of x . first which are left ; Return the number of subsequences ; Function to create the maximum count of subsequences that can be formed ; Stores the frequency of [ ] arr ; Update the frequency ; Print the number of subsequences ; Driver code ; Given array [ ] arr ; Function call
using System ; using System . Collections . Generic ; class GFG { public static int No_Of_subsequences ( Dictionary < int , int > mp ) { int count = 0 ; int left = 0 ; foreach ( KeyValuePair < int , int > x in mp ) { if ( ! mp . ContainsKey ( x . Key ) ) mp . Add ( x . Key , x . Value + left ) ; count += ( x . Value / x . Key ) ; left = x . Value % x . Key ; } return count ; } public static void maximumsubsequences ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } Console . WriteLine ( No_Of_subsequences ( mp ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 } ; int N = arr . Length ; maximumsubsequences ( arr , N ) ; } }
Check if concatenation of any permutation of given list of arrays generates the given array | C # program for the above approach ; Function to check if it is possible to obtain array by concatenating the arrays in list pieces [ ] ; Stores the index of element in the given array arr [ ] ; Traverse over the list pieces ; If item size is 1 and exists in map ; If item contains > 1 element then check order of element ; If end of the array ; Check the order of elements ; If order is same as the array elements ; Increment idx ; If order breaks ; Otherwise ; Return false if the first element doesn 't exist in m ; Return true ; Driver Code ; Given target list ; Given array of list ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static bool check ( List < int > arr , List < List < int > > pieces ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < arr . Count ; i ++ ) m . Add ( arr [ i ] , i ) ; for ( int i = 0 ; i < pieces . Count ; i ++ ) { if ( pieces [ i ] . Count == 1 && m . ContainsKey ( pieces [ i ] [ 0 ] ) ) { continue ; } else if ( pieces [ i ] . Count > 1 && m . ContainsKey ( pieces [ i ] [ 0 ] ) ) { int idx = m [ pieces [ i ] [ 0 ] ] ; idx ++ ; if ( idx >= arr . Count ) return false ; for ( int j = 1 ; j < pieces [ i ] . Count ; j ++ ) { if ( arr [ idx ] == pieces [ i ] [ j ] ) { idx ++ ; if ( idx >= arr . Count && j < pieces [ i ] . Count - 1 ) return false ; } else { return false ; } } } else { return false ; } } return true ; } static public void Main ( ) { List < int > arr = new List < int > ( ) { 1 , 2 , 4 , 3 } ; List < List < int > > pieces = new List < List < int > > ( ) ; pieces . Add ( new List < int > ( ) { 1 } ) ; pieces . Add ( new List < int > ( ) { 4 , 3 } ) ; pieces . Add ( new List < int > ( ) { 2 } ) ; if ( check ( arr , pieces ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Modify given array to make sum of odd and even indexed elements same | C # program for the above approach ; Function to modify array to make sum of odd and even indexed elements equal ; Stores the count of 0 s , 1 s ; Stores sum of odd and even indexed elements respectively ; Count 0 s ; Count 1 s ; Calculate odd_sum and even_sum ; If both are equal ; Print the original array ; Otherwise ; Print all the 0 s ; For checking even or odd ; Update total count of 1 s ; Print all 1 s ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static void makeArraySumEqual ( int [ ] a , int N ) { int count_0 = 0 , count_1 = 0 ; int odd_sum = 0 , even_sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; if ( ( i + 1 ) % 2 == 0 ) even_sum += a [ i ] ; else if ( ( i + 1 ) % 2 > 0 ) odd_sum += a [ i ] ; } if ( odd_sum == even_sum ) { for ( int i = 0 ; i < N ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } else { if ( count_0 >= N / 2 ) { else { if ( count_0 >= N / 2 ) { for ( int i = 0 ; i < count_0 ; i ++ ) Console . Write ( "0 ▁ " ) ; } else { int is_Odd = count_1 % 2 ; count_1 -= is_Odd ; for ( int i = 0 ; i < count_1 ; i ++ ) Console . Write ( "1 ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 0 } ; int N = arr . Length ; makeArraySumEqual ( arr , N ) ; } }
Minimum replacement of pairs by their LCM required to reduce given array to its LCM | C # program for the above approach ; Boolean array to set or unset prime non - prime indices ; Stores the prefix sum of the count of prime numbers ; Function to check if a number is prime or not from 0 to N ; If p is a prime ; Set its multiples as non - prime ; Function to store the count of prime numbers ; Function to count the operations to reduce the array to one element by replacing each pair with its LCM ; Generating Prime Number ; Corner Case ; Driver code ; Stores the prefix sum of the count of prime numbers ; Given array [ ] arr ; Function call
using System ; class GFG { static readonly int maxm = 10001 ; static bool [ ] prime ; static int [ ] prime_number ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p < maxm ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < maxm ; i += p ) prime [ i ] = false ; } } prime [ 0 ] = false ; prime [ 1 ] = false ; } static void num_prime ( ) { prime_number [ 0 ] = 0 ; for ( int i = 1 ; i <= maxm ; i ++ ) { int tmp ; if ( prime [ i ] == true ) { tmp = 1 ; } else { tmp = 0 ; } prime_number [ i ] = prime_number [ i - 1 ] + tmp ; } } static void min_steps ( int [ ] arr , int n ) { SieveOfEratosthenes ( ) ; num_prime ( ) ; if ( n == 1 ) { Console . WriteLine ( "0" ) ; } else if ( n == 2 ) { Console . WriteLine ( "1" ) ; } else { Console . WriteLine ( prime_number [ n ] - 1 + ( n - 2 ) ) ; } } public static void Main ( String [ ] args ) { prime = new bool [ maxm + 1 ] ; prime_number = new int [ maxm + 1 ] ; int [ ] arr = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . Length ; min_steps ( arr , N ) ; } }
Find the winner of a game of removing any number of stones from the least indexed non | C # program for the above approach ; Function to find the winner of game between A and B ; win = 1 means B is winner win = 0 means A is winner ; If size is even , winner is B ; If size is odd , winner is A ; Stone will be removed by B ; B will take n - 1 stones from current pile having n stones and force A to pick 1 stone ; Stone will be removed by A ; A will take n - 1 stones from current pile having n stones and force B to pick 1 stone ; Print the winner accordingly ; Driver Code ; Given piles of stone ; Function call
using System ; class GFG { static void findWinner ( int [ ] a , int n ) { int win = 0 ; if ( n % 2 == 0 ) win = 1 ; else win = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( i % 2 == 1 ) { if ( win == 0 && a [ i ] > 1 ) win = 1 ; } else { if ( win == 1 && a [ i ] > 1 ) win = 0 ; } } if ( win == 0 ) Console . Write ( " A " ) ; else Console . Write ( " B " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 2 } ; int N = arr . Length ; findWinner ( arr , N ) ; } }
Check if a destination is reachable from source with two movements allowed | Set 2 | C # program to implement the above approach ; Check if ( x2 , y2 ) can be reached from ( x1 , y1 ) ; Reduce x2 by y2 until it is less than or equal to x1 ; Reduce y2 by x2 until it is less than or equal to y1 ; If x2 is reduced to x1 ; Check if y2 can be reduced to y1 or not ; If y2 is reduced to y1 ; Check if x2 can be reduced to x1 or not ; Driver Code
using System ; class GFG { static bool isReachable ( long x1 , long y1 , long x2 , long y2 ) { while ( x2 > x1 && y2 > y1 ) { if ( x2 > y2 ) x2 %= y2 ; else y2 %= x2 ; } if ( x2 == x1 ) return ( y2 - y1 ) >= 0 && ( y2 - y1 ) % x1 == 0 ; else if ( y2 == y1 ) return ( x2 - x1 ) >= 0 && ( x2 - x1 ) % y1 == 0 ; else return false ; } public static void Main ( String [ ] args ) { long source_x = 2 , source_y = 10 ; long dest_x = 26 , dest_y = 12 ; if ( isReachable ( source_x , source_y , dest_x , dest_y ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Find the player who wins the game by removing the last of given N cards | C # program to implement the above approach ; Function to check which player can win the game ; Driver code
using System ; class GFG { static void checkWinner ( int N , int K ) { if ( N % ( K + 1 ) > 0 ) { Console . Write ( " A " ) ; } else { Console . Write ( " B " ) ; } } public static void Main ( String [ ] args ) { int N = 50 ; int K = 10 ; checkWinner ( N , K ) ; } }
Length of longest subarray having only K distinct Prime Numbers | C # program for the above approach ; Function to precalculate all the prime up to 10 ^ 6 ; Initialize prime to true ; Iterate [ 2 , sqrt ( N ) ] ; If p is prime ; Mark all multiple of p as true ; Function that finds the length of longest subarray K distinct primes ; Precompute all prime up to 2 * 10 ^ 6 ; Keep track occurrence of prime ; Initialize result to - 1 ; If number is prime then increment its count and decrease k ; Decrement K ; Remove required elements till k become non - negative ; Decrease count so that it may appear in another subarray appearing after this present subarray ; Increment K ; Take the max value as length of subarray ; Return the readonly length ; Driver Code ; Given array [ ] arr ; Function call
using System ; using System . Collections . Generic ; class GFG { static bool [ ] isprime = new bool [ 2000010 ] ; static void SieveOfEratosthenes ( int n ) { for ( int i = 0 ; i < isprime . Length ; i ++ ) isprime [ i ] = true ; isprime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isprime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) isprime [ i ] = false ; } } } static int KDistinctPrime ( int [ ] arr , int n , int k ) { SieveOfEratosthenes ( 2000000 ) ; Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; int result = - 1 ; for ( int i = 0 , j = - 1 ; i < n ; ++ i ) { int x = arr [ i ] ; if ( isprime [ x ] ) { if ( cnt . ContainsKey ( x ) ) cnt [ x ] = cnt [ x ] + 1 ; else cnt . Add ( x , 1 ) ; if ( cnt [ x ] == 1 ) { -- k ; } } while ( k < 0 ) { x = arr [ ++ j ] ; if ( isprime [ x ] ) { if ( cnt . ContainsKey ( x ) ) cnt [ x ] = cnt [ x ] - 1 ; else cnt . Add ( x , 0 ) ; if ( cnt [ x ] == 0 ) { ++ k ; } } } if ( k == 0 ) result = Math . Max ( result , i - j ) ; } return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int K = 3 ; int N = arr . Length ; Console . WriteLine ( KDistinctPrime ( arr , N , K ) ) ; } }
Smallest composite number not divisible by first N prime numbers | C # program to implement the above approach ; Initializing the max value ; Function to generate N prime numbers using Sieve of Eratosthenes ; Stores the primes ; Setting all numbers to be prime initially ; If a prime number is encountered ; Set all its multiples as composites ; Store all the prime numbers ; Function to find the square of the ( N + 1 ) - th prime number ; Driver Code ; Stores all prime numbers
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX_SIZE = 1000005 ; static void SieveOfEratosthenes ( List < int > StorePrimes ) { bool [ ] IsPrime = new bool [ MAX_SIZE ] ; for ( int i = 0 ; i < MAX_SIZE ; i ++ ) IsPrime [ i ] = true ; for ( int p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( int i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } for ( int p = 2 ; p < MAX_SIZE ; p ++ ) if ( IsPrime [ p ] ) StorePrimes . Add ( p ) ; } static int Smallest_non_Prime ( List < int > StorePrimes , int N ) { int x = StorePrimes [ N ] ; return x * x ; } public static void Main ( String [ ] args ) { int N = 3 ; List < int > StorePrimes = new List < int > ( ) ; SieveOfEratosthenes ( StorePrimes ) ; Console . Write ( Smallest_non_Prime ( StorePrimes , N ) ) ; } }
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | C # program for the above approach ; Function to print the required N - th subset ; Nearest power of 2 <= N ; Now insert k ^ p in the answer ; update n ; Print the ans in sorted order ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printSubset ( int n , int k ) { List < int > answer = new List < int > ( ) ; while ( n > 0 ) { int p = ( int ) Math . Log ( n , 2 ) ; answer . Add ( ( int ) Math . Pow ( k , p ) ) ; n %= ( int ) Math . Pow ( 2 , p ) ; } answer . Reverse ( ) ; foreach ( int x in answer ) { Console . Write ( x + " ▁ " ) ; } } static void Main ( ) { int n = 5 ; int k = 4 ; printSubset ( n , k ) ; } }