Partially Correct Answer due to runtime


#1

here is my code which is giving partial correct due to runtime
/**

  • Definition for an interval.

  • struct Interval {

  • int start;
    
  • int end;
    
  • Interval() : start(0), end(0) {}
    
  • Interval(int s, int e) : start(s), end(e) {}
    
  • };
    */
    #define pb push_back
    vector Solution::merge(vector &A) {
    int n = A.size() ;
    int arr[200005] ;
    memset(arr , 0 , sizeof(arr));
    int maxi = LLONG_MIN ;
    for(int x = 0 ; x<= n - 1 ; x++)
    {
    int a = A[x].start ;
    int b = A[x].end ;
    arr[a]++;
    arr[b]-- ;
    maxi = max(a , maxi);
    maxi = max(b , maxi);
    }
    for(int x = 1 ; x <= maxi ; x++)
    {
    arr[x] = arr[x] + arr[x-1] ;
    }

    int sw = 0 ;
    vector < int > open ,close ;
    for(int x = 0 ;x<= maxi ; x++)
    {
    if(sw == 0)
    {
    if(arr[x] > 0)
    {
    sw = 1 ;
    open.pb(x);
    }
    }
    else
    {
    if(arr[x] == 0)
    {
    close.pb(x);
    sw = 0 ;
    }
    }
    }
    if(open.size() != close.size())
    close.pb(maxi);
    vector < pair < int , int > > ans ;
    for(int x = 0 ; open.size() != 0 && x<= open.size() - 1 ; x++)
    {
    ans.pb( {open[x] , close[x]} );
    }
    for(int x = 0 ; A.size() != 0 && x<= A.size() - 1 ; x++)
    {
    int a = A[x].start ;
    int b = A[x].end ;
    if( a == b && arr[a] == 0 && a != 0 && arr[a-1] == 0)
    {
    ans.pb({a,b});
    }
    }
    sort(ans.begin() , ans.end());
    vector < Interval > f ;
    for(int x = 0 ; ans.size() != 0 && x <= ans.size() - 1 ; x++)
    {
    Interval a ;
    a.start = ans[x].first ;
    a.end = ans[x].second ;
    f.pb(a);
    }

    return f ;
    }