Zig Zag

Run Settings
LanguagePython
Language Version
Run Command
def findZigZagSequence(a, n): a.sort() mid = int((n + 1)/2)-1 a[mid], a[n-1] = a[n-1], a[mid] # mid_n = [a[int((n + 1)/2)-1]] # f = [] # b = [] # for i in range(mid): # f.append(a[i]) # for i in range(mid+1,n): # b.append(a[i]) # f.sort() # return f+mid_n+sorted(b,reverse=True) st = mid + 1 ed = n - 1 while(st <= n - mid+1): if a[st] < a[st+1]: a[st], a[st+1] = a[st+1], a[st] st = st + 1 st = 0 while(st <= mid-1): if a[st] > a[st+1]: a[st], a[st+1] = a[st+1], a[st] st = st + 1 return a test_cases = int(input()) for cs in range (test_cases): n = int(input()) a = list(map(int, input().split())) arr = findZigZagSequence(a, n) print(*arr)
Editor Settings
Theme
Key bindings
Full width
Lines