• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
April 11, 2022 |11.1K Views
Convert array into Zig-Zag fashion | SDE Sheet | Arrays
  Share  8 Likes
Description
Discussion

Given an array of DISTINCT elements, rearrange the elements of array in zig-zag fashion in O(n) time. The converted array should be in form a < b > c < d > e < f. Compare 2 elements at a time and accordingly place the larger element or the smaller element. 

Example: 

Input: {4, 3, 7, 8, 6, 2, 1} 

Output: {3, 7, 4, 8, 2, 6, 1} 

As you can see 3 < 7 > 4 < 8 > 2 < 6 > 1 

Check out the video to see how we implement this!! 

Next video: https://www.geeksforgeeks.org/videos/rearrange-array-alternatively/ 

Practice Problem: https://practice.geeksforgeeks.org/problems/convert-array-into-zig-zag-fashion1638/1 

Article: https://www.geeksforgeeks.org/convert-array-into-zig-zag-fashion/ 

SDE Sheet: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More