
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum number of segments of lengths a, b and c in C++
Given the task is to find the maximum number of line segments of lengths a, b and c that can be formed from given positive integer N.
Let’s now understand what we have to do using an example −
Input − N=8, a=3, b=1, c=2
Output − 8
Explanation − N can be divided into 8 segments of b which is maximum number of segments that can be made.
Input − N=13, a=2, b=7, c=3
Output − 6
Approach used in the below program as follows
In function MaxSegment() declare an array MaxSeg[N +1] of type int and initialize it with value -1.
The put the zero-th index equal to 0 as it will have no segments.
Loop from i=0 till i<N and check if (MaxSeg[i]! = -1).
Inside the above if statement put another statement if(i + a <=N) and put MaxSeg[i + a] = max(MaxSeg[i] + 1, MaxSeg[i + a]);
Repeat the above step for both b and c.
Outside the loop, return MaxSeg[N].
Example
#include <bits/stdc++.h> using namespace std; int MaxSegment(int N, int a,int b, int c){ /* It will store the maximum number of segments each index can have*/ int MaxSeg[N + 1]; // initialization memset(MaxSeg, -1, sizeof(MaxSeg)); // 0th index will have 0 segments MaxSeg[0] = 0; // traversing for every segments till n for (int i = 0; i < N; i++){ if (MaxSeg[i] != -1){ if(i + a <= N ){ MaxSeg[i + a] = max(MaxSeg[i] + 1, MaxSeg[i + a]); } if(i + b <= N ){ MaxSeg[i + b] = max(MaxSeg[i] + 1, MaxSeg[i + b]); } if(i + c <= N ){ MaxSeg[i + c] = max(MaxSeg[i] + 1, MaxSeg[i + c]); } } } return MaxSeg[N]; } int main(){ int N = 13, a = 2, b = 7, c = 3; cout << MaxSegment(N, a, b, c); return 0; }
Output
If we run the above code we will get the following output −
6
- Related Articles
- Maximum number of segments that can contain the given points in C++
- Maximum Product of Word Lengths in C++
- Number of Segments in a String in C++
- Maximum length of segments of 0’s and 1’s in C++
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- In the figure, a \( \triangle A B C \) is drawn to circumscribe a circle of radius \( 4 \mathrm{~cm} \) such that the segments \( B D \) and \( D C \) are of lengths \( 8 \mathrm{~cm} \) and \( 6 \mathrm{~cm} \) respectively. Find the lengths of sides \( A B \) and \( A C \), when area of \( \triangle A B C \) is \( 84 \mathrm{~cm}^{2} \). "\n
- Maximum possible intersection by moving centers of line segments in C++
- Maximize the number of segments of length p, q and r in C++
- C++ Program to get maximum area of rectangle made from line segments
- Maximum Number of Occurrences of a Substring in C++
- Maximum number that can be display on Seven Segment Display using N segments in C++
- Maximum Number of Ones in C++
- Maximum sum of lengths of non-overlapping subarrays with k as the max element in C++
- Find sum of a number and its maximum prime factor in C++
- C++ program to find the number of triangles amongst horizontal and vertical line segments
