-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathM0009.cpp
More file actions
42 lines (38 loc) · 763 Bytes
/
Copy pathM0009.cpp
File metadata and controls
42 lines (38 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
Problem Statement: https://www.hackerrank.com/challenges/absolute-permutation/problem
*/
#include <iostream>
#include <vector>
#include <numeric>
#include <cmath>
using namespace std;
vector<int> absolutePermutation(int n, int k) {
vector<int> absPerm(n);
if(k == 0){
iota(absPerm.begin(), absPerm.end(), 1);
return absPerm;
}
if(n%(2*k) != 0)
return vector<int>({-1});
for(int i=1 ; i<=n ; i++){
if( (i-1) % k == (i-1) % (2*k) )
absPerm[i-1] = i+k;
else
absPerm[i-1] = abs(i-k);
}
return absPerm;
}
int main()
{
int t, n, k;
vector<int> absPerm;
cin>>t;
for(int i=1 ; i<=t ; i++){
cin>>n>>k;
absPerm = absolutePermutation(n, k);
for(int i=0 ; i<absPerm.size() ; i++)
cout<<absPerm[i]<<" ";
cout<<endl;
}
return 0;
}