- 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

# C++ Program to find out how many movies an attendee can watch entirely at a Film festival

Suppose there is a film festival going on that showcase various movies from various countries. Now, an attendee wants to attend the maximum number of movies that do not overlap with each other and we have to help them to find out how many movies they can attend.

There is a structure Movie that has the following members −

- The beginning time of the movie.
- The duration of the movie.
- The ending time of the movie.

There is another structure Festival with the following members −

- The number of movies at the festival.
- An array of type Movie whose size is similar to the number of movies at the festival.

We have to create and initialize a Festival object with two arrays 'timeBegin' and 'duration' that contain the start time and duration of several movies respectively. An integer n denotes the total number of movies and that is also used to initialize the object. We further use that object to calculate how many movies an attendee can fully watch.

So, if the input is like timeBegin = {1, 3, 0, 5, 5, 8, 8}, duration = {3, 2, 2, 4, 3, 2, 3}, n = 7, then the output will be 4

The attendee can watch a total of 4 movies entirely at that festival.

To solve this, we will follow these steps −

- struct Movie {
- Define three member variables timeBegin, duration, timeEnd
- Overload an operator ‘<’, this will take a Movie type variable another.
- return timeEnd < another.timeEnd

- struct Festival {
- Define a member count
- Define an array movies that contains item of type Movie

- Define a function initialize(). This will take arrays timeBegin and timeEnd and an iteger n.
- filmFestival := A new Festival object
- count of filmFestival := count
- for initialize i := 0, when i < count, update (increase i by 1), do −
- temp := a new object of type Movie
- timeBegin of temp:= timeBegin[i]
- duration of temp:= duration[i]
- timeEnd of temp := timeBegin[i] + duration[i]
- insert temp into array movies of filmFestival

- return filmFestival

- Define a function solve(), this will take a variable fest of type Festival,
- res := 0
- sort the array movies of fest
- timeEnd := -1
- for initialize i := 0, when i < fest - > count, update (increase i by 1), do −
- if timeBegin of movies[i] of fest >= timeEnd, then −
- (increase res by 1)
- timeEnd := timeEnd of movies[i] of fest

- if timeBegin of movies[i] of fest >= timeEnd, then −
- return res

## Example

Let us see the following implementation to get better understanding −

#include<bits/stdc++.h> using namespace std; struct Movie { int timeBegin, duration, timeEnd; bool operator<(const Movie& another) const { return timeEnd < another.timeEnd; } }; struct Festival { int count; vector<Movie> movies; }; Festival* initialize(int timeBegin[], int duration[], int count) { Festival* filmFestival = new Festival; filmFestival->count = count; for (int i = 0; i < count; i++) { Movie temp; temp.timeBegin = timeBegin[i]; temp.duration = duration[i]; temp.timeEnd = timeBegin[i] + duration[i]; filmFestival->movies.push_back(temp); } return filmFestival; } int solve(Festival* fest) { int res = 0; sort(fest->movies.begin(), fest->movies.end()); int timeEnd = -1; for (int i = 0; i < fest->count; i++) { if (fest->movies[i].timeBegin >= timeEnd) { res++; timeEnd = fest->movies[i].timeEnd; } } return res; } int main(int argc, char *argv[]) { int timeBegin[] = {1, 3, 0, 5, 5, 8, 8}; int duration[] = {3, 2, 2, 4, 3, 2, 3}; Festival * fest; fest = initialize(timeBegin,duration, 7); cout << solve(fest) << endl; return 0; }

## Input

int timeBegin[] = {1, 3, 0, 5, 5, 8, 8}; int duration[] = {3, 2, 2, 4, 3, 2, 3}; Festival * fest; fest = initialize(timeBegin,duration, 7);

## Output

4

- Related Articles
- How to watch hd movies without data drainage
- Caution! Subtitled Files Can Hack The Devices When You Watch Movies
- Program to find out how many transfer requests can be satisfied in Python
- Program to find out how many boxes can be put into the godown in Python
- Python Program to find out how many cubes are cut
- C++ program to find maximum how many chocolates we can buy with at most k rupees
- 9 Eye Symptoms to Watch Out For
- Python Program to find out how many times the balls will collide in a circular tube
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Program to find how many ways we can climb stairs in Python
- C++ Program to find how many pawns can reach the first row
- I purchased a watch for â‚¹ 330 and sold it at a loss 20 %. Find selling price of the watch.
- How many of Chetan Bhagat's books are turned into movies?
- Program to find maximum how many water bottles we can drink in Python
- C++ Program to find which episode we have missed to watch