Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmC++ Program For Converting Array Into Zig-Zag Fashion

C++ Program For Converting Array Into Zig-Zag Fashion

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

Example:

Input: arr[] = {4, 3, 7, 8, 6, 2, 1} 
Output: arr[] = {3, 7, 4, 8, 2, 6, 1}

Input: arr[] = {1, 4, 3, 2} 
Output: arr[] = {1, 4, 2, 3}

A Simple Solution is to first sort the array. After sorting, exclude the first element, swap the remaining elements in pairs. (i.e. keep arr[0] as it is, swap arr[1] and arr[2], swap arr[3] and arr[4], and so on). 
Time complexity: O(N log N) since we need to sort the array first.

We can convert in O(n) time using an efficient approach. The idea is to use a modified one pass of bubble sort.

  • Maintain a flag for representing which order(i.e. < or >) currently we need.
  • If the current two elements are not in that order then swap those elements otherwise not.

Let us see the main logic using three consecutive elements A, B, C.

Suppose we are processing B and C currently and the current relation is ‘<‘, but we have B > C. Since current relation is ‘<‘ previous relation must be ‘>‘ i.e., A must be greater than B. So, the relation is A > B and B > C. We can deduce A > C. So if we swap B and C then the relation is A > C and C < B. Finally we get the desired order A C B 
Refer this for more explanation.

Below image is a dry run of the above approach:

Below is the implementation of above approach:

C++




// C++ program to sort an array in 
// Zig-Zag form 
#include <iostream> 
using namespace std; 
  
// Program for zig-zag conversion 
// of array 
void zigZag(int arr[], int n) 
    // Flag true indicates relation "<" 
    // is expected, else ">" is expected. 
    // The first expected relation is "<" 
    bool flag = true
  
    for (int i = 0; i <= n - 2; i++) 
    
        /* "<" relation expected */
        if (flag) 
        
            /* If we have a situation like 
               A > B > C, we get A > B < C 
               by swapping B and C */
            if (arr[i] > arr[i+1]) 
                swap(arr[i], arr[i+1]); 
        
  
        /* ">" relation expected */
        else 
        
            /* If we have a situation like 
               A < B < C, we get A < C > B 
               by swapping B and C */
            if (arr[i] < arr[i+1]) 
                swap(arr[i], arr[i+1]); 
        
  
        // flip flag 
        flag = !flag; 
    
  
// Driver code
int main() 
    int arr[] = {4, 3, 7, 8, 6, 2, 1}; 
    int n = sizeof(arr) / sizeof(arr[0]); 
    zigZag(arr, n); 
    for (int i = 0; i < n; i++) 
        cout << arr[i] << " "
    return 0; 


Output: 

3  7  4  8  2  6  1 

Time complexity: O(n) 
Auxiliary Space: O(1) 
 

Please refer complete article on Convert array into Zig-Zag fashion for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments