There is some problem with for loop, it doesnt print all i values if keep a cout statement. Please help me out


#1

/**

  • Definition for an interval.
  • struct Interval {
  • int start;
    
  • int end;
    
  • Interval() : start(0), end(0) {}
    
  • Interval(int s, int e) : start(s), end(e) {}
    
  • };
    */
    bool compareInterval(Interval i1, Interval i2)
    {
    return (i1.start<i2.start);
    }

vector Solution::merge(vector &A)
{
vector output;
int n=A.size();
int i,flag=0;
if(n<2)
return A;

for(int i=0;i<n;i++)            // swapping pair if inconsistensy
{
    if(A[i].start > A[i].end)
        swap(A[i].start,A[i].end);
}

sort(A.begin(),A.end(),compareInterval);        //sorting based on starting time

Interval current_val;
current_val.start=A[0].start;               //creating initial values
current_val.end = A[0].end;

for(i=1 ; i<=n-1 ;i++)
{
    //cout<<i;
    if(current_val.end>= A[i].start)
    {
        current_val.start = min(current_val.start,A[i].start);
        current_val.end = max(current_val.end,A[i].end);
    }
    
    
    if(current_val.end < A[i].start)
    {
        output.push_back({current_val.start,current_val.end});
        current_val.start = A[i].start;
        current_val.end = A[i].end;
       
        if(i=n-1)
            flag = 1;
    }


    
}

if(flag == 0)
{
    output.push_back({current_val.start,current_val.end});
}

return output;

}


#2

the function compareInterval has two parameter,
but while sorting the function lacked the parameters