Convert array into Zig-Zag fashion | Practice | GeeksforGeeks

Given an array Arr (distinct elements) of size N. Rearrange the elements of array in zig-zag fashion. The converted array should be in form a < b > c < d > e < f. The relative order of elements is same in the output i.e you have to iterate on the original array only.
Example 1:
Input:
N = 7
Arr[] = {4, 3, 7, 8, 6, 2, 1}
Output: 3 7 4 8 2 6 1
Explanation: 3 < 7 > 4 < 8 > 2 < 6 > 1
Example 2:
Input:
N = 4
Arr[] = {1, 4, 3, 2}
Output: 1 4 2 3
Explanation: 1 < 4 > 2 < 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function zigZag() which takes the array of integers arr and n as parameters and returns void. You need to modify the array itself.
NOTE: In the mentioned complexity, only a unique solution will exist.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 105
0 <= Arri <= 106
 
0
19bcs1314in 6 hours
Java Solution
 
class Solution {
    void swap(int[] arr,int i,int j){
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
    void zigZag(int arr[], int n) {
        // code here
        
        for(int i=0;i<n-1;i++){
            if(i%2==0){
                if(arr[i]>arr[i+1]){
                    swap(arr,i,i+1);
                }
            }else{
                if(arr[i]<arr[i+1]){
                    swap(arr,i,i+1);
                }
            }
        }
    }

}
0
rinnivatsa092 days ago
class Solution {      void zigZag(int arr[], int n) {    for(int i = 0; i < n-1;i++){        if(i%2== 0 && arr[i] > arr[i+1]){          int temp = arr[i];               arr[i] = arr[i+1];               arr[i+1] = temp;                      } else if(i %2 == 1 && arr[i] < arr[i+1]){           int temp = arr[i];               arr[i] = arr[i+1];               arr[i+1] = temp;        }    }    }
}
+1
prashanth7424 days ago
Python:
def zigZag(self,arr, n): # code here for i in range(n-1):           if(i%2==0):               if(arr[i]>arr[i+1]):                   arr[i],arr[i+1]=arr[i+1],arr[i]           else:               if(arr[i]<arr[i+1]):                   arr[i],arr[i+1]=arr[i+1],arr[i]
0
mrnecto5 days ago
class Solution{
public: 
// Program for zig-zag conversion of array
void zigZag(int arr[], int n) {
    bool isLess = true;
    for(int i = 0; i < n - 1; i++)
    {
        if((isLess && arr[i] > arr[i+1]) || (!isLess && arr[i] < arr[i+1]))
            swap(arr[i], arr[i + 1]);
        isLess ^= 1;
    }
}
};
0
vishalja77191 week ago
	void zigZag(int arr[], int n) {
	    // code here
	    bool flag=true;
	    for(int i=0;i<=n-2;i++)
	    {
	        if(flag)
	        {
	            if(arr[i]>arr[i+1])
	            {
	                swap(arr[i],arr[i+1]);
	            }
	        }else
	        {
	            if(arr[i]<arr[i+1])
	            {
	                swap(arr[i],arr[i+1]);
	            }
	        }
	        flag=!flag;
	    }
	    
	}
0
geeta1997cs1 week ago
Python Solution:
 
 def zigZag(self,arr, n): # code here for i in range(1,n):     if i%2!=0:         if arr[i-1]>arr[i]:             arr[i-1],arr[i]=arr[i],arr[i-1]     else:         if arr[i-1]<arr[i]:             arr[i-1],arr[i]=arr[i],arr[i-1] return arr
0
dharmendra bajiya1 week ago
 def zigZag(self,arr, n):     # code here       less_great = 0  # 0 mens less       for i in range(n-1):           if less_great ==0:               if arr[i] > arr[i+1]:                   arr[i], arr[i+1] = arr[i+1], arr[i]                   less_great = 1               else:                   less_great = 1           else:               if arr[i] < arr[i+1]:                   arr[i], arr[i+1] = arr[i+1], arr[i]                   less_great = 0               else:                   less_great = 0          return arr
0
akshayausekar17982 weeks ago
I thing this could be one of the easiest solution.
void zigZag(int arr[], int n) {
	    bool lessThan = true;
	    for(int i = 0;i < n-1; i++){
	        if(lessThan){
	            if(arr[i]>arr[i+1]){
	                int t = arr[i];
	                arr[i] = arr[i+1];
	                arr[i+1] = t;
	            }
	        }else{
	            if(arr[i]<arr[i+1]){
	                int t = arr[i];
	                arr[i] = arr[i+1];
	                arr[i+1] = t;
	            }
	        }
	        lessThan = !lessThan;
	    }
	}
0
bhardwajji2 weeks ago
void zigZag(int arr[], int n) {
	    for(int i=0 ;i<n-1;i++){
	        if( arr[i] > arr[i+1] && i%2 == 0){
	            swap(arr[i] , arr[i+1]);
	        }
	        else if( arr[i] < arr[i+1] && i%2 == 1){
	            swap(arr[i] , arr[i+1]);
	        }
	        
	    }
	}
0
harshilgupta00993 weeks ago
void zigZag(int arr[], int n) {
	   bool smaller=true;
	   bool greater=false;
	   
        for(int i=0;i<n-1;i++){
            if(smaller){
                if(arr[i]>=arr[i+1])
                    swap(arr[i],arr[i+1]);
                smaller=false;
                greater=true;
            }
            else if(greater){
                if(arr[i]<=arr[i+1])
                    swap(arr[i],arr[i+1]);
                    
                greater=false;
                smaller=true;
            }
        }
	}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.