27/03/2023

Attending Workshops Hackerrank Solution in C++

Attending Workshops Hackerrank Solution in C++. A student signed up for n workshops and wants to attend the maximum number of workshops where no two workshops overlap. You must do the following:

Implement 2 structures:

1. struct Workshop has the following members:

  • The workshop's start time.
  • The workshop's duration.
  • The workshop's end time.

2. struct Available_Workshops having the following members:

  • An integer, n (the number of workshops the student signed up for).
  • An array of type Workshop array having size n.

Implement  functions:

Attending Workshops Hackerrank Solution in C++

Available_Workshops* initialize (int start_time[], int duration[], int n)
Creates an Available_Workshops object and initializes its elements using the elements in the start_time[] and duration[] parameters (both are of size n). Here, start_time[] and duration[] are the respective start time and duration for the ith workshop. This function must return a pointer to an Available_Workshops object.

int CalculateMaxWorkshops(Available_Workshops* ptr)
Returns the maximum number of workshops the student can attend—without overlap. The next workshop cannot be attended until the previous workshop ends.

Note: An array of unknown size (n) should be declared as follows:

DataType* arrayName = new DataType[n];

Input Format

Input from stdin is handled by the locked code in the editor; you simply need to write your functions to meet the specifications of the problem statement above.

Output Format

Output to stdout is handled for you.

Your initialize function must return a pointer to an Available_Workshops object.
Your CalculateMaxWorkshops function must return a maximum number of non-overlapping workshops the student can attend.

Sample Input

6
1 3 0 5 5 8
1 1 6 2 4 1

Sample Output

CalculateMaxWorkshops should return 4.

Explanation

The first line denotes n, the number of workshops.
The next line contains n space-separated integers where the ith integer is the ith workshop's start time.
The next line contains n space-separated integers where the ith integer is the ith workshop's duration.

The student can attend workshops 0, 1, 3 and 5 without overlap, so CalculateMaxWorkshops returns 4 to the main (which then prints 4 to stdout).

Submit your solution here: Click here

Attending Workshops Hackerrank Solution in C++


#include <bits/stdc++.h>
using namespace std;

//Define the structs Workshops and Available_Workshops.
//Implement the functions initialize and CalculateMaxWorkshops

struct Workshop
{
	int m_startTime;
	int m_duration;
	int m_endTime;

	int operator < (const Workshop &oprand)
	{
		return m_endTime < oprand.m_endTime;
	}
};

struct Available_Workshops
{
	int m_numWorkshops = 0;
	Workshop *m_arrWorkshop = nullptr;

	Available_Workshops(int n): m_numWorkshops(n)
	{
		m_arrWorkshop = new Workshop[m_numWorkshops];
	}~Available_Workshops()
	{
		delete[] m_arrWorkshop;
	}
};

Available_Workshops* initialize(int *start_time, int *duration, int n)
{
	Available_Workshops *resPtr = new Available_Workshops(n);
	resPtr->m_numWorkshops = n;
	for (int i = 0; i < n; i++)
	{
		resPtr->m_arrWorkshop[i].m_startTime = start_time[i];
		resPtr->m_arrWorkshop[i].m_duration = duration[i];
		resPtr->m_arrWorkshop[i].m_endTime = start_time[i] + duration[i];
	}

	return resPtr;
}

int CalculateMaxWorkshops(Available_Workshops *workshopsPtr)
{
	if (workshopsPtr->m_arrWorkshop == nullptr) return 0;

	std::sort(workshopsPtr->m_arrWorkshop, workshopsPtr->m_arrWorkshop + workshopsPtr->m_numWorkshops);

	Workshop *lastPtr = &workshopsPtr->m_arrWorkshop[0];
	int res = 1;
	for (int i = 1; i < workshopsPtr->m_numWorkshops; i++)
	{
		if (lastPtr->m_endTime <= workshopsPtr->m_arrWorkshop[i].m_startTime)
		{
			res++;
			lastPtr = &workshopsPtr->m_arrWorkshop[i];
		}
	}

	return res;
}

int main(int argc, char *argv[])
{
	int n;	// number of workshops
	cin >> n;
	// create arrays of unknown size n
	int *start_time = new int[n];
	int *duration = new int[n];

	for (int i = 0; i < n; i++)
	{
		cin >> start_time[i];
	}

	for (int i = 0; i < n; i++)
	{
		cin >> duration[i];
	}

	Available_Workshops * ptr;
	ptr = initialize(start_time, duration, n);
	cout << CalculateMaxWorkshops(ptr) << endl;
	return 0;
}

The Output of Attending Workshops Hackerrank Solution


The Output of Attending Workshops Hackerrank Solution

Similar to Attending Workshops


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: