|
| 1 | +#include <bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +string ltrim(const string &); |
| 6 | +string rtrim(const string &); |
| 7 | +vector<string> split(const string &); |
| 8 | + |
| 9 | +/* |
| 10 | + * Complete the 'absolutePermutation' function below. |
| 11 | + * |
| 12 | + * The function is expected to return an INTEGER_ARRAY. |
| 13 | + * The function accepts following parameters: |
| 14 | + * 1. INTEGER n |
| 15 | + * 2. INTEGER k |
| 16 | + */ |
| 17 | + |
| 18 | +vector<int> absolutePermutation(int n, int k) { |
| 19 | + // Case when k is 0, return the identity permutation |
| 20 | + if (k == 0) { |
| 21 | + vector<int> permutation(n); |
| 22 | + iota(permutation.begin(), permutation.end(), 1); |
| 23 | + return permutation; |
| 24 | + } |
| 25 | + |
| 26 | + // If n is not divisible by 2 * k, a valid permutation is not possible |
| 27 | + if (n % (2 * k) != 0) |
| 28 | + return {-1}; |
| 29 | + |
| 30 | + vector<int> permutation; |
| 31 | + bool add = true; |
| 32 | + |
| 33 | + // Iterate through numbers and alternate between adding and subtracting k |
| 34 | + for (int i = 1; i <= n; ++i) { |
| 35 | + if (add) { |
| 36 | + permutation.push_back(i + k); |
| 37 | + } else { |
| 38 | + permutation.push_back(i - k); |
| 39 | + } |
| 40 | + |
| 41 | + // Flip `add` every k steps |
| 42 | + if (i % k == 0) { |
| 43 | + add = !add; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return permutation; |
| 48 | +} |
| 49 | + |
| 50 | +int main() |
| 51 | +{ |
| 52 | + ofstream fout(getenv("OUTPUT_PATH")); |
| 53 | + |
| 54 | + string t_temp; |
| 55 | + getline(cin, t_temp); |
| 56 | + |
| 57 | + int t = stoi(ltrim(rtrim(t_temp))); |
| 58 | + |
| 59 | + for (int t_itr = 0; t_itr < t; t_itr++) { |
| 60 | + string first_multiple_input_temp; |
| 61 | + getline(cin, first_multiple_input_temp); |
| 62 | + |
| 63 | + vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp)); |
| 64 | + |
| 65 | + int n = stoi(first_multiple_input[0]); |
| 66 | + |
| 67 | + int k = stoi(first_multiple_input[1]); |
| 68 | + |
| 69 | + vector<int> result = absolutePermutation(n, k); |
| 70 | + |
| 71 | + for (size_t i = 0; i < result.size(); i++) { |
| 72 | + fout << result[i]; |
| 73 | + |
| 74 | + if (i != result.size() - 1) { |
| 75 | + fout << " "; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + fout << "\n"; |
| 80 | + } |
| 81 | + |
| 82 | + fout.close(); |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | +string ltrim(const string &str) { |
| 88 | + string s(str); |
| 89 | + |
| 90 | + s.erase( |
| 91 | + s.begin(), |
| 92 | + find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))) |
| 93 | + ); |
| 94 | + |
| 95 | + return s; |
| 96 | +} |
| 97 | + |
| 98 | +string rtrim(const string &str) { |
| 99 | + string s(str); |
| 100 | + |
| 101 | + s.erase( |
| 102 | + find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), |
| 103 | + s.end() |
| 104 | + ); |
| 105 | + |
| 106 | + return s; |
| 107 | +} |
| 108 | + |
| 109 | +vector<string> split(const string &str) { |
| 110 | + vector<string> tokens; |
| 111 | + |
| 112 | + string::size_type start = 0; |
| 113 | + string::size_type end = 0; |
| 114 | + |
| 115 | + while ((end = str.find(" ", start)) != string::npos) { |
| 116 | + tokens.push_back(str.substr(start, end - start)); |
| 117 | + |
| 118 | + start = end + 1; |
| 119 | + } |
| 120 | + |
| 121 | + tokens.push_back(str.substr(start)); |
| 122 | + |
| 123 | + return tokens; |
| 124 | +} |
0 commit comments