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.
Constraints
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 maximum number of non-overlapping workshops the student can attend.

hackerrank attending workshops problem solution in c++ programming


Attending workshops problem solution in c++ programming | HackerRank

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