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 hereList<Integer>result=newArrayList<>();if(k==0){for(inti=1;i<=n;i++){result.add(i);}returnresult;}if(n%(2*k)!=0||k>n||n==0){returnCollections.singletonList(-1);}booleanadd=true;// add or substractfor(inti=1;i<=n;i++){if(add){result.add(i+k);}else{result.add(i-k);}if(i%k==0){add=!add;}}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 →
Java solution: