Java Program For Converting Array Into Zig-Zag Fashion Last Updated : 09 Dec, 2022 Comments Improve Suggest changes Like Article Like Report 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} Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. 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: Java // 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! Comment More infoAdvertise with us Next Article Java Program For Converting Array Into Zig-Zag Fashion kartik Follow Improve Article Tags : Java Programs DSA Arrays Amazon Paytm +1 More Practice Tags : AmazonPaytmArrays Similar Reads Program to convert Array to Set in Java Array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In the case of primitives data types, the actual values are stored in contiguous memory locations. In cas 7 min read Program to Convert Stream to an Array in Java A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result. An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition 3 min read Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r 4 min read Program to convert Boxed Array to Stream in Java An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case 3 min read Program to convert Primitive Array to Stream in Java An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case 3 min read Java Program for Menu Driven Sorting of Array In Java, sorting an array consists of arranging the elements in a particular order, such as ascending or descending. This can be achieved using various algorithms like Bubble Sort, Selection Sort, or Insertion Sort. A menu-driven program allows users to select the desired sorting method dynamically. 7 min read Java Program for Block swap algorithm for array rotation Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. Rotation of the above array by 2 will make array Algorithm : Initialize A = arr[0..d-1] and B = arr[d..n-1] 1) Do following until size of A is equal to size of B a) If A is shorter, divide B into Bl and Br such that Br i 4 min read How to Clone a 2D Array With Different Row Sizes in Java? 2D arrays are two-dimensional arrays. These are the simplest forms of multidimensional arrays. Java provides various methods to clone the arrays, but when dealing with 2D arrays having varying sizes, then the process becomes more difficult. Here, we will see different methods to clone 2D arrays with 5 min read Convert ArrayList to Vector in Java There are several ways to convert ArrayList to Vector. We can use a vector constructor for converting ArrayList to vector. We can read ArrayList elements one by one and add them in vector. Approach 1: (Using Vector Constructor) Create an ArrayList.Add elements in ArrayList.Create a vector and pass t 3 min read Java Program to Print matrix in antispiral form Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16}; Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12 3 min read Like