You are viewing a single comment's thread. Return to all comments →
void check_swap(vector<int>& arr, int i, int size, int k) { for (int j = i + 1; j < size; ++j) { if (arr[j] - arr[i] == k) { swap(arr[j], arr[i]); return; } else if(arr[j]-arr[i]>k) { return; } } } vector<int> absolutePermutation(int n, int k) { if(k==0) { vector<int> arr(n); for (int i = 0; i < n; ++i) { arr[i] = i + 1; } return arr; } else if (n % (2 * k) == 0) { vector<int> arr(n); for (int i = 0; i < n; ++i) { arr[i] = i + 1; } for (int i = 0; i < n; ++i) { check_swap(arr, i, n, k); } return arr; } else { return {-1}; } }
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 →