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.
vector<int>absolutePermutation(intn,intk){if(k==0){vector<int>permutation(n,0);for(intindex=0;index<n;index++){permutation[index]=index+1;}returnpermutation;}// we need an even count of k-chucks in order to find an absolute permutationif(k>n/2||n%k!=0||(n/k)%2!=0){return{-1};}vector<int>permutation(n,0);// process two chucks in a single iterationfor(intindexChucks=0;indexChucks<n;indexChucks+=2*k){for(intindexElement=0;indexElement<k;indexElement++){intindex=indexChucks+indexElement;permutation[index]=k+index+1;permutation[k+index]=index+1;}}returnpermutation;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Absolute Permutation
You are viewing a single comment's thread. Return to all comments →
c++ solution: