Q:
Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?
belongs to collection: interview C++ coding problems/challenges | Recursion
interview C++ coding problems/challenges | Recursion
- Given an array A of size N. Find the minimum number of operations needed to convert the given array to Palindromic Array
- Given n wines in a row, with integers denoting the cost of each wine respectively. Each year you can sale the first or the last wine in the row
- Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?
- You are given N eggs, and building with K floors from 1 to K. Each egg is identical, you drop an egg and if an egg breaks, you cannot drop it again
- Given a binary Tree find the maximum path sum. The path may start and end at any node in the tree
- Given an array of n integers. Find the minimum number of elements from the array to remove or delete so that when the remaining elements are placed in the same sequence order form a sorted sequence
- You are provided an input string S and the string \"includehelp\". You need to figure out all possible subsequences \"includehelp\" in the string S?
- Given two positive integer n and m, find how many arrays of size n that can be formed
- Shivang is very foodie but he has a diet plan. He has an array of elements indicating the calorie of food he can consume on that day
- Imagine you have a special keyboard with four types of keys: Key 1: Prints \'I\' on screen Key 2: Select whatever printed in screen Key 3: Copy selection to buffer Key 4: Append buffer on-screen after what has already been printed
- Given a string S, write a program to check if it is possible to construct the given string S by performing any of the below operations any number of times
- Given the number of digits n, find the count of total non-decreasing numbers with n digits
- You need to display N similar characters on a screen. You are allowed to do three types of operation each time
- Weights and values are given for n items along with the maximum capacity allowed W. What is the maximum value we can achieve if we can pick any weights, any number of times for the total allowed capacity of W?
- Given 3 strings X, Y and Z, the task is to find the longest common sub-sequence in all three given sequences
- Given a rectangular grid of dimension 2 x N find out the maximum sum such that no two chosen numbers are adjacent, vertically, diagonally or horizontally
- Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1\'s. Output your answer mod 10^9 + 7
- Given a string str, find the longest palindromic substring. A substring need to be consecutive such that for any xixj i<j must be valid in the parent string too
- Given a string str, find total number of possible palindromic sub-sequences. A substring need to be consecutive such that for any xixj i<j must be valid in the parent string too
- Given a range 1 to N. Among its different permutations you have to find out those permutations where only one element is greater than its previous element and you have to count that number
- Given N number of parenthesis (pair of opening and closing parenthesis), you have to print the valid combinations of the parenthesis and print the value
- Given N number of parenthesis (pair of opening and closing parenthesis), you have to count all the valid combinations of the parenthesis and print the value
- Given an array of integers A[] and a positive integer k, find whether it\'s possible to divide this array into k non-empty subsets whose sums are all equal
- Given a number n, count minimum steps to minimize it to 1
- You are standing in some location (x, y) on an island, given in form of a matrix of size N*M, you are allowed to move in all four directions left, right, up and down. You can move only one step at a time, if you step out of the matrix region then you die
- You are given a list of jobs where each job has a start time, finish time, and the profit associated with that job, find the maximum profit subset of non-overlapping jobs
- Given a string, find out if the string is K-palindrome or not. A k-palindrome string transforms into a palindrome on removing at most k characters from it
- You are given a staircase, you need to find the total number of ways to reach the nth stair from the bottom of the staircase when you are only allowed to climb 1, 2 or 3 stairs at a time
- Given the mobile numeric keypad. You can only press buttons that are up, left, right or down to the current button
- Given a rectangular matrix of size N*M, we can move from the current cell in 4 directions with equal probability
- Given a rope of length n meters, cut the rope in different parts of integer lengths in a way that maximizes the product of lengths of all parts. You must make at least one cut
- Given an array A of N positive integers. Find the sum of maximum sum increasing subsequence of the given array
- Given a N*M binary matrix find the size of largest square sub-matrix of 1\'s present in it
- You are given a matrix of size N*M with each cell having some values, you need to find the number of paths from first cell to last bottom right cell such that the path has exactly given sum. You can only move in two direction either right or down
- Given an array of integers and a sum, the task is to count all subsets of a given array with the sum equal to the given sum
- Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with a sum equal to the given sum
1) Recursive solution
We will start the solution with initially sum = V cents and at each iteration find the minimum coins required by dividing the problem into subproblems where we take {C1, C2, ..., CN} coin and decrease the sum V.
By C[i] (based on the coin we took). Whenever V becomes 0, this means we have a possible or otherwise there is no solution and we return -1;
Solution: To find the optimal answer, we return the minimum of all answers for which N became 0.
If V == 0, then 0 coins required.
We calculate the total required number of coins using the following function:
So, initially the function call would be:
Where C is the coins array, N is the size of the coin array and V is the required sum.
Now our idea is the get the minimum number of coins from the given array
and if the sum that we want to get if not possible from the given set of coins we will return -1.
So, let's see the function definition:
Function:
Pseudo Code:
The recursive definition consists of the part:
This recursive part will generate a recursion tree where we can find many overlapping subproblems, hence we would solve by dynamic programming.
C++ Implementation:
Output
2) Dynamic Programming Approach
Since the same subproblems are called, again and again, this problem has Overlapping Subproblems property.
Like other typical Dynamic Programming (DP) problems, re-computations of the same subproblems can be avoided by constructing a temporary array dp[] and memorizing the computed values in this array.
We will declare a dp[] array of length equal to the sum required+1 since indexing starts from 0, we will use Top-Down DP, use memoization plus recursion approach.
Initially, we will fill the dp[] array with -1 so that during recursion call if the array if not -1 then we don't have to calculate the value we just need to return the dp[value](memoized array), else we will call the function and fill the required value of the array.
2.1) Top Down Approach
pseudo code:
C++ Implementation:
Output
2.2) Bottom Up Approach
ith state of dp : dp[i] : Minimum number of coins required to sum to i cents.
In this approach, we move from the base case to the desired value of the sum
base dp[0]=0 , where for 0 cents of value we need 0 coins.
Initially, we will fill the dp[] array with INT_MAX and dp[0]=0 as for 0 cents we need 0 coins.
Then we will iterate from 1 cent required sum to V value cents.
we will use the outer loop for sum array and inner loop for coins array.
Pseudo Code:
The overall Time complexity of this approach is O(N*V).
C++ Implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer