text
stringlengths 1
4.49k
| code
stringlengths 1
5.46k
|
---|---|
Update all multiples of p as false , i . e . non - prime | for ( int i = p * p ; i <= 100000 ; i += p ) prime [ i ] = false ; } } } |
Returns number of subsequences of maximum length k and contains distinct primes | int distinctPrimeSubSeq ( int a [ ] , int n , int k ) { SieveOfEratosthenes ( ) ; |
Store the primes in the given array | vector < int > primes ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ a [ i ] ] ) primes . push_back ( a [ i ] ) ; } int l = primes . size ( ) ; |
Sort the primes | sort ( primes . begin ( ) , primes . end ( ) ) ; |
Store the frequencies of all the distinct primes | vector < int > b ; vector < int > dp ; int sum = 0 ; for ( int i = 0 ; i < l ; ) { int count = 1 , x = a [ i ] ; i ++ ; while ( i < l && a [ i ] == x ) { count ++ ; i ++ ; } |
Store the frequency of primes | b . push_back ( count ) ; dp . push_back ( count ) ; |
Store the sum of all frequencies | sum += count ; } |
Store the length of subsequence at every instant | int of_length = 2 ; int len = dp . size ( ) ; int ans = 0 ; while ( of_length <= k ) { |
Store the frequency | int freq = 0 ; |
Store the previous count of updated DP | int prev = 0 ; for ( int i = 0 ; i < ( len - 1 ) ; i ++ ) { freq += dp [ i ] ; int j = sum - freq ; |
Calculate total subsequences of current of_length | int subseq = b [ i ] * j ; |
Add the number of subsequences to the answer | ans += subseq ; |
Update the value in dp [ i ] | dp [ i ] = subseq ; |
Store the updated dp [ i ] | prev += dp [ i ] ; } len -- ; sum = prev ; of_length ++ ; } ans += ( l + 1 ) ; return ans ; } |
Driver Code | int main ( ) { int a [ ] = { 1 , 2 , 2 , 3 , 3 , 4 , 5 } ; int n = sizeof ( a ) / sizeof ( int ) ; int k = 3 ; cout << distinctPrimeSubSeq ( a , n , k ) ; return 0 ; } |
C ++ implementation to find the minimum prizes required such that adjacent smaller elements gets less number of prizes | #include <bits/stdc++.h> NEW_LINE using namespace std ; |
Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes | int findMinPrizes ( int arr [ ] , int n ) { int totalPrizes = 0 , j , x , y ; |
Loop to iterate over every elements of the array | for ( int i = 0 ; i < n ; i ++ ) { x = 1 ; j = i ; |
Loop to find the consecutive smaller elements at left | while ( j > 0 && arr [ j ] > arr [ j - 1 ] ) { x ++ ; j -- ; } j = i ; y = 1 ; |
Loop to find the consecutive smaller elements at right | while ( j < n - 1 && arr [ j ] > arr [ j + 1 ] ) { y ++ ; j ++ ; } totalPrizes += max ( { x , y } ) ; } cout << totalPrizes << endl ; return 0 ; } |
Driver Code | int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMinPrizes ( arr , n ) ; } |
C ++ implementation to count the number of ways to divide N in K groups such that each group has elements in range [ L , R ] | #include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1000000007 ; |
DP Table | int dp [ 1000 ] [ 1000 ] ; |
Function to count the number of ways to divide the number N in K groups such that each group has number of elements in range [ L , R ] | int calculate ( int pos , int left , int k , int L , int R ) { |
Base Case | if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } |
if N is divides completely into less than k groups | if ( left == 0 ) return 0 ; |
If the subproblem has been solved , use the value | if ( dp [ pos ] [ left ] != -1 ) return dp [ pos ] [ left ] ; int answer = 0 ; |
put all possible values greater equal to prev | for ( int i = L ; i <= R ; i ++ ) { if ( i > left ) break ; answer = ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod ; } return dp [ pos ] [ left ] = answer ; } |
Function to count the number of ways to divide the number N | int countWaystoDivide ( int n , int k , int L , int R ) { |
Initialize DP Table as - 1 | memset ( dp , -1 , sizeof ( dp ) ) ; return calculate ( 0 , n , k , L , R ) ; } |
Driver Code | int main ( ) { int N = 12 ; int K = 3 ; int L = 1 ; int R = 5 ; cout << countWaystoDivide ( N , K , L , R ) ; return 0 ; } |
C ++ program to represent N as the sum of minimum square numbers . | #include <bits/stdc++.h> NEW_LINE using namespace std ; |
Function for finding minimum square numbers | vector < int > minSqrNum ( int n ) { |
A [ i ] of array arr store minimum count of square number to get i | int arr [ n + 1 ] , k ; |
sqrNum [ i ] store last square number to get i | int sqrNum [ n + 1 ] ; vector < int > v ; |
Initialize | arr [ 0 ] = 0 ; sqrNum [ 0 ] = 0 ; |
Find minimum count of square number for all value 1 to n | for ( int i = 1 ; i <= n ; i ++ ) { |
In worst case it will be arr [ i - 1 ] + 1 we use all combination of a [ i - 1 ] and add 1 | arr [ i ] = arr [ i - 1 ] + 1 ; sqrNum [ i ] = 1 ; k = 1 ; |
Check for all square number less or equal to i | while ( k * k <= i ) { |
if it gives less count then update it | if ( arr [ i ] > arr [ i - k * k ] + 1 ) { arr [ i ] = arr [ i - k * k ] + 1 ; sqrNum [ i ] = k * k ; } k ++ ; } } |
Vector v stores optimum square number whose sum give N | while ( n > 0 ) { v . push_back ( sqrNum [ n ] ) ; n -= sqrNum [ n ] ; } return v ; } |
Driver code | int main ( ) { int n = 10 ; vector < int > v ; |
Calling function | v = minSqrNum ( n ) ; |
Printing vector | for ( auto i = v . begin ( ) ; i != v . end ( ) ; i ++ ) { cout << * i ; if ( i + 1 != v . end ( ) ) cout << " ▁ + ▁ " ; } return 0 ; } |