Skip to content

Commit 4b7e834

Browse files
committed
Add implementation for absolute permutation algorithm
This commit introduces a complete solution for generating absolute permutations based on the input parameters `n` and `k`. It accounts for edge cases such as when `k` is zero or when a valid permutation is impossible. Additionally, helper functions for input trimming and parsing are included.
1 parent ca8c36b commit 4b7e834

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

AbsolutePermutation.cpp

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)