0

There was a problem in cracking the coding interview -part V Question 11.7 and the solution I am trying to implement here is their solution in Java converted to C++.But I am facing issue of segmentation fault.

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
class HtWt
{
    public:
        int ht;
        int wt;
        HtWt(int ht,int wt):ht(ht),wt(wt){}
        HtWt(const HtWt &other)
        {
            this->ht=other.ht;
            this->wt=other.wt;
        }
        bool operator<(const HtWt& obj) const
        {
            cout << __func__ << std::endl;
            return (this->ht<obj.ht && this->wt<obj.wt);
        }
};

typedef vector<HtWt> vHtWt;
typedef vector<vHtWt > vvHtWt;

vHtWt& getSeqWithMaxLen(vHtWt& seq1,vHtWt& seq2)
{
    cout << __func__ << std::endl;
    if(seq1.empty())
        return seq2;
    if(seq2.empty())
        return seq1;
    return (seq1.size() > seq2.size() ? seq1 : seq2);
}

void  LIS(vHtWt& arr,vvHtWt& solutions,int current_index)
{
    cout << __func__ << std::endl;
    if(current_index>arr.size()-1 || current_index<0)
        return;
    cout<<"arr.size()="<<arr.size()<<"current_index = "<<current_index<<endl;
    HtWt cur_element = arr[current_index];
    /* Find longest sequence we can append current_element to */
    vHtWt best_sequence;
    for(int i=0;i<current_index;i++)
    {
        cout<<"inside for loop"<<endl;
        if (arr[i]<cur_element)
            best_sequence = getSeqWithMaxLen(best_sequence,solutions[i]);
            //cout<<"{"<<best_sequence[best_sequence.size()-1].ht<<","<<best_sequence[best_sequence.size()-1].wt<<"}"<<" ";
    }
    /* Append current_element */
    vHtWt new_solution;
    if(!best_sequence.empty())
        new_solution.insert(new_solution.end(),best_sequence.begin(),best_sequence.end());
    new_solution.push_back(cur_element);  
    /* Add to list and recurse */
    solutions[current_index] = new_solution;
    LIS(arr,solutions,current_index+1);
}

vHtWt LIS(vHtWt& arr)
{
    cout << __func__ << std::endl;
    vvHtWt solutions;
    LIS(arr,solutions,0);
    vHtWt best_sequence;
    for(int i=0;i<arr.size();i++)
        best_sequence = getSeqWithMaxLen(best_sequence,solutions[i]);
    return best_sequence;
}

vHtWt getLIS(vHtWt& arr)
{
    cout << __func__ << std::endl;
    // sort the array for either height or weight 
    sort(arr.begin(),arr.end());
    return LIS(arr);
}

int main()
{
    HtWt arr[] = {HtWt(12, 13), HtWt(11, 15), HtWt(9, 20), HtWt(20, 20), HtWt(40, 21), HtWt(8, 42)};
    vHtWt vArr(arr,arr+(sizeof(arr)/sizeof(arr[0])));
    vHtWt result = getLIS(vArr);
    for(int i=0;i<result.size();i++)
        cout<<"{"<<result[i].ht<<","<<result[i].wt<<"}"<<" ";
    cout<<endl;
    return 0;
}

Can somebody please help me tell why the code is crashing at line

HtWt cur_element = arr[current_index];  

The size of arr is 6 and index is 0 , so there should not be a segmentation fault I guess.

$ ./a.out 
getLIS
operator<
operator<
operator<
operator<
operator<
LIS
LIS
arr.size()=6current_index = 0
Segmentation fault: 11
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Peter_pk
  • 147
  • 1
  • 9
  • It looks like stack overflow (since recursion is involved). Can you paste the bt here? Wanna look @ stack depth i.e number of frames. – kspviswa May 15 '15 at 14:45

1 Answers1

0

You are not getting segmentation fault at line

HtWt cur_element = arr[current_index]; but at

solutions[current_index] = new_solution;

because solutions has not been initialized. Change line

vvHtWt solutions; to

vvHtWt solutions(arr.size());

nikhil_vyas
  • 513
  • 5
  • 16