codeparrot/xlcost-text-to-code huggingface dataset:
Count of distinct Primonacci Numbers in a given range [ L , R ] | C # Program to implement the above approach ; Stores list of all primes ; Function to find all primes ; To mark the prime ones ; Initially all indices as prime ; If i is prime ; Set all multiples of i as non - prime ; Adding all primes to a list ; Function to return the count of Primonacci Numbers in the range [ l , r ] ; dp [ i ] contains ith Primonacci Number ; Stores the Primonacci Numbers ; Iterate over all smaller primes ; If Primonacci number lies within the range [ L , R ] ; Count of Primonacci Numbers ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < int > primes ; static int M = 100005 ; static void sieve ( ) { primes = new List < int > ( ) ; bool [ ] mark = new bool [ M ] ; for ( int i = 2 ; i < M ; i ++ ) mark [ i ] = true ; for ( int i = 2 ; i * i < M ; i ++ ) { if ( mark [ i ] ) { for ( int j = i * i ; j < M ; j += i ) mark [ j ] = false ; } } for ( int i = 2 ; i < M ; i ++ ) if ( mark [ i ] ) primes . Add ( i ) ; } static void countPrimonacci ( int l , int r ) { List < int > dp = new List < int > ( ) ; dp . Add ( 1 ) ; dp . Add ( 1 ) ; int i = 2 ; HashSet < int > s = new HashSet < int > ( ) ; while ( true ) { int x = 0 ; for ( int j = 0 ; j < primes . Count ; j ++ ) { int p = primes [ j ] ; if ( p >= i ) break ; x += dp [ i - p ] ; } if ( x >= l && x <= r ) s . Add ( x ) ; if ( x > r ) break ; dp . Add ( x ) ; i ++ ; } Console . WriteLine ( s . Count ) ; } public static void Main ( String [ ] args ) { sieve ( ) ; int L = 1 , R = 10 ; countPrimonacci ( L , R ) ; } }
Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties | C # program to implement the above approach ; Function to calculate the count of possible Strings ; Initialize dp [ 0 ] ; dp [ i ] represents count of Strings of length i ; Add dp [ i - k ] if i >= k ; Update Prefix Sum Array ; Driver Code
using System ; class GFG { static int N = ( int ) ( 1e5 + 5 ) ; static int MOD = 1000000007 ; static int [ ] dp = new int [ N ] ; static void countStrings ( int K , int [ , ] Q ) { dp [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( i >= K ) dp [ i ] = ( dp [ i ] + dp [ i - K ] ) % MOD ; } for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = ( dp [ i ] + dp [ i - 1 ] ) % MOD ; } for ( int i = 0 ; i < Q . GetLength ( 0 ) ; i ++ ) { int ans = dp [ Q [ i , 1 ] ] - dp [ Q [ i , 0 ] - 1 ] ; if ( ans < 0 ) ans = ans + MOD ; Console . Write ( ans + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int K = 3 ; int [ , ] Q = { { 1 , 4 } , { 3 , 7 } } ; countStrings ( K , Q ) ; } }
Minimum possible sum of prices of a Triplet from the given Array | C # program to implement the above approach ; Function to minimize the sum of price by taking a triplet ; Initialize a [ ] dp array ; Stores the readonly result ; Iterate for all values till N ; Check if num [ j ] > num [ i ] ; Update dp [ j ] if it is greater than stored value ; Update the minimum sum as ans ; If there is no minimum sum exist then print - 1 else print the ans ; Driver Code
using System ; class GFG { public static long minSum ( int n , int [ ] num , int [ ] price ) { long [ ] dp = new long [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = int . MaxValue ; long ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( num [ j ] > num [ i ] ) { dp [ j ] = ( long ) Math . Min ( ( long ) dp [ j ] , ( long ) price [ i ] + ( long ) price [ j ] ) ; ans = Math . Min ( ans , ( long ) dp [ i ] + ( long ) price [ j ] ) ; } } } return ans != int . MaxValue ? ans : - 1 ; } public static void Main ( String [ ] args ) { int [ ] num = { 2 , 4 , 6 , 7 , 8 } ; int [ ] price = { 10 , 20 , 100 , 20 , 40 } ; int n = price . Length ; Console . WriteLine ( minSum ( n , num , price ) ) ; } }
Count of binary strings of length N having equal count of 0 ' s ▁ and ▁ 1' s and count of 1 ' s ▁ â ‰ ¥ ▁ count ▁ of ▁ 0' s in each prefix substring | C # program to implement the above approach ; Function to calculate and returns the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Function to return the count of all binary strings having equal count of 0 ' s ▁ and ▁ 1' s and each prefix substring having frequency of 1 ' s ▁ > = ▁ frequencies ▁ of ▁ 0' s ; If N is odd ; No such strings possible ; Otherwise ; Calculate value of 2 nCn ; Return 2 nCn / ( n + 1 ) ; Driver code
using System ; class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long countStrings ( int N ) { if ( N % 2 == 1 ) return 0 ; else { N /= 2 ; long c = binomialCoeff ( 2 * N , N ) ; return c / ( N + 1 ) ; } } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( countStrings ( N ) + " ▁ " ) ; } }
Number of ways to cut a stick of length N into in even length at most K units long pieces | C # program for the above approach ; Recursive function to count the total number of ways ; Base case if no - solution exist ; Condition if a solution exist ; Check if already calculated ; Initialize counter ; Recursive call ; Store the answer ; Return the answer ; Driver code
using System ; class GFG { static int solve ( int n , int k , int mod , int [ ] dp ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int cnt = 0 ; for ( int i = 2 ; i <= k ; i += 2 ) { cnt = ( cnt % mod + solve ( n - i , k , mod , dp ) % mod ) % mod ; } dp [ n ] = cnt ; return cnt ; } public static void Main ( String [ ] args ) { int mod = ( int ) ( 1e9 + 7 ) ; int n = 4 , k = 2 ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; int ans = solve ( n , k , mod , dp ) ; Console . WriteLine ( ans ) ; } }
Number of ways to select exactly K even numbers from given Array | C # program for the above approach ; Function for calculating factorial ; Factorial of n defined as : n ! = n * ( n - 1 ) * ... * 1 ; Function to find the number of ways to select exactly K even numbers from the given array ; Count even numbers ; Check if the current number is even ; Check if the even numbers to be chosen is greater than n . Then , there is no way to pick it . ; The number of ways will be nCk ; Driver Code ; Given array [ ] arr ; Given count of even elements ; Function call
using System ; class GFG { static int [ ] f = new int [ 12 ] ; static void fact ( ) { f [ 0 ] = f [ 1 ] = 1 ; for ( int i = 2 ; i <= 10 ; i ++ ) f [ i ] = i * 1 * f [ i - 1 ] ; } static void solve ( int [ ] arr , int n , int k ) { fact ( ) ; int even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; } if ( k > even ) Console . Write ( 0 + " STRNEWLINE " ) ; else { Console . Write ( f [ even ] / ( f [ k ] * f [ even - k ] ) ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 1 ; solve ( arr , n , k ) ; } }
Count of binary strings of length N having equal count of 0 ' s ▁ and ▁ 1' s | C # program for the above approach ; Function to calculate C ( n , r ) % MOD DP based approach ; Corner case ; Stores the last row of Pascal 's Triangle ; Initialize top row of pascal triangle ; Construct Pascal 's Triangle  from top to bottom ; Fill current row with the help of previous row ; C ( n , j ) = C ( n - 1 , j ) + C ( n - 1 , j - 1 ) ; Driver code
using System ; class GFG { static int MOD = 1000000007 ; static int nCrModp ( int n , int r ) { if ( n % 2 == 1 ) { return - 1 ; } int [ ] C = new int [ r + 1 ] ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . Min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % MOD ; } return C [ r ] ; } static void Main ( ) { int N = 6 ; Console . WriteLine ( nCrModp ( N , N / 2 ) ) ; } }
Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1 | C # program for the above approach ; Function to print the Minimum number of operations required to convert 1 into X by using three given operations ; Initialize a DP array to store min operations for sub - problems ; Base Condition : No operation required when N is 1 ; Multiply the number by 2 ; Multiply the number by 3 ; Add 1 to the number . ; Print the minimum operations ; Initialize a list to store the sequence ; Backtrack to find the sequence ; If add by 1 ; If multiply by 2 ; If multiply by 3 ; Print the sequence of operation ; Driver code ; Given number X ; Function call
using System ; using System . Collections . Generic ; class GFG { static void printMinOperations ( int N ) { int [ ] dp = new int [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = N ; dp [ 1 ] = 0 ; for ( int i = 2 ; i < N + 1 ; i ++ ) { if ( i % 2 == 0 && dp [ i ] > dp [ i / 2 ] + 1 ) dp [ i ] = dp [ i / 2 ] + 1 ; if ( i % 3 == 0 && dp [ i ] > dp [ i / 3 ] + 1 ) dp [ i ] = dp [ i / 3 ] + 1 ; if ( dp [ i ] > dp [ i - 1 ] + 1 ) dp [ i ] = dp [ i - 1 ] + 1 ; } Console . WriteLine ( dp [ N ] ) ; List < int > seq = new List < int > ( ) ; while ( N > 1 ) { seq . Add ( N ) ; if ( dp [ N - 1 ] == dp [ N ] - 1 ) N = N - 1 ; else if ( N % 2 == 0 && dp [ N / 2 ] == dp [ N ] - 1 ) N = N / 2 ; else N = N / 3 ; } seq . Add ( 1 ) ; seq . Reverse ( ) ; foreach ( var i in seq ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int X = 96234 ; printMinOperations ( X ) ; } }
Largest subsequence such that all indices and all values are multiples individually | C # program for the above approach ; Function that print maximum length of array ; dp [ ] array to store the maximum length ; Find all divisors of i ; If the current value is greater than the divisor 's value ; If current value is greater than the divisor 's value  and s is not equal  to current index ; Condition if current value is greater than the divisor 's value ; Computing the greatest value ; Printing maximum length of array ; Driver Code ; Given array arr [ ] ; Function call
using System ; class GFG { static void maxLength ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = 1 ; } for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 1 ; j <= Math . Sqrt ( i ) ; j ++ ) { if ( i % j == 0 ) { int s = i / j ; if ( s == j ) { if ( arr [ i ] > arr [ s ] ) { dp [ s ] = Math . Max ( dp [ i ] + 1 , dp [ s ] ) ; } } else { if ( s != i && arr [ i ] > arr [ s ] ) dp [ s ] = Math . Max ( dp [ i ] + 1 , dp [ s ] ) ; if ( arr [ i ] > arr [ j ] ) { dp [ j ] = Math . Max ( dp [ i ] + 1 , dp [ j ] ) ; } } } } } int max = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( dp [ i ] > max ) max = dp [ i ] ; } Console . WriteLine ( max ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 } ; int size = arr . Length ; maxLength ( arr , size ) ; } }
Maximum profit by buying and selling a stock at most twice | Set 2 | C # implmenetation to find maximum possible profit with at most two transactions ; Function to find the maximum profit with two transactions on a given list of stock prices ; buy1 - Money lent to buy 1 stock profit1 - Profit after selling the 1 st stock buyed . buy2 - Money lent to buy 2 stocks including profit of selling 1 st stock profit2 - Profit after selling 2 stocks ; Set initial buying values to int . MaxValue as we want to minimize it ; Set initial selling values to zero as we want to maximize it ; Money lent to buy the stock should be minimum as possible . buy1 tracks the minimum possible stock to buy from 0 to i - 1. ; Profit after selling a stock should be maximum as possible . profit1 tracks maximum possible profit we can make from 0 to i - 1. ; Now for buying the 2 nd stock , we will integrate profit made from selling the 1 st stock ; Profit after selling a 2 stocks should be maximum as possible . profit2 tracks maximum possible profit we can make from 0 to i - 1. ; Driver Code
using System ; class GFG { static int maxProfit ( int [ ] price , int n ) { int buy1 , profit1 , buy2 , profit2 ; buy1 = buy2 = int . MaxValue ; profit1 = profit2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { buy1 = Math . Min ( buy1 , price [ i ] ) ; profit1 = Math . Max ( profit1 , price [ i ] - buy1 ) ; buy2 = Math . Min ( buy2 , price [ i ] - profit1 ) ; profit2 = Math . Max ( profit2 , price [ i ] - buy2 ) ; } return profit2 ; } public static void Main ( String [ ] args ) { int [ ] price = { 2 , 30 , 15 , 10 , 8 , 25 , 80 } ; int n = price . Length ; Console . Write ( " Maximum ▁ Profit ▁ = ▁ " + maxProfit ( price , n ) ) ; } }
Maximum score of deleting an element from an Array based on given condition | C # implementation to find the maximum score of the deleting a element from an array ; Function to find the maximum score of the deleting an element from an array ; Creating a map to keep the frequency of numbers ; Loop to iterate over the elements of the array ; Creating a DP array to keep count of max score at ith element and it will be filled in the bottom Up manner ; Loop to choose the elements of the array to delete from the array ; Driver Code ; Function call
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int findMaximumScore ( int [ ] a , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( a [ i ] ) ) { freq [ a [ i ] ] = freq [ a [ i ] ] + 1 ; } else { freq . Add ( a [ i ] , 1 ) ; } } int [ ] dp = new int [ a . Max ( ) + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = freq [ 1 ] ; for ( int i = 2 ; i < dp . Length ; i ++ ) dp [ i ] = Math . Max ( dp [ i - 1 ] , dp [ i - 2 ] + freq [ i ] * i ) ; return dp [ dp . Length - 1 ] ; } public static void Main ( String [ ] args ) { int n ; n = 3 ; int [ ] a = { 1 , 2 , 3 } ; Console . Write ( findMaximumScore ( a , n ) ) ; } }
Reverse a subarray to maximize sum of even | C # program to implement the above approach ; Function to return maximized sum at even indices ; Stores difference with element on the left ; Stores difference with element on the right ; Compute and store left difference ; For first index ; If previous difference is positive ; Otherwise ; For the last index ; Otherwise ; For first index ; If the previous difference is positive ; Driver Code
using System ; class GFG { public static int maximizeSum ( int [ ] arr ) { int n = arr . Length ; int sum = 0 ; for ( int i = 0 ; i < n ; i = i + 2 ) sum += arr [ i ] ; int [ ] leftDP = new int [ n / 2 ] ; int [ ] rightDP = new int [ n / 2 ] ; int c = 0 ; for ( int i = 1 ; i < n ; i = i + 2 ) { int leftDiff = arr [ i ] - arr [ i - 1 ] ; if ( c - 1 < 0 ) leftDP = leftDiff ; else { if ( leftDP > 0 ) leftDP = leftDiff + leftDP ; else leftDP = leftDiff ; } int rightDiff ; if ( i + 1 >= arr . Length ) rightDiff = 0 ; else rightDiff = arr [ i ] - arr [ i + 1 ] ; if ( c - 1 < 0 ) rightDP = rightDiff ; else { if ( rightDP > 0 ) rightDP = rightDiff + rightDP ; else rightDP = rightDiff ; } c ++ ; } int max = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { max = Math . Max ( max , Math . Max ( leftDP [ i ] , rightDP [ i ] ) ) ; } return max + sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 8 , 4 , 5 , 7 , 6 , 8 , 9 , 7 , 3 } ; int ans = maximizeSum ( arr ) ; Console . WriteLine ( ans ) ; } }
Count of all subsequences having adjacent elements with different parity | C # program implementation of the approach ; Function to find required subsequences ; dp [ i ] [ 0 ] : Stores the number of subsequences till i - th index ending with even element dp [ i ] [ 1 ] : Stores the number of subsequences till i - th index ending with odd element ; Initialise the dp [ ] [ ] with 0. ; If odd element is encountered ; Considering i - th element will be present in the subsequence ; Appending i - th element to all non - empty subsequences ending with even element till ( i - 1 ) th indexes ; Considering ith element will not be present in the subsequence ; Considering i - th element will be present in the subsequence ; Appending i - th element to all non - empty subsequences ending with odd element till ( i - 1 ) th indexes ; Considering ith element will not be present in the subsequence ; Count of all valid subsequences ; Driver code
using System ; class GFG { static int validsubsequences ( int [ ] arr , int n ) { long [ , ] dp = new long [ n + 1 , 2 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { dp [ i , 0 ] = 0 ; dp [ i , 1 ] = 0 ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] % 2 != 0 ) { dp [ i , 1 ] += 1 ; dp [ i , 1 ] += dp [ i - 1 , 0 ] ; dp [ i , 1 ] += dp [ i - 1 , 1 ] ; dp [ i , 0 ] += dp [ i - 1 , 0 ] ; } else { dp [ i , 0 ] += 1 ; dp [ i , 0 ] += dp [ i - 1 , 1 ] ; dp [ i , 0 ] += dp [ i - 1 , 0 ] ; dp [ i , 1 ] += dp [ i - 1 , 1 ] ; } } return ( int ) ( dp [ n , 0 ] + dp [ n , 1 ] ) ; } public static void Main ( ) { int [ ] arr = { 5 , 6 , 9 , 7 } ; int n = arr . Length ; Console . Write ( validsubsequences ( arr , n ) ) ; } }
Count of N | C # Program to implement the above approach ; Function to return the count of N - digit numbers with absolute difference of adjacent digits not exceeding K ; For 1 - digit numbers , the count is 10 ; dp [ i ] [ j ] stores the count of i - digit numbers ending with j ; Initialize count for 1 - digit numbers ; Compute values for count of digits greater than 1 ; Find the range of allowed numbers with last digit j ; Perform Range update ; Prefix sum to count i - digit numbers ending in j ; Stores the final answer ; Driver Code
using System ; class GFG { static long getCount ( int n , int k ) { if ( n == 1 ) return 10 ; long [ , ] dp = new long [ n + 1 , 11 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) dp [ 1 , i ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { int l = Math . Max ( 0 , j - k ) ; int r = Math . Min ( 9 , j + k ) ; dp [ i , l ] += dp [ i - 1 , j ] ; dp [ i , r + 1 ] -= dp [ i - 1 , j ] ; } for ( int j = 1 ; j <= 9 ; j ++ ) dp [ i , j ] += dp [ i , j - 1 ] ; } long count = 0 ; for ( int i = 0 ; i <= 9 ; i ++ ) count += dp [ n , i ] ; return count ; } public static void Main ( ) { int n = 2 , k = 1 ; Console . WriteLine ( getCount ( n , k ) ) ; } }
Find if there is a path between two vertices in a directed graph | Set 2 | C # program to find if there is a path between two vertices in a directed graph using Dynamic Programming ; Function to find if there is a path between two vertices in a directed graph ; mat matrix ; set mat [ i , j ] = true if there is edge between i to j ; Check for all intermediate vertex ; If vertex is invalid ; If there is a path ; Driver code
using System ; class GFG { static readonly int X = 6 ; static readonly int Z = 2 ; static bool existPath ( int V , int [ , ] edges , int u , int v ) { bool [ , ] mat = new bool [ V , V ] ; for ( int i = 0 ; i < X ; i ++ ) mat [ edges [ i , 0 ] , edges [ i , 1 ] ] = true ; for ( int k = 0 ; k < V ; k ++ ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { mat [ i , j ] = mat [ i , j ] || mat [ i , k ] && mat [ k , j ] ; } } } if ( u >= V v >= V ) { return false ; } if ( mat [ u , v ] ) return true ; return false ; } public static void Main ( String [ ] args ) { int V = 4 ; int [ , ] edges = { { 0 , 2 } , { 0 , 1 } , { 1 , 2 } , { 2 , 3 } , { 2 , 0 } , { 3 , 3 } } ; int u = 1 , v = 3 ; if ( existPath ( V , edges , u , v ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } }
Maximize the numbers of splits in an Array having sum divisible by 3 | C # implementation of the above approach ; Prefix array storing right most index with prefix sums 0 , 1 , 2 ; dp array ; Current prefix sum ; Calculating the prefix sum modulo 3 ; We dont have a left pointer with prefix sum C ; We cannot consider i as a right pointer of any segment ; We have a left pointer pre [ C ] with prefix sum C ; i is the rightmost index of prefix sum C ; Driver code
using System ; class GFG { static int calculate_maximum_splits ( int [ ] arr , int N ) { int [ ] pre = { 0 , - 1 , - 1 } ; int [ ] dp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] = 0 ; } int C = 0 ; for ( int i = 0 ; i < N ; i ++ ) { C = C + arr [ i ] ; C = C % 3 ; if ( pre [ C ] == - 1 ) { if ( 1 <= i ) dp [ i ] = dp [ i - 1 ] ; } else { dp [ i ] = Math . Max ( dp [ i - 1 ] , dp [ pre [ C ] ] + 1 ) ; } pre [ C ] = i ; } return dp [ N - 1 ] ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 36 , 1 , 9 , 2 , 0 , 1 , 8 , 1 } ; int N = arr . Length ; Console . Write ( calculate_maximum_splits ( arr , N ) ) ; } }
Count of elements having Euler 's Totient value one less than itself | C # program for the above approach ; Seiev of Erotosthenes method to compute all primes ; If current number is marked prime then mark its multiple as non - prime ; Function to count the number of element satisfying the condition ; Compute the number of primes in count prime array ; Print the number of elements satisfying the condition ; Driver Code ; Given array ; Size of the array ; Function Call
using System ; class GFG { static int [ ] prime = new int [ 1000001 ] ; static void seiveOfEratosthenes ( ) { for ( int i = 2 ; i < 1000001 ; i ++ ) { prime [ i ] = 1 ; } for ( int i = 2 ; i * i < 1000001 ; i ++ ) { if ( prime [ i ] == 1 ) { for ( int j = i * i ; j < 1000001 ; j += i ) { prime [ j ] = 0 ; } } } } static void CountElements ( int [ ] arr , int n , int L , int R ) { seiveOfEratosthenes ( ) ; int [ ] countPrime = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { countPrime [ i ] = countPrime [ i - 1 ] + prime [ arr [ i - 1 ] ] ; } Console . Write ( countPrime [ R ] - countPrime [ L - 1 ] + " STRNEWLINE " ) ; return ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 5 , 8 } ; int N = arr . Length ; int L = 1 , R = 3 ; CountElements ( arr , N , L , R ) ; } }
Count of elements having odd number of divisors in index range [ L , R ] for Q queries | C # program for the above approach ; Function count the number of elements having odd number of divisors ; Initialise [ ] dp array ; Precomputation ; Find the Prefix Sum ; Iterate for each query ; Find the answer for each query ; Driver Code ; Given array [ ] arr ; Given Query ; Function Call
using System ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void OddDivisorsCount ( int n , int q , int [ ] a , pair [ ] Query ) { int [ ] DP = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = ( int ) Math . Sqrt ( a [ i ] ) ; if ( x * x == a [ i ] ) DP [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { DP [ i ] = DP [ i - 1 ] + DP [ i ] ; } int l , r ; for ( int i = 0 ; i < q ; i ++ ) { l = Query [ i ] . first ; r = Query [ i ] . second ; if ( l == 0 ) { Console . Write ( DP [ r ] + " STRNEWLINE " ) ; } else { Console . Write ( DP [ r ] - DP [ l - 1 ] + " STRNEWLINE " ) ; } } } public static void Main ( String [ ] args ) { int N = 5 ; int Q = 3 ; int [ ] arr = { 2 , 4 , 5 , 6 , 9 } ; pair [ ] Query = { new pair ( 0 , 2 ) , new pair ( 1 , 3 ) , new pair ( 1 , 4 ) } ; OddDivisorsCount ( N , Q , arr , Query ) ; } }