-
Notifications
You must be signed in to change notification settings - Fork 275
/
DivisibleByiii.cpp
69 lines (59 loc) · 1.88 KB
/
DivisibleByiii.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Divisible by i Problem Code: DIVBYISolvedSubmit
// You are given an integer N.
// Construct a permutation P of length N such that
// For all i (1≤i≤N−1), i divides abs(Pi+1−Pi).
// Recall that a permutation of length N is an array where every integer from 1 to N occurs exactly once.
// It can be proven that for the given constraints at least one such P always exists.
// Input Format
// The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follow.
// The only line of each test case contains an integer N - the length of the array to be constructed.
// Output Format
// For each test case, output a single line containing N space-separated integers P1,P2,…,PN, denoting the elements of the array P.
// If there exist multiple such arrays, print any.
// Constraints
// 1≤T≤5⋅104
// 2≤N≤105
// The sum of N over all test cases does not exceed 105.
// Sample Input 1
// 2
// 2
// 3
// Sample Output 1
// 1 2
// 2 1 3
// Explanation
// Test case 1: A possible array satisfying all the conditions is [1,2]:
// For i=1: abs(A2−A1)=abs(2−1)=1 is divisible by 1.
// Test case 2: A possible array satisfying all the conditions is [2,1,3]:
// For i=1: abs(A2−A1)=abs(1−2)=1 is divisible by 1.
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
if(n%2==1)
{
for(int i=n/2+1,j=n/2;j>0;j--,i++)
{
cout<<i<<" "<<j<<" ";
}
cout<<n<<endl;
}
else
{
for(int i=n/2,j=n/2+1;j<=n;j++,i--)
{
cout<<i<<" "<<j<<" ";
}
cout<<endl;
}
}
// your code goes here
return 0;
}