Maximum number of segments of lengths a, b and c
Last Updated :
13 Jan, 2023
Given a positive integer N, find the maximum number of segments of lengths a, b and c that can be formed from N .
Examples :
Input : N = 7, a = 5, b, = 2, c = 5
Output : 2
N can be divided into 2 segments of lengths
2 and 5. For the second example,
Input : N = 17, a = 2, b = 1, c = 3
Output : 17
N can be divided into 17 segments of 1 or 8
segments of 2 and 1 segment of 1. But 17 segments
of 1 is greater than 9 segments of 2 and 1.
To understand any DP problem clearly, we need to write first of all its recursive code and then go for optimization.
Recursion-Based Solution:
Here for any value of n, we have 3 possibilities, for making the maximum segment count
if (n >= a) we can make 1 segment of length a + another possible segment from the length of n - a
if (n >= b) we can make 1 segment of length b + another possible segment from the length of n - b
if (n >= c) we can make 1 segment of length c + another possible segment from the length of n - c
so now we have to take the maximum possible segment above in 3 condition
Below is an implementation for the same.
C++
// C++ implementation to divide N into maximum
// number of segments of length a, b and c
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum number
// of segments
int maximumSegments(int n, int a, int b, int c)
{
// Base case
if (n == 0) {
return 0;
}
int maxa = INT_MIN;
// Conditions
// Making one segment of length a
if (n >= a) {
maxa = max(maxa,
1 + maximumSegments(n - a, a, b, c));
}
// Making one segment of length b
if (n >= b) {
maxa = max(maxa,
1 + maximumSegments(n - b, a, b, c));
}
// Making one segment of length c
if (n >= c) {
maxa = max(maxa,
1 + maximumSegments(n - c, a, b, c));
}
// Return maximum out of all possible segment
return maxa;
}
// Driver code
int main()
{
int n = 7, a = 5, b = 2, c = 5;
// Function call
cout << maximumSegments(n, a, b, c);
return 0;
}
Java
// Java implementation to divide N into
// maximum number of segments of length a, b and c
class GFG {
static int INT_MIN = -1000000000;
// Function to find the maximum number of segments
static int maximumSegments(int n, int a, int b, int c)
{
// Base case
if (n == 0) {
return 0;
}
int maxa = INT_MIN;
// Conditions
// Making one segment of length a
if (n >= a) {
maxa = Math.max(maxa, 1 + maximumSegments(n - a, a, b, c));
}
// Making one segment of length b
if (n >= b) {
maxa = Math.max(maxa, 1 + maximumSegments(n - b, a, b, c));
}
// Making one segment of length c
if (n >= c) {
maxa = Math.max(maxa, 1 + maximumSegments(n - c, a, b, c));
}
// Return maximum out of all possible segment
return maxa;
}
// Driver code
public static void main(String[] args) {
int n = 7, a = 5, b = 2, c = 5;
// Function call
System.out.println(maximumSegments(n, a, b, c));
}
}
// This code is contributed by ajaymakvana.
Python
# Python implementation to divide N into maximum
# number of segments of length a, b and c
# Function to find the maximum number
# of segments
def maximumSegments(n, a, b, c):
# Base case
if n == 0:
return 0
maxa = float('-inf')
# Conditions
# Making one segment of length a
if n >= a:
maxa = max(maxa,
1 + maximumSegments(n - a, a, b, c))
# Making one segment of length b
if n >= b:
maxa = max(maxa,
1 + maximumSegments(n - b, a, b, c))
# Making one segment of length c
if n >= c:
maxa = max(maxa,
1 + maximumSegments(n - c, a, b, c))
# Return maximum out of all possible segment
return maxa
# Driver code
if __name__ == '__main__':
n = 7
a = 5
b = 2
c = 5
# Function call
print(maximumSegments(n, a, b, c))
# This code is contributed by divyansh2212
C#
using System;
namespace ConsoleApp {
class Program {
static void Main(string[] args)
{
int n = 7, a = 5, b = 2, c = 5;
Console.WriteLine(maximumSegments(n, a, b, c));
}
// Function to find the maximum number of segments
static int maximumSegments(int n, int a, int b, int c)
{
// Base case
if (n == 0) {
return 0;
}
int maxa = int.MinValue;
// Conditions
// Making one segment of length a
if (n >= a) {
maxa = Math.Max(
maxa, 1 + maximumSegments(n - a, a, b, c));
}
// Making one segment of length b
if (n >= b) {
maxa = Math.Max(
maxa, 1 + maximumSegments(n - b, a, b, c));
}
// Making one segment of length c
if (n >= c) {
maxa = Math.Max(
maxa, 1 + maximumSegments(n - c, a, b, c));
}
// Return maximum out of all possible segments
return maxa;
}
}
}
// This code is contributed by divyansh2212
JavaScript
// Javascript implementation to divide N into maximum
// number of segments of length a, b and c
// Function to find the maximum number
// of segments
function maximumSegments(n, a, b, c) {
// Base case
if (n === 0) {
return 0;
}
let maxa = Number.MIN_SAFE_INTEGER;
// Making one segment of length a
if (n >= a) {
maxa = Math.max(maxa,
1 + maximumSegments(n - a, a, b, c));
}
// Making one segment of length b
if (n >= b) {
maxa = Math.max(maxa,
1 + maximumSegments(n - b, a, b, c));
}
// Making one segment of length c
if (n >= c) {
maxa = Math.max(maxa,
1 + maximumSegments(n - c, a, b, c));
}
// Return maximum out of all possible segment
return maxa;
}
// Driver code
let n = 7, a = 5, b = 2, c = 5;
// Function call
console.log(maximumSegments(n, a, b, c));
// This code is contributed by poojaagarwal2.
Time Complexity: O(3n)
Auxiliary Space : O(n)
Optimized Approach : The approach used is Dynamic Programming. The base dp0 will be 0 as initially it has no segments. After that, iterate from 1 to n, and for each of the 3 states i.e, dpi+a, dpi+b and dpi+c, store the maximum value obtained by either using or not using the a, b or c segment.
The 3 states to deal with are :
dpi+a=max(dpi+1, dpi+a);
dpi+b=max(dpi+1, dpi+b);
dpi+c=max(dpi+1, dpi+c);
Below is the implementation of above idea :
C++
// C++ implementation to divide N into
// maximum number of segments
// of length a, b and c
#include <bits/stdc++.h>
using namespace std;
// function to find the maximum
// number of segments
int maximumSegments(int n, int a, int b, int c)
{
// stores the maximum number of
// segments each index can have
int dp[n + 1];
// initialize with -1
memset(dp, -1, sizeof(dp));
// 0th index will have 0 segments
// base case
dp[0] = 0;
// traverse for all possible
// segments till n
for (int i = 0; i < n; i++) {
if (dp[i] != -1) {
// conditions
if (i + a <= n) // avoid buffer overflow
dp[i + a] = max(dp[i] + 1, dp[i + a]);
if (i + b <= n) // avoid buffer overflow
dp[i + b] = max(dp[i] + 1, dp[i + b]);
if (i + c <= n) // avoid buffer overflow
dp[i + c] = max(dp[i] + 1, dp[i + c]);
}
}
return dp[n];
}
// Driver code
int main()
{
int n = 7, a = 5, b = 2, c = 5;
cout << maximumSegments(n, a, b, c);
return 0;
}
Java
// Java implementation to divide N into
// maximum number of segments
// of length a, b and c
import java.util.*;
class GFG
{
// function to find the maximum
// number of segments
static int maximumSegments(int n, int a,
int b, int c)
{
// stores the maximum number of
// segments each index can have
int dp[] = new int[n + 10];
// initialize with -1
Arrays.fill(dp, -1);
// 0th index will have 0 segments
// base case
dp[0] = 0;
// traverse for all possible
// segments till n
for (int i = 0; i < n; i++)
{
if (dp[i] != -1)
{
// conditions
if(i + a <= n ) //avoid buffer overflow
dp[i + a] = Math.max(dp[i] + 1,
dp[i + a]);
if(i + b <= n ) //avoid buffer overflow
dp[i + b] = Math.max(dp[i] + 1,
dp[i + b]);
if(i + c <= n ) //avoid buffer overflow
dp[i + c] = Math.max(dp[i] + 1,
dp[i + c]);
}
}
return dp[n];
}
// Driver code
public static void main(String arg[])
{
int n = 7, a = 5, b = 2, c = 5;
System.out.print(maximumSegments(n, a, b, c));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python implementation
# to divide N into maximum
# number of segments of
# length a, b and c
# function to find
# the maximum number
# of segments
def maximumSegments(n, a, b, c) :
# stores the maximum
# number of segments
# each index can have
dp = [-1] * (n + 10)
# 0th index will have
# 0 segments base case
dp[0] = 0
# traverse for all possible
# segments till n
for i in range(0, n) :
if (dp[i] != -1) :
# conditions
if(i + a <= n ): # avoid buffer overflow
dp[i + a] = max(dp[i] + 1,
dp[i + a])
if(i + b <= n ): # avoid buffer overflow
dp[i + b] = max(dp[i] + 1,
dp[i + b])
if(i + c <= n ): # avoid buffer overflow
dp[i + c] = max(dp[i] + 1,
dp[i + c])
return dp[n]
# Driver code
n = 7
a = 5
b = 2
c = 5
print (maximumSegments(n, a,
b, c))
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# implementation to divide N into
// maximum number of segments
// of length a, b and c
using System;
class GFG
{
// function to find the maximum
// number of segments
static int maximumSegments(int n, int a,
int b, int c)
{
// stores the maximum number of
// segments each index can have
int []dp = new int[n + 10];
// initialize with -1
for(int i = 0; i < n + 10; i++)
dp[i]= -1;
// 0th index will have 0 segments
// base case
dp[0] = 0;
// traverse for all possible
// segments till n
for (int i = 0; i < n; i++)
{
if (dp[i] != -1)
{
// conditions
if(i + a <= n ) // avoid buffer overflow
dp[i + a] = Math.Max(dp[i] + 1,
dp[i + a]);
if(i + b <= n ) // avoid buffer overflow
dp[i + b] = Math.Max(dp[i] + 1,
dp[i + b]);
if(i + c <= n ) // avoid buffer overflow
dp[i + c] = Math.Max(dp[i] + 1,
dp[i + c]);
}
}
return dp[n];
}
// Driver code
public static void Main()
{
int n = 7, a = 5, b = 2, c = 5;
Console.Write(maximumSegments(n, a, b, c));
}
}
// This code is contributed by nitin mittal
PHP
<?php
// PHP implementation to divide
// N into maximum number of
// segments of length a, b and c
// function to find the maximum
// number of segments
function maximumSegments($n, $a,
$b, $c)
{
// stores the maximum
// number of segments
// each index can have
$dp = array();
// initialize with -1
for($i = 0; $i < $n + 10; $i++)
$dp[$i]= -1;
// 0th index will have
// 0 segments base case
$dp[0] = 0;
// traverse for all possible
// segments till n
for ($i = 0; $i < $n; $i++)
{
if ($dp[$i] != -1)
{
// conditions
if($i + $a <= $n ) // avoid buffer overflow
$dp[$i + $a] = max($dp[$i] + 1,
$dp[$i + $a]);
if($i + $b <= $n ) // avoid buffer overflow
$dp[$i + $b] = max($dp[$i] + 1,
$dp[$i + $b]);
if($i + $c <= $n ) // avoid buffer overflow
$dp[$i + $c] = max($dp[$i] + 1,
$dp[$i + $c]);
}
}
return $dp[$n];
}
// Driver code
$n = 7; $a = 5;
$b = 2; $c = 5;
echo (maximumSegments($n, $a,
$b, $c));
// This code is contributed by
// Manish Shaw(manishshaw1)
?>
JavaScript
<script>
// JavaScript program implementation to divide N into
// maximum number of segments
// of length a, b and c
// function to find the maximum
// number of segments
function maximumSegments(n, a, b, c)
{
// stores the maximum number of
// segments each index can have
let dp = [];
// initialize with -1
for(let i = 0; i < n + 10; i++)
dp[i]= -1;
// 0th index will have 0 segments
// base case
dp[0] = 0;
// traverse for all possible
// segments till n
for (let i = 0; i < n; i++)
{
if (dp[i] != -1)
{
// conditions
if(i + a <= n ) //avoid buffer overflow
dp[i + a] = Math.max(dp[i] + 1,
dp[i + a]);
if(i + b <= n ) //avoid buffer overflow
dp[i + b] = Math.max(dp[i] + 1,
dp[i + b]);
if(i + c <= n ) //avoid buffer overflow
dp[i + c] = Math.max(dp[i] + 1,
dp[i + c]);
}
}
return dp[n];
}
// Driver Code
let n = 7, a = 5, b = 2, c = 5;
document.write(maximumSegments(n, a, b, c));
// This code is contributed by susmitakundugoaldanga.
</script>
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(N), as we are using extra space for dp array.
Similar Reads
Maximize the number of segments of length x, y and z Given a rod of length n, the task is to cut the rod in such a way that the total number of segments of length x, y, and z is maximized. The segments can only be of length x, y, and z. Note: If no segment can be cut then return 0.Examples: Input: n = 4, x = 2, y = 1, z = 1Output: 4Explanation: Total
15+ min read
Maximum number of segments that can contain the given points Given an array arr[] containing N integers and two integers X and Y. Consider N line segments, where each line segment has starting and ending point as arr[i] - X and arr[i] + Y respectively. Given another array b[] of M points. The task is to assign these points to segments such that the number of
7 min read
Maximum number of pieces in N cuts Given a square piece and a total number of cuts available n, Find out the maximum number of rectangular or square pieces of equal size that can be obtained with n cuts. The allowed cuts are horizontal and vertical cut. Note: Stacking and folding is not allowed.Examples: Input : n = 1 Output : 2 Expl
5 min read
Minimum and maximum possible length of the third side of a triangle Given two sides of a triangle s1 and s2, the task is to find the minimum and maximum possible length of the third side of the given triangle. Print -1 if it is not possible to make a triangle with the given side lengths. Note that the length of all the sides must be integers.Examples: Input: s1 = 3,
5 min read
Maximum number of parallelograms that can be made using the given length of line segments Given N line segments where the length of the ith line segment is a_i . The task is to find the maximum number of parallelograms can be made with those line segments if each line segment is used at most in one parallelogram.Examples: Input: arr[] = {1, 2, 1, 2} Output: 1 Only one parallelogram can b
6 min read
Maximum number of elements without overlapping in a Line Given two arrays X and L of same size N. Xi represent the position in an infinite line. Li represents the range up to which ith element can cover on both sides. The task is to select the maximum number of elements such that no two selected elements overlap if they cover the right or the left side se
7 min read