codeparrot/xlcost-text-to-code huggingface dataset:
Program to convert Centimeters to Pixels | C ++ program to convert centimeter to pixels ; Function to convert centimeters to pixels ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Conversion ( double centi ) { double pixels = ( 96 * centi ) / 2.54 ; cout << fixed << setprecision ( 2 ) << pixels ; } int main ( ) { double centi = 15 ; Conversion ( centi ) ; return 0 ; }
Kth array element after M replacements of array elements by XOR of adjacent pairs | C ++ program for the above approach ; Method that returns the corresponding output by taking the given inputs . ; If this condition is satisfied , value of M is invalid ; Check if index K is valid ; Loop to perform M operations ; Creating a temporary list ; Traversing the array ; Calculate XOR values of adjacent elements ; Adding this value to the temporary list ; Update the original array ; Getting value at index K ; Driver Code ; Number of elements ; Given array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xor_operations ( int N , int arr [ ] , int M , int K ) { if ( M < 0 or M > = N ) return -1 ; if ( K < 0 or K > = N - M ) return -1 ; for ( int p = 0 ; p < M ; p ++ ) { vector < int > temp ; for ( int i = 0 ; i < N ; i ++ ) { int value = arr [ i ] ^ arr [ i + 1 ] ; temp . push_back ( value ) ; arr [ i ] = temp [ i ] ; } } int ans = arr [ K ] ; return ans ; } int main ( ) { int N = 5 ; int arr [ ] = { 1 , 4 , 5 , 6 , 7 } ; int M = 1 , K = 2 ; cout << xor_operations ( N , arr , M , K ) ; return 0 ; }
Check if N can be divided into K consecutive elements with a sum equal to N | C ++ program for the above approach ; Function to find the K consecutive elements with a sum equal to N ; Iterate over [ 2 , INF ] ; Store the sum ; If the sum exceeds N then break the loop ; Common difference should be divisible by number of terms ; Print value of i & return ; Print " - 1" if not possible to break N ; Driver Code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canBreakN ( long long n ) { for ( long long i = 2 ; ; i ++ ) { long long m = i * ( i + 1 ) / 2 ; if ( m > n ) break ; long long k = n - m ; if ( k % i ) continue ; cout << i << endl ; return ; } cout << " - 1" ; } int main ( ) { long long N = 12 ; canBreakN ( N ) ; return 0 ; }
Coprime divisors of a number | C ++ program to find two coprime divisors of a given number such that both are greater than 1 ; Function which finds the required pair of divisors of N ; We iterate upto sqrt ( N ) as we can find all the divisors of N in this time ; If x is a divisor of N keep dividing as long as possible ; We have found a required pair ; No such pair of divisors of N was found , hence print - 1 ; Driver Code ; Sample example 1 ; Sample example 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findCoprimePair ( int N ) { for ( int x = 2 ; x <= sqrt ( N ) ; x ++ ) { if ( N % x == 0 ) { while ( N % x == 0 ) { N /= x ; } if ( N > 1 ) { cout << x << " ▁ " << N << endl ; return ; } } } cout << -1 << endl ; } int main ( ) { int N = 45 ; findCoprimePair ( N ) ; N = 25 ; findCoprimePair ( N ) ; return 0 ; }
Wasteful Numbers | C ++ program for the above approach ; Array to store all prime less than and equal to MAX . ; Function for Sieve of Sundaram ; Boolean Array ; Mark all numbers which do not generate prime number by 2 * i + 1 ; Since 2 is a prime number ; Print remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function that returns true if n is a Wasteful number ; Count digits in original number ; Count all digits in prime factors of N pDigit is going to hold this value . ; Count powers of p in n ; If primes [ i ] is a prime factor , ; Count the power of prime factors ; Add its digits to pDigit ; Add digits of power of prime factors to pDigit . ; If n != 1 then one prime factor still to be summed up ; If digits in prime factors is more than digits in original number then return true . Else return false . ; Function to print Wasteful Number before N ; Iterate till N and check if i is wastefull or not ; Driver code ; Precompute prime numbers upto 10 ^ 6 ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10000 ; vector < int > primes ; void sieveSundaram ( ) { bool marked [ MAX / 2 + 1 ] = { 0 } ; for ( int i = 1 ; i <= ( sqrt ( MAX ) - 1 ) / 2 ; i ++ ) { for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) { marked [ j ] = true ; } } primes . push_back ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . push_back ( 2 * i + 1 ) ; } bool isWasteful ( int n ) { if ( n == 1 ) return false ; int original_no = n ; int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits ++ ; original_no = original_no / 10 ; } int pDigit = 0 , count_exp = 0 , p ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { p = primes [ i ] ; n = n / p ; count_exp ++ ; } while ( p > 0 ) { pDigit ++ ; p = p / 10 ; } while ( count_exp > 1 ) { pDigit ++ ; count_exp = count_exp / 10 ; } } if ( n != 1 ) { while ( n > 0 ) { pDigit ++ ; n = n / 10 ; } } return ( pDigit > sumDigits ) ; } void Solve ( int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( isWasteful ( i ) ) { cout << i << " ▁ " ; } } } int main ( ) { sieveSundaram ( ) ; int N = 10 ; Solve ( N ) ; return 0 ; }
Hexanacci Numbers | C ++ simple recursive program to print Nth Hexanacci numbers . ; Function to print the Nth Hexanacci number ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int printhexaRec ( int n ) { if ( n == 0 n == 1 n == 2 n == 3 n == 4 n == 5 ) return 0 ; else if ( n == 6 ) return 1 ; else return ( printhexaRec ( n - 1 ) + printhexaRec ( n - 2 ) + printhexaRec ( n - 3 ) + printhexaRec ( n - 4 ) + printhexaRec ( n - 5 ) + printhexaRec ( n - 6 ) ) ; } int printhexa ( int n ) { cout << printhexaRec ( n ) << endl ; } int main ( ) { privatenthexa ( n ) ; }
Hexanacci Numbers | C ++ implementation to print Nth term of hexanacci numbers . ; Function to print the Nth term of the Hexanacci number ; Initialize first five numbers to base cases ; Declare a current variable ; Loop to add previous five numbers for each number starting from 5 and then assign first , second , third , fourth fifth to second , third , fourth , fifth and curr to sixth respectively ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printhexa ( int n ) { if ( n < 0 ) return ; int first = 0 ; int second = 0 ; int third = 0 ; int fourth = 0 ; int fifth = 0 ; int sixth = 1 ; int curr = 0 ; if ( n < 6 ) cout << first << endl ; else if ( n == 6 ) cout << sixth << endl ; else { for ( int i = 6 ; i < n ; i ++ ) { curr = first + second + third + fourth + fifth + sixth ; first = second ; second = third ; third = fourth ; fourth = fifth ; fifth = sixth ; sixth = curr ; } } cout << curr << endl ; } int main ( ) { int n = 11 ; printhexa ( n ) ; return 0 ; }
Find the smallest number whose sum of digits is N | C ++ program to find the smallest number whose sum of digits is also N ; Function to find the smallest number whose sum of digits is also N ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void smallestNumber ( int N ) { cout << ( N % 9 + 1 ) * pow ( 10 , ( N / 9 ) ) - 1 ; } int main ( ) { int N = 10 ; smallestNumber ( N ) ; return 0 ; }
Compositorial of a number | C ++ program to find compositorial of composite numbers ; Function to check if a number is composite . ; Corner cases ; This is checked so that we can skip the middle five numbers in the below loop ; This function stores all composite numbers less than N ; Function to calculate the compositorial of n ; Multiply first n composite number ; Driver code ; Vector to store all the composite less than N
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > compo ; bool isComposite ( int n ) { if ( n <= 3 ) return false ; if ( n % 2 == 0 or n % 3 == 0 ) return true ; int i = 5 ; while ( i * i <= n ) { if ( n % i == 0 or n % ( i + 2 ) == 0 ) return true ; i = i + 6 ; } return false ; } void Compositorial_list ( int n ) { int l = 0 ; for ( int i = 4 ; i < 1000000 ; i ++ ) { if ( l < n ) { if ( isComposite ( i ) ) { compo . push_back ( i ) ; l += 1 ; } } } } int calculateCompositorial ( int n ) { int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) result = result * compo [ i ] ; return result ; } int main ( ) { int n = 5 ; Compositorial_list ( n ) ; cout << ( calculateCompositorial ( n ) ) ; return 0 ; }
Distinct powers of a number N such that the sum is equal to K | C ++ implementation to find out the powers of N that add upto K ; Initializing the PowerArray with all 0 's ; Function to find the powers of N that add up to K ; Initializing the counter ; Executing the while loop until K is greater than 0 ; If K % N == 1 , then the power array is incremented by 1 ; Checking if any power is occurred more than once ; For any other value , the sum of powers cannot be added up to K ; Printing the powers of N that sum up to K ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int b [ 50 ] = { 0 } ; int PowerArray ( int n , int k ) { int count = 0 ; while ( k ) { if ( k % n == 0 ) { k /= n ; count ++ ; } else if ( k % n == 1 ) { k -= 1 ; b [ count ] ++ ; if ( b [ count ] > 1 ) { cout << -1 ; return 0 ; } } else { cout << -1 ; return 0 ; } } for ( int i = 0 ; i < 50 ; i ++ ) { if ( b [ i ] ) { cout << i << " , ▁ " ; } } } int main ( ) { int N = 3 ; int K = 40 ; PowerArray ( N , K ) ; return 0 ; }
Program to find value of 1 ^ k + 2 ^ k + 3 ^ k + ... + n ^ k | C ++ Program to find the value 1 ^ K + 2 ^ K + 3 ^ K + . . + N ^ K ; Function to find value of 1 ^ K + 2 ^ K + 3 ^ K + . . + N ^ K ; Initialise sum to 0 ; Find the value of pow ( i , 4 ) and then add it to the sum ; Return the sum ; Drivers Code ; Function call to find the sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int N , int k ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += pow ( i , k ) ; } return sum ; } int main ( ) { int N = 8 , k = 4 ; cout << findSum ( N , k ) << endl ; return 0 ; }
Count of indices in an array that satisfy the given condition | C ++ implementation of the approach ; Function to return the count of indices that satisfy the given condition ; To store the result ; To store the current maximum Initialized to 0 since there are only positive elements in the array ; i is a valid index ; Update the maximum so far ; Increment the counter ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countIndices ( int arr [ ] , int n ) { int cnt = 0 ; int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) { max = arr [ i ] ; cnt ++ ; } } return cnt ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << countIndices ( arr , n ) ; return 0 ; }
Maximum frequency of a remainder modulo 2 i | C ++ implementation of the approach ; Binary representation of the digits ; Function to return the maximum frequency of s modulo with a power of 2 ; Store the binary representation ; Convert the octal to binary ; Remove the LSB ; If there is 1 in the binary representation ; Find the number of zeroes in between two 1 's in the binary representation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const string bin [ ] = { "000" , "001" , "010" , "011" , "100" , "101" , "110" , "111" } ; int maxFreq ( string s ) { string binary = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { binary += bin [ s [ i ] - '0' ] ; } binary = binary . substr ( 0 , binary . length ( ) - 1 ) ; int count = 1 , prev = -1 , i , j = 0 ; for ( i = binary . length ( ) - 1 ; i >= 0 ; i -- , j ++ ) if ( binary [ i ] == '1' ) { count = max ( count , j - prev ) ; prev = j ; } return count ; } int main ( ) { string octal = "13" ; cout << maxFreq ( octal ) ; return 0 ; }
Find all the prime numbers of given number of digits | C ++ implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print all the prime numbers with d digits ; Range to check integers ; For every integer in the range ; If the current integer is prime ; Driver code ; Generate primes
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sz = 1e5 ; bool isPrime [ sz + 1 ] ; void sieve ( ) { memset ( isPrime , true , sizeof ( isPrime ) ) ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } void findPrimesD ( int d ) { int left = pow ( 10 , d - 1 ) ; int right = pow ( 10 , d ) - 1 ; for ( int i = left ; i <= right ; i ++ ) { if ( isPrime [ i ] ) { cout << i << " ▁ " ; } } } int main ( ) { sieve ( ) ; int d = 1 ; findPrimesD ( d ) ; return 0 ; }
Find the number of cells in the table contains X | C ++ program to find number of cells in the table contains X ; Function to find number of cells in the table contains X ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Cells ( int n , int x ) { if ( n <= 0 x <= 0 x > n * n ) return 0 ; int i = 0 , count = 0 ; while ( ++ i * i < x ) if ( x % i == 0 && x <= n * i ) count += 2 ; return i * i == x ? count + 1 : count ; } int main ( ) { int n = 6 , x = 12 ; cout << ( Cells ( n , x ) ) ; return 0 ; }
Find the maximum possible value of the minimum value of modified array | CPP program to find the maximum possible value of the minimum value of modified array ; Function to find the maximum possible value of the minimum value of the modified array ; To store minimum value of array ; To store sum of elements of array ; Solution is not possible ; zero is the possible value ; minimum possible value ; maximum possible value ; to store a required answer ; Binary Search ; If mid is possible then try to increase required answer ; If mid is not possible then decrease required answer ; Return required answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOfMin ( int a [ ] , int n , int S ) { int mi = INT_MAX ; int s1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s1 += a [ i ] ; mi = min ( a [ i ] , mi ) ; } if ( s1 < S ) return -1 ; if ( s1 == S ) return 0 ; int low = 0 ; int high = mi ; int ans ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( s1 - ( mid * n ) >= S ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } int main ( ) { int a [ ] = { 10 , 10 , 10 , 10 , 10 } ; int S = 10 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maxOfMin ( a , n , S ) ; return 0 ; }
Program to print ' N ' alphabet using the number pattern from 1 to n | C ++ implementation of the approach ; Function to print the desired Alphabet N Pattern ; Declaring the values of Right , Left and Diagonal values ; Main Loop for the rows ; For the left Values ; Spaces for the diagonals ; Condition for the diagonals ; Spaces for the Right Values ; For the right values ; Driver Code ; Size of the Pattern ; Calling the function to print the desired Pattern
#include <iostream> NEW_LINE using namespace std ; void Alphabet_N_Pattern ( int N ) { int index , side_index , size ; int Right = 1 , Left = 1 , Diagonal = 2 ; for ( index = 0 ; index < N ; index ++ ) { cout << Left ++ ; for ( side_index = 0 ; side_index < 2 * ( index ) ; side_index ++ ) cout << " ▁ " ; if ( index != 0 && index != N - 1 ) cout << Diagonal ++ ; else cout << " ▁ " ; for ( side_index = 0 ; side_index < 2 * ( N - index - 1 ) ; side_index ++ ) cout << " ▁ " ; cout << Right ++ ; cout << endl ; } } int main ( int argc , char * * argv ) { int Size = 6 ; Alphabet_N_Pattern ( Size ) ; }
Check if the sum of digits of a number N divides it | C ++ program to check if sum of digits of a number divides it ; Function to check if sum of digits of a number divides it ; Calculate sum of all of digits of N ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int isSumDivides ( int N ) { int temp = N ; int sum = 0 ; while ( temp ) { sum += temp % 10 ; temp /= 10 ; } if ( N % sum == 0 ) return 1 ; else return 0 ; } int main ( ) { int N = 12 ; if ( isSumDivides ( N ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Sum of numbers from 1 to N which are divisible by 3 or 4 | C ++ program to find sum of numbers from 1 to N which are divisible by 3 or 4 ; Function to calculate the sum of numbers divisible by 3 or 4 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int N ) { int S1 , S2 , S3 ; S1 = ( ( N / 3 ) ) * ( 2 * 3 + ( N / 3 - 1 ) * 3 ) / 2 ; S2 = ( ( N / 4 ) ) * ( 2 * 4 + ( N / 4 - 1 ) * 4 ) / 2 ; S3 = ( ( N / 12 ) ) * ( 2 * 12 + ( N / 12 - 1 ) * 12 ) / 2 ; return S1 + S2 - S3 ; } int main ( ) { int N = 20 ; cout << sum ( 12 ) ; return 0 ; }
Next greater number than N with exactly one bit different in binary representation of N | CPP program to find next greater number than N with exactly one bit different in binary representation of N ; Function to find next greater number than N with exactly one bit different in binary representation of N ; It is guaranteed that there is a bit zero in the number ; If the shifted bit is zero then break ; increase the bit shift ; increase the power of 2 ; set the lowest bit of the number ; Driver code ; display the next number
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long nextGreater ( long long N ) { long long power_of_2 = 1 , shift_count = 0 ; while ( true ) { if ( ( ( N >> shift_count ) & 1 ) % 2 == 0 ) break ; shift_count ++ ; power_of_2 = power_of_2 * 2 ; } return ( N + power_of_2 ) ; } int main ( ) { long long N = 11 ; cout << " The ▁ next ▁ number ▁ is ▁ = ▁ " << nextGreater ( N ) ; return 0 ; }
Count number of ways to cover a distance | Set 2 | C ++ implementation of the approach ; Function to return the count of the total number of ways to cover the distance with 1 , 2 and 3 steps ; Base conditions ; To store the last three stages ; Find the numbers of steps required to reach the distance i ; Return the required answer ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countWays ( int n ) { if ( n == 0 ) return 1 ; if ( n <= 2 ) return n ; int f0 = 1 , f1 = 1 , f2 = 2 , ans ; for ( int i = 3 ; i <= n ; i ++ ) { ans = f0 + f1 + f2 ; f0 = f1 ; f1 = f2 ; f2 = ans ; } return ans ; } int main ( ) { int n = 4 ; cout << countWays ( n ) ; return 0 ; }
Maximize sum by choosing elements from different section of a matrix | C ++ program for the above approach ; Function to find the maximum value ; Dp table ; Fill the dp in bottom up manner ; Maximum of the three sections ; Maximum of the first section ; Maximum of the second section ; Maximum of the third section ; If we choose element from section 1 , we cannot have selection from same section in adjacent rows ; Print the maximum sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int n = 6 , m = 6 ; void maxSum ( long arr [ n ] [ m ] ) { long dp [ n + 1 ] [ 3 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { long m1 = 0 , m2 = 0 , m3 = 0 ; for ( int j = 0 ; j < m ; j ++ ) { if ( ( j / ( m / 3 ) ) == 0 ) { m1 = max ( m1 , arr [ i ] [ j ] ) ; } else if ( ( j / ( m / 3 ) ) == 1 ) { m2 = max ( m2 , arr [ i ] [ j ] ) ; } else if ( ( j / ( m / 3 ) ) == 2 ) { m3 = max ( m3 , arr [ i ] [ j ] ) ; } } dp [ i + 1 ] [ 0 ] = max ( dp [ i ] [ 1 ] , dp [ i ] [ 2 ] ) + m1 ; dp [ i + 1 ] [ 1 ] = max ( dp [ i ] [ 0 ] , dp [ i ] [ 2 ] ) + m2 ; dp [ i + 1 ] [ 2 ] = max ( dp [ i ] [ 1 ] , dp [ i ] [ 0 ] ) + m3 ; } cout << max ( max ( dp [ n ] [ 0 ] , dp [ n ] [ 1 ] ) , dp [ n ] [ 2 ] ) << ' ' ; } int main ( ) { long arr [ n ] [ m ] = { { 1 , 3 , 5 , 2 , 4 , 6 } , { 6 , 4 , 5 , 1 , 3 , 2 } , { 1 , 3 , 5 , 2 , 4 , 6 } , { 6 , 4 , 5 , 1 , 3 , 2 } , { 6 , 4 , 5 , 1 , 3 , 2 } , { 1 , 3 , 5 , 2 , 4 , 6 } } ; maxSum ( arr ) ; return 0 ; }
Total number of odd length palindrome sub | C ++ implementation of the approach ; Function to find the total palindromic odd length sub - sequences ; dp array to store the number of palindromic subsequences for 0 to i - 1 and j + 1 to n - 1 ; We will start with the largest distance between i and j ; For each len , we fix our i ; For this i we will find our j ; Base cases ; If the characters are equal then look for out of bound index ; We have only 1 way that is to just pick these characters ; If the characters are not equal ; Subtract it as we have counted it twice ; We have just 1 palindrome sequence of length 1 ; Else total ways would be sum of dp [ i - 1 ] [ i + 1 ] , that is number of palindrome sub - sequences from 1 to i - 1 + number of palindrome sub - sequences from i + 1 to n - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( string & s ) { int n = s . length ( ) ; int dp [ n ] [ n ] ; memset ( dp , 0 , sizeof dp ) ; for ( int len = n - 1 ; len >= 0 ; -- len ) { for ( int i = 0 ; i + len < n ; ++ i ) { int j = i + len ; if ( i == 0 and j == n - 1 ) { if ( s [ i ] == s [ j ] ) dp [ i ] [ j ] = 2 ; else if ( s [ i ] != s [ j ] ) dp [ i ] [ j ] = 1 ; } else { if ( s [ i ] == s [ j ] ) { if ( i - 1 >= 0 ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] ; } if ( j + 1 <= n - 1 ) { dp [ i ] [ j ] += dp [ i ] [ j + 1 ] ; } if ( i - 1 < 0 or j + 1 >= n ) { dp [ i ] [ j ] += 1 ; } } else if ( s [ i ] != s [ j ] ) { if ( i - 1 >= 0 ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] ; } if ( j + 1 <= n - 1 ) { dp [ i ] [ j ] += dp [ i ] [ j + 1 ] ; } if ( i - 1 >= 0 and j + 1 <= n - 1 ) { dp [ i ] [ j ] -= dp [ i - 1 ] [ j + 1 ] ; } } } } } vector < int > ways ; for ( int i = 0 ; i < n ; ++ i ) { if ( i == 0 or i == n - 1 ) { ways . push_back ( 1 ) ; } else { int total = dp [ i - 1 ] [ i + 1 ] ; ways . push_back ( total ) ; } } for ( int i = 0 ; i < ways . size ( ) ; ++ i ) { cout << ways [ i ] << " ▁ " ; } } int main ( ) { string s = " xyxyx " ; solve ( s ) ; return 0 ; }
Find the number of Chicks in a Zoo at Nth day | C ++ implementation of the approach ; Function to return the number of chicks on the nth day ; Size of dp [ ] has to be at least 6 ( 1 - based indexing ) ; Every day current population will be three times of the previous day ; Manually calculated value ; From 8 th day onwards ; Chick population decreases by 2 / 3 everyday . For 8 th day on [ i - 6 ] i . e 2 nd day population was 3 and so 2 new born die on the 6 th day and so on for the upcoming days ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll getChicks ( int n ) { int size = max ( n , 7 ) ; ll dp [ size ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= 6 ; i ++ ) { dp [ i ] = dp [ i - 1 ] * 3 ; } dp [ 7 ] = 726 ; for ( int i = 8 ; i <= n ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] - ( 2 * dp [ i - 6 ] / 3 ) ) * 3 ; } return dp [ n ] ; } int main ( ) { int n = 3 ; cout << getChicks ( n ) ; return 0 ; }
Find the number of Chicks in a Zoo at Nth day | C ++ implementation of the approach ; Function to return the number of chicks on the nth day ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll getChicks ( int n ) { ll chicks = ( ll ) pow ( 3 , n - 1 ) ; return chicks ; } int main ( ) { int n = 3 ; cout << getChicks ( n ) ; return 0 ; }
Find minimum steps required to reach the end of a matrix | Set 2 | C ++ implementation of the approach ; 2d array to store states of dp ; Array to determine whether a state has been solved before ; Function to return the minimum steps required ; Base cases ; If a state has been solved before it won 't be evaluated again ; Recurrence relation ; Driver code
#include <bits/stdc++.h> NEW_LINE #define n  3 NEW_LINE using namespace std ; int dp [ n ] [ n ] ; int v [ n ] [ n ] ; int minSteps ( int i , int j , int arr [ ] [ n ] ) { if ( i == n - 1 and j == n - 1 ) return 0 ; if ( i > n - 1 j > n - 1 ) return 9999999 ; if ( v [ i ] [ j ] ) return dp [ i ] [ j ] ; v [ i ] [ j ] = 1 ; dp [ i ] [ j ] = 9999999 ; for ( int k = max ( 0 , arr [ i ] [ j ] + j - n + 1 ) ; k <= min ( n - i - 1 , arr [ i ] [ j ] ) ; k ++ ) { dp [ i ] [ j ] = min ( dp [ i ] [ j ] , minSteps ( i + k , j + arr [ i ] [ j ] - k , arr ) ) ; } dp [ i ] [ j ] ++ ; return dp [ i ] [ j ] ; } int main ( ) { int arr [ n ] [ n ] = { { 4 , 1 , 2 } , { 1 , 1 , 1 } , { 2 , 1 , 1 } } ; int ans = minSteps ( 0 , 0 , arr ) ; if ( ans >= 9999999 ) cout << -1 ; else cout << ans ; return 0 ; }