HackerRank Zig Zag Sequence Problem Solution
HackerRank Zig Zag Sequence Problem Solution
HackerRank
CLOSE ADS Zig Zag Sequence problem CLOSE ADS
solution
YASH PAL April 13, 2022
In this HackerRank Zig Zag Sequence problem solution, the task is to debug the existing
code to successfully execute all provided test files.
Search
Given an array of n distinct integers, transform the array into a zig-zag sequence by
permuting the array elements. A sequence will be called a zig-zag sequence if the first k
elements in the sequence are in increasing order and the last k elements are in
decreasing order, where k = (n + 1)/2. You need to find the lexicographically smallest zig-
zag sequence of the given array.
Subscribe To Channel
Programmingoneonone
YouTube 797
st = mid + 1
ed = n - 2
while(st <= ed):
a[st], a[ed] = a[ed], a[st]
st = st + 1
ed = ed - 1
test_cases = int(input())
for cs in range (test_cases):
n = int(input())
a = list(map(int, input().split()))
findZigZagSequence(a, n)
int st = mid + 1;
int ed = n - 2;
while(st <= ed){
temp = a[st];
a[st] = a[ed];
a[ed] = temp;
st = st + 1;
ed = ed - 1;
}
for(int i = 0; i < n; i++){
if(i > 0) System.out.print(" ");
System.out.print(a[i]);
}
System.out.println();
}
}
int st = mid + 1;
int ed = n - 2; //change 2
while(st <= ed){
swap(a[st], a[ed]);
st = st + 1;
ed = ed 1; //change 3
ed = ed - 1; //change 3
}
for(int i = 0; i < n; i++){
if(i > 0) cout << " ";
cout << a[i];
}
cout << endl;
}
int main() {
int n, x;
int test_cases;
cin >> test_cases;
Facebook Twitter
Post a Comment