Skip to content

Latest commit

 

History

History
23 lines (13 loc) · 577 Bytes

Convert array into Zig-Zag fashion.md

File metadata and controls

23 lines (13 loc) · 577 Bytes
void zigZag(int n, vector<int> &arr) {
       
        // odd position must have greater valye than neighbouring index
        
        for(int i=0;i<n-1;i++)
        {
            if(i%2==0 && arr[i]>arr[i+1])
            
                swap(arr[i],arr[i+1]);

            
            else if(i%2==1 && arr[i+1]>arr[i])
                swap(arr[i],arr[i+1]);
            
        }
        
        
    }