-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZig-zag_Array-using_flag.cpp
More file actions
49 lines (39 loc) · 958 Bytes
/
Copy pathZig-zag_Array-using_flag.cpp
File metadata and controls
49 lines (39 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Approach :
Swap pairs of elements except the first element.
As - keep a[0], swap a[1] with a[2], swap a[3] with a[4], and so on.
*/
#include <bits/stdc++.h>
using namespace std;
void zigzag(int a[] , int n)
{
for(int i=1 ; i<n ; i+=2)
{
if(i < n-1)
swap(a[i] , a[i+1]);
}
cout<<"\nZig-Zag array is : ";
for(int i=0 ; i<n ; i++)
cout<<a[i]<<" ";
}
//Driver Program
int main()
{
int a[] = {4,3,7,8,6,2,1};
int n = sizeof(a)/sizeof(a[0]);
cout<<"Original array is : ";
for(int i=0 ; i<n ; i++)
cout<<a[i]<<" ";
zigzag(a , n);
return 0;
}
/*
Time Complexity : O(n)
Sample Input-Output :
---------------------
Input --> 4,3,7,8,6,2,1
Output: Original array : 4 3 7 8 6 2 1
Zig - Zag array : 4 7 3 6 8 1 2
NOTE : If we want a sorted data we can first sort the array
using sort() and then call the zigzag() function.
*/