Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Attending workshops solution in C++ programming

YASH PAL, 31 July 202416 January 2026

In this HackerRank Attending Workshops problem solution in C++ programming 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:

struct Workshop having the following members:

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

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 2 functions:

  1. 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[i] and duration[i] are the respective start time and duration for the  workshop. This function must return a pointer to an Available_Workshops object.
  2. 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.
HackerRank Attending workshops solution in c++ programming

HackerRank Attending workshops problem solution in c++ programming.

#include<bits/stdc++.h>

using namespace std;

//Define the structs Workshops and Available_Workshops.
//Implement the functions initialize and CalculateMaxWorkshops
struct Workshops{
    friend ostream &operator<<(ostream &os, const Workshops &obj);
    int start_time;
    int end_time;
    int duration;

    bool operator<(const Workshops &rhs){
    return (this->end_time < rhs.end_time);
    }
};

ostream &operator<<(ostream &os, const Workshops &obj){
    os << obj.start_time << ": " << obj.end_time << ": "
    << obj.duration << "n";
    return os;
}

struct Available_Workshops{
    int n;
    vector<Workshops> vec;
};

Available_Workshops* initialize(int start_time[], int duration[], int num)
{
    Available_Workshops *avail = new Available_Workshops;
    avail->n = num;
    Workshops test;
    for(int i{0}; i < num; i++){
        test.start_time = start_time[i];
        test.duration = duration[i];
        test.end_time = start_time[i] + duration[i];
        avail->vec.push_back(test);
    }
    sort(avail->vec.begin(), avail->vec.end());
    return avail;
}

int CalculateMaxWorkshops(Available_Workshops *test){
    int w_count = 1;
    int test_end_time = test->vec.at(0).end_time;
    for(int i{1}; i < test->n; i++){
        if(test_end_time <= test->vec.at(i).start_time){
            w_count++;
            test_end_time = test->vec.at(i).end_time;
        }
    }
    return w_count;
}

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;
}

Second solution

#include<bits/stdc++.h>

using namespace std;
typedef struct Workshops
{
    int s,d,e;
}Workshops;

typedef struct Available_Workshops
{
    int n;
    Workshops *shops;
    Available_Workshops(int t)
    {
        n = t;
        shops = (Workshops*)malloc(t*sizeof(Workshops));
    }
}Available_Workshops;

bool comp(const Workshops &a, const Workshops &b)
{
    if(a.e < b.e)
        return true;
    return false;
}
Available_Workshops* initialize (int start_time[], int duration[] ,int N) 
{
    Available_Workshops* node = new Available_Workshops(N);
    for(int i =0 ; i< node->n; i++)
    {
        (node->shops)[i].s = start_time[i];
        (node->shops)[i].d = duration[i];
        (node->shops)[i].e = start_time[i] + duration[i];
    }
    return node;
}

int CalculateMaxWorkshops (Available_Workshops* ptr) 
{
    sort(ptr->shops, ptr->shops + ptr->n, comp);
    int ans = 1, t;
    for(int i = 0 ; i < ptr->n; i++)
    {
        if(i == 0)
        {
            t = (ptr->shops[i]).e;    
            continue;
        }
        if((ptr->shops[i]).s>=t)
        {
            ans++;
            t = (ptr->shops[i]).e;
        }
    }
    return ans;
}

int main()
{
    int n;
    cin>>n;
    int start_time[n],duration[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;
}
C++ Solutions coding problems solutions Hackerrank Problems Solutions cppHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

HackerRank C++ Problems Solutions
Say “Hello, World!” with C++ solution
Input and Output solution
Basic Data Types solution
Conditional Statements solution
For loop solution
Functions solution
Pointer solution
Arrays Introduction solution
Variable-Sized Arrays solution
Attribute Parser solution
StringStream solution
Strings solution
Structs solution
Class solution
classes and objects solution
Box It! solution
Inherited code solution
Exceptional server solution
Virtual Functions solution
Abstract Classes — Polymorphism solution
Vector-Sort solution
Vector-Erase solution
Lower Bound-STL solution
Sets-STL solution
Maps-STL solution
Print Pretty solution
Deque-STL solution
Inheritance Introduction solution
Hotel Prices solution
Cpp exception handling solution
Rectangle Area solution
Multi-Level Inheritance solution
Overloading Ostream Operator solution
Messages Order solution
Accessing Inherited Functions solution
Magic Spells solution
C++ Class Templates problem solution
Preprocessor Solution
Operator Overloading solution
Overload Operators solution
Attending workshops solution
C++ Class Template Specialization solution
C++ Variadics problem solution
Bit Array solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes