1712B - Woeful Permutation - CodeForces Solution


constructive algorithms greedy number theory

Please click on ads to support us..

Python Code:

def solve():
    n = int(input())
    a = [int(i + 1) for i in range(n)]
    for i in range(n - 2, -1, -2):
        a[i + 1], a[i] = a[i], a[i + 1]
    for i in range(n):
        print(a[i], end=' ')
    print()
    
if __name__ == '__main__':
    for i in range(int(input())):
        solve()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
    long long t,x,y,n;
    cin >> t;
    while (t--){
        cin >> n;
        if (n % 2){
            cout << 1 <<" ";
            for(int i = 2; i <= n; i++){
                if (i % 2 == 0) cout << i+1 <<" ";
                else cout << i-1 <<" ";  
            }
        }
        else {
            for (int i = 1; i <= n; i++){
                if(i % 2) cout << i+1 <<" ";
                else cout << i-1 << " ";
            }
        }   
        cout << endl;     
    }
}


Comments

Submit
0 Comments
More Questions

1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push