Zigzag Array in Java13 May 2025 | 6 min read In this section, we are going to discuss what is a zigzag array with proper examples. Also, we will create a Java program to convert a simple or ordinary array into a zigzag array and vice-versa. What is a zigzag array?An array is said to be a zigzag array if there are no three consecutive elements of the array are in either increasing or decreasing order. In other words, we can say if there are three elements (say ai, ai+1, ai+2) in the array such that ai < ai+1 < ai+2 or ai > ai+1 > ai+2, the array is not a zigzag array. Hence, we can symbolically represent the condition as follows: Where a, b, c, d, e, and f are the elements of the array. In short, we can say that array elements must follow less than (<) and greater than (>), order alternatively. Every element is either greater than or less than its neighbors. Example of Zigzag ArrayThe arrays A = [4, 2, 6, 3, 10, 1] and B = [8, 4, 6, 3, 5, 1, 10, 9] are zigzag arrays. Because no three consecutive elements of the array are in either increasing or decreasing order. An array C = [5, 2, 3, 6, 1] is not a zigzag array because the element a1 < a2 < a3 i.e. (2 < 3 < 6). If we remove the third element i.e. 6, the array becomes [5, 2, 3, 1] that is a zigzag array. Other zigzag arrays are: ![]() The following arrays are not zigzag arrays because the elements (that are in red color) do not follow the order. ![]() The following two approaches can be used: Approach 1: Simple Solution
For the above approach, the complexity will be O(n log n) and the space complexity is O(1) because no extra space is required. Approach 2: Efficient SolutionThe second approach provides an efficient solution by performing the bubble short in one pass. It converts the array in the zigzag form in O(n) time. For performing the bubble short, we need to maintain a flag that represents the order (less than (<) or greater than (>)). If the two elements are not in that order then swap those two elements, else not. Algorithm
Let's see an example. Consider an array A having three elements A = [5, 3, 1]. Here, 5 > 3 > 1. It means not a zigzag array. Suppose, we are processing 3 and 1 which is 3 > 1. So, if we swap 3 and 1 then the relationship becomes 5 > 1 and 1 < 3 and finally, we get the zigzag array i.e. [5, 1, 3]. Convert an Ordinary Array into Zigzag ArrayConsider an array given below and convert it into a zigzag array. ![]() Initially, the variable flag is set to false. In the following steps, a[i] is the current element and a[i+1] is the next element. Step 1: Keep the first element as it is because the first element does not have its left neighbor and we cannot compare it with the previous element. Here, flag = false and a[i] i.e. 3 is less than a[i+1] i.e. 4. So, we will not swap the elements. Increment i by 1 and set the flag variable to true. ![]() Step 2: Here, flag = true and a[i] i.e. 4 is less than a[i+1] i.e. 6. The condition i>i+1 becomes false. So, we will swap the elements (a[i], a[i+1]). Increment i by 1 and set flag to false. ![]() Step 3: Here, flag = false and a[i] i.e. 4 is greater than a[i+1] i.e. 2. The condition i<i+1 becomes false. So, we will swap the elements (a[i], a[i+1]). Increment i by 1 and set flag to true. ![]() Step 4: Here, flag = true and a[i] i.e. 4 is greater than a[i+1] i.e. 1. The condition i<i+1 becomes false. So, we will not swap the elements (a[i], a[i+1]). Increment i by 1 and set flag to false. ![]() Step 5: Here, flag = false and a[i] i.e. 1 is less than a[i+1] i.e. 6. The condition i<i+1 satisfies. So, we will not swap the elements (a[i], a[i+1]). Increment i by 1 and set flag to true. ![]() Step 6: Here, flag = true and a[i] i.e. 8 is less than a[i+1] i.e. 9. The condition i>i+1 becomes false. So, we will swap the elements (a[i], a[i+1]). Set flag to false. ![]() In the above example, we observe that the flag alternatively becomes true and false. Let's check the array is a zigzag array or not. ![]() The above array is a zigzag array because less-than and greater-than operations are placed alternatively. Let's implement the above algorithm in a Java program. ZigzagArrayExample.java Output: [2, 5, 1, 7, 4, 8, 6] Let's see another example. ZigzagArrayExample.java Output: ![]() In order to convert an array into zigzag form, we can also use the following logic: The above logic also works fine. |
Web data extraction, sometimes referred to as web harvesting or web scraping, is a method for obtaining information from websites. Because of its strong libraries and adaptability, Java is a popular programming language for jobs involving web scraping. In this section, we will discuss web scrapping...
3 min read
In Java, the preconditions are such states or conditions that must be achieved before any particular method or operation can go into action. It helps to check that all the method's arguments are correct and the state of the object or system is suitable for...
5 min read
In today's world, everything is instant and fast forwarded. Online compilers accessible via internet are very useful for programmers who are trying to learn a new programming language but have not ready to install the necessary software setups. In this section, we will discuss about JDoodle...
3 min read
The dictionary meaning of obfuscation is to make something unclear or unintelligible. In programming, obfuscator is used to protect source code from hackers. In this section, we will learn what is code obfuscation, the working of obfuscators, obfuscation tools, its uses. Also, we will learn how...
6 min read
? Java programs frequently need to break down dates and times, particularly those that deal with scheduling, event management, and data analysis. The LocalDate, LocalTime, LocalDateTime, and DateTimeFormatter classes are just a few of Java's classes and methods for managing dates and times. To decompose a date and...
4 min read
In this section, we will learn what is a strobogrammatic numbers and also create Java programs to check if the given number is a strobogrammatic numbers or not. The strobogrammatic numbers Java program is frequently asked in Java coding interviews and academics. Strobogrammatic numbers, an interesting mathematical...
4 min read
Selection statements in Java are control flow statements that allow you to make decisions in your Code based on certain conditions. These statements enable your Java programs to execute different blocks of Code depending on whether specific conditions are true or false. Selection statements are fundamental...
15 min read
We can access data from a specific URL using a Java program. Java provides URL and URLConnection classes to communicate with the URL over a network. These classes have several useful methods to deal with HTTP URLs. In this section, we will discuss how to access data...
3 min read
An unreachable code or statement in Java is a common problem among Java beginners. It is a compile-time error. A lot of novice developers confuse the error with dead code- another Java-related phenomenon. Although the two are similar by manifestation, there are a slight difference between...
4 min read
Difference between Array and ArrayList In Java, array and ArrayList are the well-known data structures. An array is a basic functionality provided by Java, whereas ArrayList is a class of Java Collections framework. It belongs to java.util package. Java Array An array is a dynamically-created object. It serves...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India