Open In App

Java 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 c e . 

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.

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 ‘ C. Since current relation is ‘‘ 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 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:




// Java program to sort an array 
// in Zig-Zag form 
import java.util.Arrays; 
  
class Test 
    static int arr[] = new int[]{4, 3, 7
                                 8, 6, 2, 1}; 
      
    // Method for zig-zag conversion 
    // of array 
    static void zigZag() 
    
        // Flag true indicates relation "<" 
        // is expected, else ">" is expected. 
        // The first expected relation is "<" 
        boolean flag = true
          
        int temp =0
      
        for (int i=0; i<=arr.length-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 
                    temp = arr[i]; 
                    arr[i] = arr[i+1]; 
                    arr[i+1] = temp; 
                
                  
            
  
            // ">" 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 
                    temp = arr[i]; 
                    arr[i] = arr[i+1]; 
                    arr[i+1] = temp; 
                
            
  
            // flip flag 
            flag = !flag; 
        
    
      
    // Driver code
    public static void main(String[] args) 
    
        zigZag(); 
        System.out.println(Arrays.toString(arr)); 
    

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!


Article Tags :