codeparrot/xlcost-text-to-code huggingface dataset:
Find the Product of first N Prime Numbers | C ++ implementation of above solution ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Set all multiples of p to non - prime ; find the product of 1 st N prime numbers ; count of prime numbers ; product of prime numbers ; if the number is prime add it ; increase the count ; get to next number ; Driver code ; create the sieve ; find the value of 1 st n prime numbers
#include " cstring " NEW_LINE #include <iostream> NEW_LINE using namespace std ; #define MAX  10000 NEW_LINE bool prime [ MAX + 1 ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } int solve ( int n ) { int count = 0 , num = 1 ; long long int prod = 1 ; while ( count < n ) { if ( prime [ num ] ) { prod *= num ; count ++ ; } num ++ ; } return prod ; } int main ( ) { SieveOfEratosthenes ( ) ; int n = 5 ; cout << solve ( n ) ; return 0 ; }
Program to find count of numbers having odd number of divisors in given range | C ++ implementation of the approach ; Function to return the count of divisors of a number ; Count the powers of the current prime i which divides a ; Update the count of divisors ; Reset the count ; If the remaining a is prime then a ^ 1 will be one of its prime factors ; Function to count numbers having odd number of divisors in range [ A , B ] ; To store the count of elements having odd number of divisors ; Iterate from a to b and find the count of their divisors ; To store the count of divisors of i ; If the divisor count of i is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divisor ( int a ) { int div = 1 , count = 0 ; for ( int i = 2 ; i <= sqrt ( a ) ; i ++ ) { while ( a % i == 0 ) { count ++ ; a = a / i ; } div = div * ( count + 1 ) ; count = 0 ; } if ( a > 1 ) { div = div * ( 2 ) ; } return div ; } int OddDivCount ( int a , int b ) { int res = 0 ; for ( int i = a ; i <= b ; ++ i ) { int divCount = divisor ( i ) ; if ( divCount % 2 ) { ++ res ; } } return res ; } int main ( ) { int a = 1 , b = 10 ; cout << OddDivCount ( a , b ) ; return 0 ; }
Check if there is any pair in a given range with GCD is divisible by k | C ++ program to count the numbers divisible by k in a given range ; Returns count of numbers in [ l r ] that are divisible by k . ; Add 1 explicitly as l is divisible by k ; l is not divisible by k ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Check_is_possible ( int l , int r , int k ) { int div_count = ( r / k ) - ( l / k ) ; if ( l % k == 0 ) div_count ++ ; return ( div_count > 1 ) ; } int main ( ) { int l = 30 , r = 70 , k = 10 ; if ( Check_is_possible ( l , r , k ) ) cout << " YES STRNEWLINE " ; else cout << " NO STRNEWLINE " ; return 0 ; }
Find sum of N | C ++ program to find sum in Nth group ; calculate sum of Nth group ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nth_group ( int n ) { return n * ( 2 * pow ( n , 2 ) + 1 ) ; } int main ( ) { int N = 5 ; cout << nth_group ( N ) ; return 0 ; }
Find if a molecule can be formed from 3 atoms using their valence numbers | C ++ implementation of the above approach ; Function to check if it is possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPossible ( int a , int b , int c ) { if ( ( a + b + c ) % 2 != 0 a + b < c ) cout << " NO " ; else cout << " YES " ; } int main ( ) { int a = 2 , b = 4 , c = 2 ; printPossible ( a , b , c ) ; return 0 ; }
Check if a number is a Trojan Number | CPP program to check if a number is Trojan Number or not ; Function to check if a number can be expressed as x ^ y ; Try all numbers from 2 to sqrt ( n ) as base ; Keep increasing y while power ' p ' is smaller than n . ; Function to check if a number is Strong ; count the number for each prime factor ; minimum number of prime divisors should be 2 ; Function to check if a number is Trojan Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectPower ( int n ) { if ( n == 1 ) return true ; for ( int x = 2 ; x <= sqrt ( n ) ; x ++ ) { int y = 2 ; int p = pow ( x , y ) ; while ( p <= n && p > 0 ) { if ( p == n ) return true ; y ++ ; p = pow ( x , y ) ; } } return false ; } bool isStrongNumber ( int n ) { unordered_map < int , int > count ; while ( n % 2 == 0 ) { n = n / 2 ; count [ 2 ] ++ ; } for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { n = n / i ; count [ i ] ++ ; } } if ( n > 2 ) count [ n ] ++ ; int flag = 0 ; for ( auto b : count ) { if ( b . second == 1 ) { flag = 1 ; break ; } } if ( flag == 1 ) return false ; else return true ; } bool isTrojan ( int n ) { if ( ! isPerfectPower ( n ) && isStrongNumber ( n ) ) return true ; else return false ; } int main ( ) { int n = 108 ; if ( isTrojan ( n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Find the sum of first N terms of the series 2 Ã — 3 + 4 Ã — 4 + 6 Ã — 5 + 8 Ã — 6 + ... | C ++ program to find sum upto N term of the series : 2 A 3 + 4 A 4 + 6 A 5 + 8 A 6 + ... ; calculate sum upto N term of series ; Driver code
#include <iostream> NEW_LINE using namespace std ; void Sum_upto_nth_Term ( int n ) { int r = n * ( n + 1 ) * ( 2 * n + 7 ) / 3 ; cout << r ; } int main ( ) { int N = 5 ; Sum_upto_nth_Term ( N ) ; return 0 ; }
Absolute Difference between the Sum of Non | C ++ program to find the Absolute Difference between the Sum of Non - Prime numbers and Prime numbers of an Array ; Function to find the difference between the sum of non - primes and the sum of primes of an array . ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the sum of primes in S1 and the sum of non primes in S2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Driver Code ; Get the array ; Find the absolute difference
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CalculateDifference ( int arr [ ] , int n ) { int max_val = * max_element ( arr , arr + n ) ; vector < bool > prime ( max_val + 1 , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int S1 = 0 , S2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { S1 += arr [ i ] ; } else if ( arr [ i ] != 1 ) { S2 += arr [ i ] ; } } return abs ( S2 - S1 ) ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CalculateDifference ( arr , n ) ; return 0 ; }
Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | C ++ implementation of the approach ; Function to compute the series sum ; To store the value of S [ i - 1 ] ; Iterate over n to store sum in total ; Update previous with S [ i ] ; Driver code ; Get x and n ; Find and print the sum
#include <iostream> NEW_LINE using namespace std ; double sum ( int x , int n ) { double total = 1.0 ; double previous = 1.0 ; for ( int i = 1 ; i <= n ; i ++ ) { previous = ( previous * x ) / ( i + 1 ) ; total = total + previous ; } return total ; } int main ( ) { int x = 5 , n = 4 ; cout << " Sum ▁ is : ▁ " << sum ( x , n ) ; return 0 ; }
Count number of integers less than or equal to N which has exactly 9 divisors | C ++ implementation of above approach ; Function to count factors in O ( N ) ; iterate and check if factor or not ; Function to count numbers having exactly 9 divisors ; check for all numbers <= N ; check if exactly 9 factors or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfDivisors ( int num ) { int c = 0 ; for ( int i = 1 ; i <= num ; i ++ ) { if ( num % i == 0 ) { c += 1 ; } } return c ; } int countNumbers ( int n ) { int c = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( numberOfDivisors ( i ) == 9 ) c += 1 ; } return c ; } int main ( ) { int n = 1000 ; cout << countNumbers ( n ) ; return 0 ; }
Number of distinct integers obtained by lcm ( X , N ) / X | C ++ program to find distinct integers ontained by lcm ( x , num ) / x ; Function to count the number of distinct integers ontained by lcm ( x , num ) / x ; iterate to count the number of factors ; Driver Code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int numberOfDistinct ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { ans ++ ; if ( ( n / i ) != i ) ans ++ ; } } return ans ; } int main ( ) { int n = 3 ; cout << numberOfDistinct ( n ) ; return 0 ; }
Ulam Number Sequence | CPP code to print nth Ulam number ; Array to store Ulam Number ; function to compute ulam Number ; push First 2 two term of the sequence in the array for further calculation ; loop to generate Ulam number ; traverse the array and check if i can be represented as sum of two distinct element of the array ; If count is 1 that means i can be represented as sum of two distinct terms of the sequence ; i is ulam number ; Driver code ; Pre compute Ulam Number sequence ; Print nth Ulam number
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10000 NEW_LINE vector < int > arr ; void ulam ( ) { arr . push_back ( 1 ) ; arr . push_back ( 2 ) ; for ( int i = 3 ; i < MAX ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr . size ( ) - 1 ; j ++ ) { for ( int k = j + 1 ; k < arr . size ( ) ; k ++ ) { if ( arr [ j ] + arr [ k ] == i ) { count ++ ; } if ( count > 1 ) break ; } if ( count > 1 ) break ; } if ( count == 1 ) { arr . push_back ( i ) ; } } } int main ( ) { ulam ( ) ; int n = 9 ; cout << arr [ n - 1 ] ; return 0 ; }
Find the number of rectangles of size 2 * 1 which can be placed inside a rectangle of size n * m | CPP program to Find the number of rectangles of size 2 * 1 can be placed inside a rectangle of size n * m ; function to Find the number of rectangles of size 2 * 1 can be placed inside a rectangle of size n * m ; if n is even ; if m is even ; if both are odd ; Driver code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int NumberOfRectangles ( int n , int m ) { if ( n % 2 == 0 ) return ( n / 2 ) * m ; else if ( m % 2 == 0 ) return ( m / 2 ) * n ; return ( n * m - 1 ) / 2 ; } int main ( ) { int n = 3 , m = 3 ; cout << NumberOfRectangles ( n , m ) ; return 0 ; }
Next greater Number than N with the same quantity of digits A and B | C ++ program to find next greater Number than N with the same quantity of digits A and B ; Recursive function to find the required number ; If the resulting number is >= n and count of a = count of b , return the number ; select minimum of two and call the function again ; Function to find the number next greater Number than N with the same quantity of digits A and B ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long findNumUtil ( long res , int a , int aCount , int b , int bCount , int n ) { if ( res > 1e11 ) return 1e11 ; if ( aCount == bCount && res >= n ) return res ; return min ( findNumUtil ( res * 10 + a , a , aCount + 1 , b , bCount , n ) , findNumUtil ( res * 10 + b , a , aCount , b , bCount + 1 , n ) ) ; } int findNum ( int n , int a , int b ) { int result = 0 ; int aCount = 0 ; int bCount = 0 ; return findNumUtil ( result , a , aCount , b , bCount , n ) ; } int main ( ) { int N = 4500 ; int A = 4 ; int B = 7 ; cout << findNum ( N , A , B ) ; return 0 ; }
Minimum and maximum number of N chocolates after distribution among K students | CPP implementation of the above approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int n = 7 , k = 3 ; if ( n % k == 0 ) cout << n / k << " ▁ " << n / k ; else cout << ( ( n - ( n % k ) ) / k ) << " ▁ " << ( ( ( n - ( n % k ) ) / k ) + 1 ) ; return 0 ; }
Total money to be paid after traveling the given number of hours | CPP implementation of the above approach ; calculating hours travelled
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { float m = 50 , n = 5 , x = 67 , h = 2927 ; int z = ( ceil ( h / 60 * 1.0 ) ) ; if ( z <= n ) cout << z * m ; else cout << n * m + ( z - n ) * x ; return 0 ; }
Absolute difference between sum and product of roots of a quartic equation | C ++ implementation of above approach ; Function taking coefficient of each term of equation as input ; Finding sum of roots ; Finding product of roots ; Absolute difference ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double sumProductDifference ( int a , int b , int c , int d , int e ) { double rootSum = ( double ) ( -1 * b ) / a ; double rootProduct = ( double ) e / a ; return abs ( rootSum - rootProduct ) ; } int main ( ) { cout << sumProductDifference ( 8 , 4 , 6 , 4 , 1 ) ; return 0 ; }
Number of solutions of n = x + n âŠ • x | C ++ implementation of above approach ; Function to find the number of solutions of n = n xor x ; Counter to store the number of solutions found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfSolutions ( int n ) { int c = 0 ; for ( int x = 0 ; x <= n ; ++ x ) if ( n == x + n ^ x ) ++ c ; return c ; } int main ( ) { int n = 3 ; cout << numberOfSolutions ( n ) ; return 0 ; }
Program to find minimum number of lectures to attend to maintain 75 % | C ++ Program to find minimum number of lectures to attend to maintain 75 % attendance ; Function to compute minimum lecture ; Formula to compute ; Driver function
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int minimumLectures ( int m , int n ) { int ans = 0 ; if ( n < ( int ) ceil ( 0.75 * m ) ) ans = ( int ) ceil ( ( ( 0.75 * m ) - n ) / 0.25 ) ; else ans = 0 ; return ans ; } int main ( ) { int M = 9 , N = 1 ; cout << minimumLectures ( M , N ) ; return 0 ; }
Count Numbers with N digits which consists of odd number of 0 's | C ++ program to count numbers with N digits which consists of odd number of 0 's ; Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int N ) { return ( pow ( 10 , N ) - pow ( 8 , N ) ) / 2 ; } int main ( ) { int n = 5 ; cout << countNumbers ( n ) << endl ; return 0 ; }
Sum of all Primes in a given range using Sieve of Eratosthenes | CPP program to find sum of primes in range L to R ; prefix [ i ] is going to store sum of primes till i ( including i ) . ; Function to build the prefix sum array ; Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Build prefix array ; Function to return sum of prime in range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10000 ; int prefix [ MAX + 1 ] ; void buildPrefix ( ) { bool prime [ MAX + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } prefix [ 0 ] = prefix [ 1 ] = 0 ; for ( int p = 2 ; p <= MAX ; p ++ ) { prefix [ p ] = prefix [ p - 1 ] ; if ( prime [ p ] ) prefix [ p ] += p ; } } int sumPrimeRange ( int L , int R ) { buildPrefix ( ) ; return prefix [ R ] - prefix [ L - 1 ] ; } int main ( ) { int L = 10 , R = 20 ; cout << sumPrimeRange ( L , R ) << endl ; return 0 ; }
Sum of the first N terms of the series 5 , 12 , 23 , 38. ... | C ++ program to find sum of first n terms ; Function to calculate the sum ; Driver code ; number of terms to be included in sum ; find the Sn
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateSum ( int n ) { return 2 * ( n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ) + n * ( n + 1 ) / 2 + 2 * ( n ) ; } int main ( ) { int n = 3 ; cout << " Sum ▁ = ▁ " << calculateSum ( n ) ; return 0 ; }
Program to find number of solutions in Quadratic Equation | C ++ Program to find the solutions of specified equations ; Method to check for solutions of equations ; If the expression is greater than 0 , then 2 solutions ; If the expression is equal 0 , then 2 solutions ; Else no solutions ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void checkSolution ( int a , int b , int c ) { if ( ( ( b * b ) - ( 4 * a * c ) ) > 0 ) cout << "2 ▁ solutions " ; else if ( ( ( b * b ) - ( 4 * a * c ) ) == 0 ) cout << "1 ▁ solution " ; else cout << " No ▁ solutions " ; } int main ( ) { int a = 2 , b = 5 , c = 2 ; checkSolution ( a , b , c ) ; return 0 ; }
Program to convert KiloBytes to Bytes and Bits | C ++ implementation of above program ; Function to calculates the bits ; calculates Bits 1 kilobytes ( s ) = 8192 bits ; Function to calculates the bytes ; calculates Bytes 1 KB = 1024 bytes ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long Bits ( int kilobytes ) { long Bits = 0 ; Bits = kilobytes * 8192 ; return Bits ; } long Bytes ( int kilobytes ) { long Bytes = 0 ; Bytes = kilobytes * 1024 ; return Bytes ; } int main ( ) { int kilobytes = 1 ; cout << kilobytes << " ▁ Kilobytes ▁ = ▁ " << Bytes ( kilobytes ) << " ▁ Bytes ▁ and ▁ " << Bits ( kilobytes ) << " ▁ Bits . " ; return 0 ; }
Program to find the Hidden Number | C ++ Program to find the hidden number ; Driver Code ; Getting the size of array ; Getting the array of size n ; Solution ; Finding sum of the array elements ; Dividing sum by size n ; Print x , if found
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int n = 3 ; int a [ ] = { 1 , 2 , 3 } ; int i = 0 ; long sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } long x = sum / n ; if ( x * n == sum ) cout << x << endl ; else cout << ( " - 1" ) << endl ; return 0 ; }
Find sum of the series ? 3 + ? 12 + ... ... ... upto N terms | C ++ implementation of above approach ; Function to find the sum ; Apply AP formula ; Driver code ; number of terms
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; double findSum ( ll n ) { return sqrt ( 3 ) * ( n * ( n + 1 ) / 2 ) ; } int main ( ) { ll n = 10 ; cout << findSum ( n ) << endl ; return 0 ; }
Find the sum of the series x ( x + y ) + x ^ 2 ( x ^ 2 + y ^ 2 ) + x ^ 3 ( x ^ 3 + y ^ 3 ) + ... + x ^ n ( x ^ n + y ^ n ) | CPP program to find the sum of series ; Function to return required sum ; sum of first series ; sum of second series ; Driver Code ; function call to print sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int x , int y , int n ) { int sum1 = ( pow ( x , 2 ) * ( pow ( x , 2 * n ) - 1 ) ) / ( pow ( x , 2 ) - 1 ) ; int sum2 = ( x * y * ( pow ( x , n ) * pow ( y , n ) - 1 ) ) / ( x * y - 1 ) ; return sum1 + sum2 ; } int main ( ) { int x = 2 , y = 2 , n = 2 ; cout << sum ( x , y , n ) ; return 0 ; }
Find any pair with given GCD and LCM | C ++ program to print any pair with a given gcd G and lcm L ; Function to print the pairs ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void printPair ( int g , int l ) { cout << g << " ▁ " << l ; } int main ( ) { int g = 3 , l = 12 ; printPair ( g , l ) ; return 0 ; }
Sum of first n terms of a given series 3 , 6 , 11 , ... . . | C ++ program to find sum of first n terms ; Function to calculate the sum ; starting number ; Common Ratio ; Common difference ; Driver code ; N th term to be find ; find the Sn
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateSum ( int n ) { int a1 = 1 , a2 = 2 ; int r = 2 ; int d = 1 ; return ( n ) * ( 2 * a1 + ( n - 1 ) * d ) / 2 + a2 * ( pow ( r , n ) - 1 ) / ( r - 1 ) ; } int main ( ) { int n = 5 ; cout << " Sum ▁ = ▁ " << calculateSum ( n ) ; return 0 ; }
Maximum of sum and product of digits until number is reduced to a single digit | CPP implementation of above approach ; Function to sum the digits until it becomes a single digit ; Function to product the digits until it becomes a single digit ; Loop to do sum while sum is not less than or equal to 9 ; Function to find the maximum among repeated sum and repeated product ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long repeatedSum ( long n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } long repeatedProduct ( long n ) { long prod = 1 ; while ( n > 0 prod > 9 ) { if ( n == 0 ) { n = prod ; prod = 1 ; } prod *= n % 10 ; n /= 10 ; } return prod ; } long maxSumProduct ( long N ) { if ( N < 10 ) return N ; return max ( repeatedSum ( N ) , repeatedProduct ( N ) ) ; } int main ( ) { long n = 631 ; cout << maxSumProduct ( n ) << endl ; return 0 ; }
Find maximum possible value of advertising | C ++ program for the above approach ; Function to find maximum possible advertising value ; To store advertising value at i - th minute ; Base Case ; If no advertisement is taken on ith minute ; If advertisement is taken on i - th minute ; Driver 's Code ; array [ ] [ 0 ] start time array [ ] [ 1 ] advertising value
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE int max_value ( int array [ ] [ 2 ] , int M , int K , int N ) { int time [ M ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { time [ array [ i ] [ 0 ] ] = array [ i ] [ 1 ] ; } int dp [ M ] [ 2 ] ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = time [ 0 ] ; for ( int i = 1 ; i < M ; i ++ ) { dp [ i ] [ 0 ] = max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; dp [ i ] [ 1 ] = time [ i ] ; if ( i - K >= 0 ) { dp [ i ] [ 1 ] += max ( dp [ i - K ] [ 0 ] , dp [ i - K ] [ 1 ] ) ; } } return max ( dp [ M - 1 ] [ 0 ] , dp [ M - 1 ] [ 1 ] ) ; } int main ( ) { int array [ ] [ 2 ] = { { 0 , 10 } , { 4 , 110 } , { 5 , 30 } } ; int N = 3 ; int K = 4 ; int M = 6 ; cout << max_value ( array , M , K , N ) ; }
Count numbers with exactly K non | C ++ program to Count the numbers having exactly K non - zero digits and sum of digits are odd and distinct . ; To store digits of N ; visited map ; DP Table ; Push all the digits of N into digits vector ; Function returns the count ; If desired number is formed whose sum is odd ; If it is not present in map , mark it as true and return 1 ; Sum is present in map already ; Desired result not found ; If that state is already calculated just return that state value ; Upper limit ; To store the count of desired numbers ; If k is non - zero , i ranges from 0 to j else [ 1 , j ] ; If current digit is 0 , decrement k and recurse sum is not changed as we are just adding 0 that makes no difference ; If i is non zero , then k remains unchanged and value is added to sum ; Memoize and return ; Driver code ; K is the number of exact non - zero elements to have in number ; break N into its digits ; We keep record of 0 s we need to place in the number
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > digits ; bool vis [ 170 ] = { false } ; int dp [ 19 ] [ 19 ] [ 2 ] [ 170 ] ; void ConvertIntoDigit ( int n ) { while ( n ) { int dig = n % 10 ; digits . push_back ( dig ) ; n /= 10 ; } reverse ( digits . begin ( ) , digits . end ( ) ) ; } int solve ( int idx , int k , int tight , int sum ) { if ( idx == digits . size ( ) && k == 0 && sum & 1 ) { if ( ! vis [ sum ] ) { vis [ sum ] = 1 ; return 1 ; } return 0 ; } if ( idx > digits . size ( ) ) { return 0 ; } if ( dp [ idx ] [ k ] [ tight ] [ sum ] ) { return dp [ idx ] [ k ] [ tight ] [ sum ] ; } int j ; if ( tight == 0 ) { j = digits [ idx ] ; } else { j = 9 ; } int cnt = 0 ; for ( int i = ( k ? 0 : 1 ) ; i <= j ; i ++ ) { int newtight = tight ; if ( i < j ) { newtight = 1 ; } if ( i == 0 ) cnt += solve ( idx + 1 , k - 1 , newtight , sum ) ; else cnt += solve ( idx + 1 , k , newtight , sum + i ) ; } return dp [ idx ] [ k ] [ tight ] [ sum ] = cnt ; } int main ( ) { int N , k ; N = 169 , k = 2 ; ConvertIntoDigit ( N ) ; k = digits . size ( ) - k ; cout << solve ( 0 , k , 0 , 0 ) ; }