codeparrot/xlcost-text-to-code huggingface dataset:
Smallest length string with repeated replacement of two distinct adjacent | C ++ program to find smallest possible length of a string of only three characters ; Program to find length of reduced string in a string made of three characters . ; To store results of subproblems ; A memoized function find result recursively . a , b and c are counts of ' a ' s , ' b ' s and ' c ' s in str ; If this subproblem is already evaluated ; If there is only one type of character ; If only two types of characters are present ; If all types of characters are present . Try combining all pairs . ; Returns smallest possible length with given operation allowed . ; Counting occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; Initialize DP [ ] [ ] entries as - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_LEN  110 NEW_LINE int DP [ MAX_LEN ] [ MAX_LEN ] [ MAX_LEN ] ; int length ( int a , int b , int c ) { if ( DP [ a ] [ b ] != -1 ) return DP [ a ] [ b ] ; if ( a == 0 && b == 0 ) return ( DP [ a ] [ b ] = c ) ; if ( a == 0 && c == 0 ) return ( DP [ a ] [ b ] = b ) ; if ( b == 0 && c == 0 ) return ( DP [ a ] [ b ] = a ) ; if ( a == 0 ) return ( DP [ a ] [ b ] = length ( a + 1 , b - 1 , c - 1 ) ) ; if ( b == 0 ) return ( DP [ a ] [ b ] = length ( a - 1 , b + 1 , c - 1 ) ) ; if ( c == 0 ) return ( DP [ a ] [ b ] = length ( a - 1 , b - 1 , c + 1 ) ) ; return ( DP [ a ] [ b ] = min ( length ( a - 1 , b - 1 , c + 1 ) , min ( length ( a - 1 , b + 1 , c - 1 ) , length ( a + 1 , b - 1 , c - 1 ) ) ) ) ; } int stringReduction ( string str ) { int n = str . length ( ) ; int count [ 3 ] = { 0 } ; for ( int i = 0 ; i < n ; ++ i ) count [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i <= count [ 0 ] ; ++ i ) for ( int j = 0 ; j < count [ 1 ] ; ++ j ) for ( int k = 0 ; k < count [ 2 ] ; ++ k ) DP [ i ] [ j ] [ k ] = -1 ; return length ( count [ 0 ] , count [ 1 ] , count [ 2 ] ) ; } int main ( ) { string str = " abcbbaacb " ; cout << stringReduction ( str ) ; return 0 ; }
Equilibrium index of an array | C ++ program to find equilibrium index of an array ; function to find the equilibrium index ; Check for indexes one by one until an equilibrium index is found ; get left sum ; get right sum ; if leftsum and rightsum are same , then we are done ; return - 1 if no equilibrium index is found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int equilibrium ( int arr [ ] , int n ) { int i , j ; int leftsum , rightsum ; for ( i = 0 ; i < n ; ++ i ) { leftsum = 0 ; rightsum = 0 ; for ( j = 0 ; j < i ; j ++ ) leftsum += arr [ j ] ; for ( j = i + 1 ; j < n ; j ++ ) rightsum += arr [ j ] ; if ( leftsum == rightsum ) return i ; } return -1 ; } int main ( ) { int arr [ ] = { -7 , 1 , 5 , 2 , -4 , 3 , 0 } ; int arr_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << equilibrium ( arr , arr_size ) ; return 0 ; }
Find number of endless points | C ++ program to find count of endless points ; Returns count of endless points ; Fills column matrix . For every column , start from every last row and fill every entry as blockage after a 0 is found . ; flag which will be zero once we get a '0' and it will be 1 otherwise ; encountered a '0' , set the isEndless variable to false ; Similarly , fill row matrix ; Calculate total count of endless points ; If there is NO blockage in row or column after this point , increment result . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; int countEndless ( bool input [ ] [ MAX ] , int n ) { bool row [ n ] [ n ] , col [ n ] [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { bool isEndless = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( input [ i ] [ j ] == 0 ) isEndless = 0 ; col [ i ] [ j ] = isEndless ; } } for ( int i = 0 ; i < n ; i ++ ) { bool isEndless = 1 ; for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( input [ i ] [ j ] == 0 ) isEndless = 0 ; row [ i ] [ j ] = isEndless ; } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) if ( row [ i ] [ j ] && col [ i ] [ j ] ) ans ++ ; return ans ; } int main ( ) { bool input [ ] [ MAX ] = { { 1 , 0 , 1 , 1 } , { 0 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 } , { 0 , 1 , 1 , 0 } } ; int n = 4 ; cout << countEndless ( input , n ) ; return 0 ; }
Equilibrium index of an array | C ++ program to find equilibrium index of an array ; function to find the equilibrium index ; initialize sum of whole array ; initialize leftsum ; Find sum of the whole array ; sum is now right sum for index i ; If no equilibrium index found , then return 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int equilibrium ( int arr [ ] , int n ) { int sum = 0 ; int leftsum = 0 ; for ( int i = 0 ; i < n ; ++ i ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) { sum -= arr [ i ] ; if ( leftsum == sum ) return i ; leftsum += arr [ i ] ; } return -1 ; } int main ( ) { int arr [ ] = { -7 , 1 , 5 , 2 , -4 , 3 , 0 } ; int arr_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " First ▁ equilibrium ▁ index ▁ is ▁ " << equilibrium ( arr , arr_size ) ; return 0 ; }
Sum of all substrings of a string representing a number | Set 1 | C ++ program to print sum of all substring of a number represented as a string ; Utility method to convert character digit to integer digit ; Returns sum of all substring of num ; allocate memory equal to length of string ; initialize first value with first digit ; loop over all digits of string ; update each sumofdigit from previous value ; add current value to the result ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int toDigit ( char ch ) { return ( ch - '0' ) ; } int sumOfSubstrings ( string num ) { int n = num . length ( ) ; int sumofdigit [ n ] ; sumofdigit [ 0 ] = toDigit ( num [ 0 ] ) ; int res = sumofdigit [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int numi = toDigit ( num [ i ] ) ; sumofdigit [ i ] = ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ] ; res += sumofdigit [ i ] ; } return res ; } int main ( ) { string num = "1234" ; cout << sumOfSubstrings ( num ) << endl ; return 0 ; }
Sum of all substrings of a string representing a number | Set 1 | C ++ program to print sum of all substring of a number represented as a string ; Utility method to convert character digit to integer digit ; Returns sum of all substring of num ; storing prev value ; substrings sum upto current index loop over all digits of string ; update each sumofdigit from previous value ; add current value to the result ; prev = current ; update previous ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int toDigit ( char ch ) { return ( ch - '0' ) ; } int sumOfSubstrings ( string num ) { int n = num . length ( ) ; int prev = toDigit ( num [ 0 ] ) ; int res = prev ; int current = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int numi = toDigit ( num [ i ] ) ; current = ( i + 1 ) * numi + 10 * prev ; res += current ; } return res ; } int main ( ) { string num = "1234" ; cout << sumOfSubstrings ( num ) << endl ; return 0 ; }
Equilibrium index of an array | C ++ program to find equilibrium index of an array ; Taking the prefixsum from front end array ; Taking the prefixsum from back end of array ; Checking if forward prefix sum is equal to rev prefix sum ; If You want all the points of equilibrium create vector and push all equilibrium points in it and return the vector ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int equilibrium ( int a [ ] , int n ) { if ( n == 1 ) return ( 0 ) ; int forward [ n ] = { 0 } ; int rev [ n ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( i ) { forward [ i ] = forward [ i - 1 ] + a [ i ] ; } else { forward [ i ] = a [ i ] ; } } for ( int i = n - 1 ; i > 0 ; i -- ) { if ( i <= n - 2 ) { rev [ i ] = rev [ i + 1 ] + a [ i ] ; } else { rev [ i ] = a [ i ] ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( forward [ i ] == rev [ i ] ) { return i ; } } return -1 ; } int main ( ) { int arr [ ] = { -7 , 1 , 5 , 2 , -4 , 3 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " First ▁ Point ▁ of ▁ equilibrium ▁ is ▁ at ▁ index ▁ " << equilibrium ( arr , n ) << " STRNEWLINE " ; return 0 ; }
Leaders in an array | C ++ Function to print leaders in an array ; the loop didn 't break ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void printLeaders ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { int j ; for ( j = i + 1 ; j < size ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) break ; } if ( j == size ) cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 16 , 17 , 4 , 3 , 5 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printLeaders ( arr , n ) ; return 0 ; }
Minimize cost to reach end of an N | C ++ program for the above approach ; For priority_queue ; Function to calculate the minimum cost required to reach the end of Line ; Checks if possible to reach end or not ; Stores the stations and respective rate of fuel ; Stores the station index and cost of fuel and litres of petrol which is being fueled ; Iterate through the entire line ; Check if there is a station at current index ; Remove all the stations where fuel cannot be pumped ; If there is no station left to fill fuel in tank , it is not possible to reach end ; Stores the best station visited so far ; Pump fuel from the best station ; Update the count of litres taken from that station ; Update the bunk in queue ; Print the cost ; Driven Program ; Given value of N , K & M ; Given arrays ; Function call to calculate minimum cost to reach end of the line
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Compare { bool operator() ( array < int , 3 > a , array < int , 3 > b ) { return a [ 1 ] > b [ 1 ] ; } } ; void minCost ( int N , int K , int M , int a [ ] , int b [ ] ) { bool flag = true ; unordered_map < int , int > map ; for ( int i = 0 ; i < M ; i ++ ) { map [ a [ i ] ] = b [ i ] ; if ( i == M - 1 && K < N - a [ i ] ) { flag = false ; break ; } else if ( i < M - 1 && K < a [ i + 1 ] - a [ i ] ) { flag = false ; break ; } } if ( ! flag ) { cout << -1 ; return ; } priority_queue < array < int , 3 > , vector < array < int , 3 > > , Compare > pq ; int cost = 0 ; flag = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( map . find ( i ) != map . end ( ) ) { array < int , 3 > arr = { i , map [ i ] , 0 } ; pq . push ( arr ) ; } while ( pq . size ( ) > 0 && pq . top ( ) [ 2 ] == K ) pq . pop ( ) ; if ( pq . size ( ) == 0 ) { flag = true ; break ; } array < int , 3 > best_bunk = pq . top ( ) ; pq . pop ( ) ; cost += best_bunk [ 1 ] ; best_bunk [ 2 ] ++ ; pq . push ( best_bunk ) ; } if ( flag ) { cout << -1 << " STRNEWLINE " ; return ; } cout << cost << " STRNEWLINE " ; } int main ( ) { int N = 10 , K = 3 , M = 4 ; int a [ ] = { 0 , 1 , 4 , 6 } ; int b [ ] = { 5 , 2 , 2 , 4 } ; minCost ( N , K , M , a , b ) ; return 0 ; }
Leaders in an array |  ; C ++ Function to print leaders in an array ; Rightmost element is always leader ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void printLeaders ( int arr [ ] , int size ) { int max_from_right = arr [ size - 1 ] ; cout << max_from_right << " ▁ " ; for ( int i = size - 2 ; i >= 0 ; i -- ) { if ( max_from_right < arr [ i ] ) { max_from_right = arr [ i ] ; cout << max_from_right << " ▁ " ; } } } int main ( ) { int arr [ ] = { 16 , 17 , 4 , 3 , 5 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printLeaders ( arr , n ) ; return 0 ; }
Unbounded Knapsack ( Repetition of items allowed ) | C ++ program to find maximum achievable value with a knapsack of weight W and multiple instances allowed . ; Returns the maximum value with knapsack of W capacity ; dp [ i ] is going to store maximum value with knapsack capacity i . ; Fill dp [ ] using above recursive formula ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int unboundedKnapsack ( int W , int n , int val [ ] , int wt [ ] ) { int dp [ W + 1 ] ; memset ( dp , 0 , sizeof dp ) ; for ( int i = 0 ; i <= W ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( wt [ j ] <= i ) dp [ i ] = max ( dp [ i ] , dp [ i - wt [ j ] ] + val [ j ] ) ; return dp [ W ] ; } int main ( ) { int W = 100 ; int val [ ] = { 10 , 30 , 20 } ; int wt [ ] = { 5 , 10 , 15 } ; int n = sizeof ( val ) / sizeof ( val [ 0 ] ) ; cout << unboundedKnapsack ( W , n , val , wt ) ; return 0 ; }
Ceiling in a sorted array | C ++ implementation of above approach ; Function to get index of ceiling of x in arr [ low . . high ] ; If x is smaller than or equal to first element , then return the first element ; Otherwise , linearly search for ceil value ; if x lies between arr [ i ] and arr [ i + 1 ] including arr [ i + 1 ] , then return arr [ i + 1 ] ; If we reach here then x is greater than the last element of the array , return - 1 in this case ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ceilSearch ( int arr [ ] , int low , int high , int x ) { int i ; if ( x <= arr [ low ] ) return low ; for ( i = low ; i < high ; i ++ ) { if ( arr [ i ] == x ) return i ; if ( arr [ i ] < x && arr [ i + 1 ] >= x ) return i + 1 ; } return -1 ; } int main ( ) { int arr [ ] = { 1 , 2 , 8 , 10 , 10 , 12 , 19 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 3 ; int index = ceilSearch ( arr , 0 , n - 1 , x ) ; if ( index == -1 ) cout << " Ceiling ▁ of ▁ " << x << " ▁ doesn ' t ▁ exist ▁ in ▁ array ▁ " ; else cout << " ceiling ▁ of ▁ " << x << " ▁ is ▁ " << arr [ index ] ; return 0 ; }
Ceiling in a sorted array |  ; Function to get index of ceiling of x in arr [ low . . high ] ; If x is smaller than or equal to the first element , then return the first element ; If x is greater than the last element , then return - 1 ; get the index of middle element of arr [ low . . high ] ; If x is same as middle element , then return mid ; If x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ] ; If x is smaller than arr [ mid ] , then either arr [ mid ] is ceiling of x or ceiling lies in arr [ low ... mid - 1 ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ceilSearch ( int arr [ ] , int low , int high , int x ) { int mid ; if ( x <= arr [ low ] ) return low ; if ( x > arr [ high ] ) return -1 ; mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] < x ) { if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid + 1 ; else return ceilSearch ( arr , mid + 1 , high , x ) ; } else { if ( mid - 1 >= low && x > arr [ mid - 1 ] ) return mid ; else return ceilSearch ( arr , low , mid - 1 , x ) ; } } int main ( ) { int arr [ ] = { 1 , 2 , 8 , 10 , 10 , 12 , 19 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 20 ; int index = ceilSearch ( arr , 0 , n - 1 , x ) ; if ( index == -1 ) cout << " Ceiling ▁ of ▁ " << x << " ▁ doesn ' t ▁ exist ▁ in ▁ array ▁ " ; else cout << " ceiling ▁ of ▁ " << x << " ▁ is ▁ " << arr [ index ] ; return 0 ; }
Sum of heights of all individual nodes in a binary tree | C ++ program to find sum of heights of all nodes in a binary tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Compute the " maxHeight " of a particular Node ; compute the height of each subtree ; use the larger one ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Function to sum of heights of individual Nodes Uses Inorder traversal ; Driver code
#include <bits/stdc++.h> NEW_LINE struct Node { int data ; struct Node * left ; struct Node * right ; } ; int getHeight ( struct Node * Node ) { if ( Node == NULL ) return 0 ; else { int lHeight = getHeight ( Node -> left ) ; int rHeight = getHeight ( Node -> right ) ; if ( lHeight > rHeight ) return ( lHeight + 1 ) ; else return ( rHeight + 1 ) ; } } struct Node * newNode ( int data ) { struct Node * Node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; return ( Node ) ; } int getTotalHeight ( struct Node * root ) { if ( root == NULL ) return 0 ; return getTotalHeight ( root -> left ) + getHeight ( root ) + getTotalHeight ( root -> right ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; printf ( " Sum ▁ of ▁ heights ▁ of ▁ all ▁ Nodes ▁ = ▁ % d " , getTotalHeight ( root ) ) ; return 0 ; }
Path with maximum average value | C / C ++ program to find maximum average cost path ; Maximum number of rows and / or columns ; method returns maximum average of all path of cost matrix ; Initialize first column of total cost ( dp ) array ; Initialize first row of dp array ; Construct rest of the dp array ; divide maximum sum by constant path length : ( 2 N - 1 ) for getting average ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int M = 100 ; double maxAverageOfPath ( int cost [ M ] [ M ] , int N ) { int dp [ N + 1 ] [ N + 1 ] ; dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; for ( int j = 1 ; j < N ; j ++ ) dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j <= N ; j ++ ) dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) + cost [ i ] [ j ] ; return ( double ) dp [ N - 1 ] [ N - 1 ] / ( 2 * N - 1 ) ; } int main ( ) { int cost [ M ] [ M ] = { { 1 , 2 , 3 } , { 6 , 5 , 4 } , { 7 , 3 , 9 } } ; printf ( " % f " , maxAverageOfPath ( cost , 3 ) ) ; return 0 ; }
Maximum weight path ending at any element of last row in a matrix | C ++ program to find the path having the maximum weight in matrix ; Function which return the maximum weight path sum ; creat 2D matrix to store the sum of the path ; Initialize first column of total weight array ( dp [ i to N ] [ 0 ] ) ; Calculate rest paht sum of weight matrix ; find the max weight path sum to rech the last row ; return maximum weight path sum ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; int maxCost ( int mat [ ] [ MAX ] , int N ) { int dp [ N ] [ N ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < i + 1 && j < N ; j ++ ) dp [ i ] [ j ] = mat [ i ] [ j ] + max ( dp [ i - 1 ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( result < dp [ N - 1 ] [ i ] ) result = dp [ N - 1 ] [ i ] ; return result ; } int main ( ) { int mat [ MAX ] [ MAX ] = { { 4 , 1 , 5 , 6 , 1 } , { 2 , 9 , 2 , 11 , 10 } , { 15 , 1 , 3 , 15 , 2 } , { 16 , 92 , 41 , 4 , 3 } , { 8 , 142 , 6 , 4 , 8 } } ; int N = 5 ; cout << " Maximum ▁ Path ▁ Sum ▁ : ▁ " << maxCost ( mat , N ) << endl ; return 0 ; }
Number of permutation with K inversions | C ++ program to find number of permutation with K inversion using Memoization ; Limit on N and K ; 2D array memo for stopping solving same problem again ; method recursively calculates permutation with K inversion ; base cases ; if already solved then return result directly ; calling recursively all subproblem of permutation size N - 1 ; Call recursively only if total inversion to be made are less than size ; store result into memo ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int M = 100 ; int memo [ M ] [ M ] ; int numberOfPermWithKInversion ( int N , int K ) { if ( N == 0 ) return 0 ; if ( K == 0 ) return 1 ; if ( memo [ N ] [ K ] != 0 ) return memo [ N ] [ K ] ; int sum = 0 ; for ( int i = 0 ; i <= K ; i ++ ) { if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ; } memo [ N ] [ K ] = sum ; return sum ; } int main ( ) { int N = 4 ; int K = 2 ; cout << numberOfPermWithKInversion ( N , K ) ; return 0 ; }
A Space Optimized DP solution for 0 | C ++ program of a space optimized DP solution for 0 - 1 knapsack problem . ; val [ ] is for storing maximum profit for each weight wt [ ] is for storing weights n number of item W maximum capacity of bag mat [ 2 ] [ W + 1 ] to store final result ; matrix to store final result ; iterate through all items ; one by one traverse each element ; traverse all weights j <= W ; if i is odd that mean till now we have odd number of elements so we store result in 1 th indexed row ; check for each value ; include element ; exclude element ; if i is even that mean till now we have even number of elements so we store result in 0 th indexed row ; Return mat [ 0 ] [ W ] if n is odd , else mat [ 1 ] [ W ] ; Driver program to test the cases
#include <bits/stdc++.h> NEW_LINE using namespace std ; int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) { int mat [ 2 ] [ W + 1 ] ; memset ( mat , 0 , sizeof ( mat ) ) ; int i = 0 ; while ( i < n ) { int j = 0 ; if ( i % 2 != 0 ) { while ( ++ j <= W ) { if ( wt [ i ] <= j ) mat [ 1 ] [ j ] = max ( val [ i ] + mat [ 0 ] [ j - wt [ i ] ] , mat [ 0 ] [ j ] ) ; else mat [ 1 ] [ j ] = mat [ 0 ] [ j ] ; } } else { while ( ++ j <= W ) { if ( wt [ i ] <= j ) mat [ 0 ] [ j ] = max ( val [ i ] + mat [ 1 ] [ j - wt [ i ] ] , mat [ 1 ] [ j ] ) ; else mat [ 0 ] [ j ] = mat [ 1 ] [ j ] ; } } i ++ ; } return ( n % 2 != 0 ) ? mat [ 0 ] [ W ] : mat [ 1 ] [ W ] ; } int main ( ) { int val [ ] = { 7 , 8 , 4 } , wt [ ] = { 3 , 8 , 6 } , W = 10 , n = 3 ; cout << KnapSack ( val , wt , n , W ) << endl ; return 0 ; }
Maximum profit by buying and selling a share at most k times | C ++ program to find out maximum profit by buying and / selling a share atmost k times given stock price of n days ; Function to find out maximum profit by buying & selling / a share atmost k times given stock price of n days ; table to store results of subproblems profit [ t ] [ i ] stores maximum profit using atmost t transactions up to day i ( including day i ) ; For day 0 , you can 't earn money  irrespective of how many times you trade ; profit is 0 if we don 't do any transaction  (i.e. k =0) ; fill the table in bottom - up fashion ; Driver Code
#include <climits> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int maxProfit ( int price [ ] , int n , int k ) { int profit [ k + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= k ; i ++ ) profit [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j <= n ; j ++ ) profit [ 0 ] [ j ] = 0 ; for ( int i = 1 ; i <= k ; i ++ ) { int prevDiff = INT_MIN ; for ( int j = 1 ; j < n ; j ++ ) { prevDiff = max ( prevDiff , profit [ i - 1 ] [ j - 1 ] - price [ j - 1 ] ) ; profit [ i ] [ j ] = max ( profit [ i ] [ j - 1 ] , price [ j ] + prevDiff ) ; } } return profit [ k ] [ n - 1 ] ; } int main ( ) { int k = 3 ; int price [ ] = { 12 , 14 , 17 , 10 , 14 , 13 , 12 , 15 } ; int n = sizeof ( price ) / sizeof ( price [ 0 ] ) ; cout << " Maximum ▁ profit ▁ is : ▁ " << maxProfit ( price , n , k ) ; return 0 ; }
Check if an array has a majority element | Hashing based C ++ program to find if there is a majority element in input array . ; Returns true if there is a majority element in a [ ] ; Insert all elements in a hash table ; Check if frequency of any element is n / 2 or more . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isMajority ( int a [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ a [ i ] ] ++ ; for ( auto x : mp ) if ( x . second >= n / 2 ) return true ; return false ; } int main ( ) { int a [ ] = { 2 , 3 , 9 , 2 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( isMajority ( a , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }