We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/* * Complete the 'lilysHomework' function below. * * The function is expected to return an INTEGER. * The function accepts INTEGER_ARRAY arr as parameter. */void_print(constvector<int>&v){for(autoe:v){cout<<e<<" ";}cout<<endl;}int_count(vector<int>&rx){intn=rx.size();inti=0;intj;intcount=0;while(i<n){while((j=rx[i])!=i){swap(rx[i],rx[j]);count++;}i++;}returncount;}intlilysHomework(vector<int>arr){intn=arr.size();// _print(arr);vector<int>ix(n);iota(ix.begin(),ix.end(),0);// _print(ix); sort(ix.begin(),ix.end(),[&arr](inti,intj){returnarr[i]<arr[j];});// _print(ix);vector<int>rx(n);// rankfor(size_ti=0;i<n;++i){rx[ix[i]]=i;}// _print(rx);vector<int>rrx(n);// reversed rankfor(size_ti=0;i<n;++i){rrx[i]=n-1-rx[i];}// _print(rrx);intc1=_count(rx);intc2=_count(rrx);returnmin(c1,c2);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
You are viewing a single comment's thread. Return to all comments →