652B - z-sort - CodeForces Solution


sortings *1000

Please click on ads to support us..

Python Code:

import sys
from sys import stdin,stdout
from math import *
import math, random, operator
from itertools import product, permutations, combinations
from collections import deque, defaultdict, Counter
input=sys.stdin.readline
def hi():
        n=int(input())
    l=list(map(int,input().split()))
    l.sort()
        res=[]
    p=0
    q=n-1
        for i in range(n):
        if i&1:
                        res.append(l[q])
            q-=1
        else:
                        res.append(l[p])
            p+=1
    print(*res,sep=' ')

if __name__=="__main__":hi()
    

C++ Code:

// Author: Kas-S
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;


int main() {
    ios::sync_with_stdio(0); cin.tie(0);
	int n;
    cin >> n;
    vector<int> a(n);
    for (int &v: a) cin >> v;
    sort(a.begin(), a.end());
    for (int i = 2; i < n; i += 2) {
        swap(a[i], a[i-1]);
    }
    for (int &v: a) cout << v << ' ';
    return 0;
}


Comments

Submit
0 Comments
More Questions

1385C - Make It Good
651A - Joysticks
1474D - Cleaning
1588A - Two Arrays
816A - Karen and Morning
9D - How many trees
918B - Radio Station
15A - Cottage Village
1737B - Ela's Fitness and the Luxury Number
1425H - Huge Boxes of Animal Toys
1737A - Ela Sorting Books
768C - Jon Snow and his Favourite Number
1006C - Three Parts of the Array
81A - Plug-in
276C - Little Girl and Maximum Sum
1738D - Permutation Addicts
1348B - Phoenix and Beauty
186A - Comparing Strings
1281A - Suffix Three
1421C - Palindromifier
1443A - Kids Seating
963A - Alternating Sum
1191B - Tokitsukaze and Mahjong
1612G - Max Sum Array
1459B - Move and Turn
1006F - Xor-Paths
706C - Hard problem
304C - Lucky Permutation Triple
1301C - Ayoub's function
38E - Let's Go Rolling