Q7 Example prog?

Can you provide a working example program for sorting as asked in this question? I can’t understand what the “correct answer” exactly wants to say.

Hi @divyansh.shekhar,
Could you please share the question, I don’t have it.

Hey @divyansh.shekhar,
please share the question.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

This was the question:

Given an array of N integers. We wish to sort these integers but we must remember what their orignal indices were before sorting.

Correct answer: We may build a vector> data where data[i].first = AR[i] and data[i].second = i and sort this vector using sort(data.begin(), data.end()).

My answer: This cannot be achieved without a custom comparator and two arrays.

Can you please explain the correct answer with a sample program?

hi, @divyansh.shekhar
say you made a vector of pairs {value,indices} and sort this then the vector will be sorted according to value and indices can still be maintained with the value
say vector is {2,0}, {3,1}, {1,2}
after sorting :- {1,2}, {2,0}, {3,1}
so we have the indices even after sorting as sorting is done on basis of first value ( default )
check I’ve coded the same : https://ide.codingblocks.com/s/655563

@divyansh.shekhar hope the doubt is clear

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.