Maximize the number of segments of length p, q and r
Given a rod of length L, the task is to cut the rod in such a way that the total number of segments of length p, q and r is maximized. The segments can only be of length p, q, and r.
Examples:
Input: l = 11, p = 2, q = 3, r = 5
Output: 5
Segments of 2, 2, 2, 2 and 3Input: l = 7, p = 2, q = 5, r = 5
Output: 2
Segments of 2 and 5
Approach: Dynamic Programming is used to solve this problem. Initialize dp[] array to 0. Iterate till the length of the rod. For every i, a cut of p, q and r if possible is done. Initialize ans[i+p] = max( ans[i+p], 1 + ans[i]), ans[i+q] = max(ans[i+q], 1 + ans[i]) and ans[i+r] = max(ans[i+r], 1 + ans[i]) for all the possible cuts. ans[i] will be 0 if a cut at i-th index is not possible. ans[l] will give the maximum number of cuts possible.
Below is the implementation of the above approach:
C++
// C++ program to maximize the number // of segments of length p, q and r #include <bits/stdc++.h> using namespace std; // Function that returns the maximum number // of segments possible int findMaximum(int l, int p, int q, int r) { // Array to store the cut at each length int dp[l + 1]; // All values with -1 memset(dp, -1, sizeof(dp)); // if length of rod is 0 then total cuts will be 0 // so, initialize the dp[0] with 0 dp[0] = 0; for (int i = 0; i <= l; i++) { // if certain length is not possible if (dp[i] == -1) continue; // if a segment of p is possible if (i + p <= l) dp[i + p] = max(dp[i + p], dp[i] + 1); // if a segment of q is possible if (i + q <= l) dp[i + q] = max(dp[i + q], dp[i] + 1); // if a segment of r is possible if (i + r <= l) dp[i + r] = max(dp[i + r], dp[i] + 1); } // return value corresponding to length l return dp[l]; } // Driver Code int main() { int l = 11, p = 2, q = 3, r = 5; // Calling Function int ans = findMaximum(l, p, q, r); cout << ans; return 0; } |
Java
// Java program to maximize // the number of segments // of length p, q and r import java.io.*; class GFG { // Function that returns // the maximum number // of segments possible static int findMaximum(int l, int p, int q, int r) { // Array to store the // cut at each length int dp[] = new int[l + 1]; // All values with -1 for(int i = 0; i < l + 1; i++) dp[i] = -1; // if length of rod is 0 // then total cuts will // be 0 so, initialize // the dp[0] with 0 dp[0] = 0; for (int i = 0; i <= l; i++) { // if certain length // is not possible if (dp[i] == -1) continue; // if a segment of // p is possible if (i + p <= l) dp[i + p] = Math.max(dp[i + p], dp[i] + 1); // if a segment of // q is possible if (i + q <= l) dp[i + q] = Math.max(dp[i + q], dp[i] + 1); // if a segment of // r is possible if (i + r <= l) dp[i + r] = Math.max(dp[i + r], dp[i] + 1); } // return value corresponding // to length l return dp[l]; } // Driver Code public static void main (String[] args) { int l = 11, p = 2, q = 3, r = 5; // Calling Function int ans = findMaximum(l, p, q, r); System.out.println( ans); } } // This code is contributed // by anuj_67. |
Python 3
# Python 3 program to # maximize the number # of segments of length # p, q and r # Function that returns # the maximum number # of segments possible def findMaximum( l, p, q, r): # Array to store the cut # at each length # All values with -1 dp = [-1]*(l + 1) # if length of rod is 0 then # total cuts will be 0 # so, initialize the dp[0] with 0 dp[0] = 0 for i in range( l+1) : # if certain length is not # possible if (dp[i] == -1): continue # if a segment of p is possible if (i + p <= l): dp[i + p] = (max(dp[i + p], dp[i] + 1)) # if a segment of q is possible if (i + q <= l): dp[i + q] = (max(dp[i + q], dp[i] + 1)) # if a segment of r is possible if (i + r <= l): dp[i + r] = (max(dp[i + r], dp[i] + 1)) # return value corresponding # to length l return dp[l] # Driver Code if __name__ == "__main__": l = 11 p = 2 q = 3 r = 5 # Calling Function ans = findMaximum(l, p, q, r) print(ans) # This code is contributed by # ChitraNayal |
C#
// C# program to maximize // the number of segments // of length p, q and r using System; class GFG { // Function that returns // the maximum number // of segments possible static int findMaximum(int l, int p, int q, int r) { // Array to store the // cut at each length int []dp = new int[l + 1]; // All values with -1 for(int i = 0; i < l + 1; i++) dp[i] = -1; // if length of rod is 0 // then total cuts will // be 0 so, initialize // the dp[0] with 0 dp[0] = 0; for (int i = 0; i <= l; i++) { // if certain length // is not possible if (dp[i] == -1) continue; // if a segment of // p is possible if (i + p <= l) dp[i + p] = Math.Max(dp[i + p], dp[i] + 1); // if a segment of // q is possible if (i + q <= l) dp[i + q] = Math.Max(dp[i + q], dp[i] + 1); // if a segment of // r is possible if (i + r <= l) dp[i + r] = Math.Max(dp[i + r], dp[i] + 1); } // return value corresponding // to length l return dp[l]; } // Driver Code public static void Main () { int l = 11, p = 2, q = 3, r = 5; // Calling Function int ans = findMaximum(l, p, q, r); Console.WriteLine(ans); } } // This code is contributed // by anuj_67. |
5
Time Complexity: O(N)
Recommended Posts:
- Maximum number of segments that can contain the given points
- Number of segments where all elements are greater than X
- Number of horizontal or vertical line segments to connect 3 points
- Minimum number of cuts required to make circle segments equal sized
- Maximize the number of subarrays with XOR as zero
- Maximize the number of sum pairs which are divisible by K
- Maximize the product of four factors of a Number
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
- Number of ways to remove elements to maximize arithmetic mean
- Rearrange the string to maximize the number of palindromic substrings
- Convert a number of length N such that it contains any one digit at least 'K' times
- Number of balanced bracket subsequence of length 2 and 4
- Number of different cyclic paths of length N in a tetrahedron
- Check if it is possible to reach a number by making jumps of two given length
- Find consecutive 1s of length >= n in binary representation of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



