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(int n, int k) {
vector<int> ans(n,0) ;
for(int i = 1 ; i <= n ; i++){
int pos = i + k ;
if(i-k > 0 && ans[i-k-1] == 0 ){
ans[i-k-1] = i ;
}else if(i+k <= n && ans[i+k-1] == 0){
ans[i+k-1] = i ;
}else{
vector<int> n ;
n.push_back(-1) ;
return n ;
}
}
cout << endl ;
for(int i = 0 ; i < n ; i++){
if(abs(ans[i] - (i+1)) != k || ans[i] == 0 ){
vector<int> n ;
n.push_back(-1) ;
return n ;
}
}
return ans ;
}
Absolute Permutation
You are viewing a single comment's thread. Return to all comments →
Cpp solution O(n) time complexity