In this post, we will solve the Zig Zag Sequence HackerRank Solution. This problem (Zig Zag Sequence) is a part of the HackerRank Problem Solving series.
Problem: https://www.hackerrank.com/challenges/three-month-preparation-kit-zig-zag-sequence/problem
Zig Zag Sequence Hacker Rank Solution
Problem solution in Python programming:
def findZigZagSequence(a, n): a.sort() mid = int(n/2) # change 1 a[mid], a[n-1] = a[n-1], a[mid] st = mid + 1 ed = n - 2 # change 2 while(st <= ed): a[st], a[ed] = a[ed], a[st] st = st + 1 ed = ed - 1 # change 3 for i in range (n): if i == n-1: print(a[i]) else: print(a[i], end = ' ') return
Problem solution in C++ programming:
void findZigZagSequence(vector < int > a, int n){ sort(a.begin(), a.end()); int mid = (n )/2; swap(a[mid], a[n-1]); int st = mid + 1; int ed = n - 2; while(st <= ed){ swap(a[st], a[ed]); st = st + 1; ed = ed - 1; } for(int i = 0; i < n; i++){ if(i > 0) cout << " "; cout << a[i]; } cout << endl; }
Leave a Reply