0% found this document useful (0 votes)
476 views

HackerRank Zig Zag Sequence Problem Solution

This document provides solutions to the HackerRank Zig Zag Sequence problem in Python, Java, and C++. The problem task is to debug code to permute an array into a zig zag sequence, where the first half of elements are in increasing order and the second half are in decreasing order. The solutions include sorting the array, swapping elements to achieve the zig zag pattern, and outputting the result. Sample code is provided and explained for each language.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
476 views

HackerRank Zig Zag Sequence Problem Solution

This document provides solutions to the HackerRank Zig Zag Sequence problem in Python, Java, and C++. The problem task is to debug code to permute an array into a zig zag sequence, where the first half of elements are in increasing order and the second half are in decreasing order. The solutions include sorting the array, swapping elements to achieve the zig zag pattern, and outputting the result. Sample code is provided and explained for each language.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Home  interview prepration kit  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

Learn DSA For Free

20% OFF 30% OFF 25% OFF


Most Popular Content

HackerRank Mini-Max Sum



problem solution
 March 23, 2021
Crafted with  by TemplatesYard | Distributed by Blogger

CLOSE ADS HackerRank Plus Minus


CLOSE ADS
problem solution
 March 23, 2021

HackerRank Time Conversion


problem solution
 March 23, 2021

Problem solution in Python.


HackerRank Diagonal
Code Difference problem solution
 March 23, 2021
def findZigZagSequence(a, n):
a.sort()
HackerRank Simple Array Sum
mid = int(n / 2)
problem solution
a[mid], a[n-1] = a[n-1], a[mid]  March 23, 2021

st = mid + 1
ed = n - 2
while(st <= ed):
a[st], a[ed] = a[ed], a[st]
st = st + 1
ed = ed - 1

for i in range (n):


if i == n-1:
print(a[i])
else:
print(a[i], end = ' ')
return

test_cases = int(input())
for cs in range (test_cases):
n = int(input())
a = list(map(int, input().split()))
findZigZagSequence(a, n)

Problem solution in Java.


Code
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class Main {

public static void main (String[] args) throws


java.lang.Exception {
Scanner kb = new Scanner(System.in);
int test_cases = kb.nextInt();
for(int cs = 1; cs <= test_cases; cs++){

int n = kb.nextInt();
();
int a[] = new int[n];
for(int i = 0; i < n; i++){
a[i] = kb.nextInt();
}
findZigZagSequence(a, n);
}
}

public static void findZigZagSequence(int [] a, int n){


Arrays.sort(a);
int mid = n/2;
int temp = a[mid];
a[mid] = a[n - 1];
a[n - 1] = temp;

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();
}
}

Problem solution in C++.


Code
#include <bits/stdc++.h>
using namespace std;

void findZigZagSequence(vector < int > a, int n){


sort(a.begin(), a.end());
int mid = (n + 1)/2 - 1; //change 1
swap(a[mid], a[n-1]);

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;

for(int cs = 1; cs <= test_cases; cs++){


cin >> n;
vector < int > a;
for(int i = 0; i < n; i++){
cin >> x;
a.push_back(x);
}
findZigZagSequence(a, n);
}
}

Tags: coding problems interview prepration kit

 Facebook  Twitter    

Posted by: YASH PAL


Yash is a Full Stack web developer. he always will to help others. and this approach
takes him to write this page.

You may like these posts

Codechef Test Score Codechef Max minus Min


HackerRank Palindrome problem solution problem solution
Index problem solution  November 05, 2022  November 05, 2022
 November 11, 2022

Post a Comment

You might also like