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.
publicstaticList<Integer>absolutePermutation(intn,intk){// Write your code herevarset=newHashSet<Integer>();for(vari=1;i<=n;i++){set.add(i);}varresult=newArrayList<Integer>();for(vari=1;i<=n;i++){varlowerNumber=i-k;varhigherNumber=i+k;if(set.remove(lowerNumber)){result.add(lowerNumber);}elseif(set.remove(higherNumber)){result.add(higherNumber);}else{returnList.of(-1);}}returnresult;}
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 →
Solution in Java using a HashSet