forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zig-zag_array_unsorted.cpp
55 lines (48 loc) · 1.08 KB
/
zig-zag_array_unsorted.cpp
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
50
51
52
53
54
55
/*
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.
So, we will take array and its size as parameters, run a loop from i to size of the array
and swap every pair of element using swap function. At the end of loop, we will print the array.
*/
#include <bits/stdc++.h>
using namespace std;
/*helper function that converts given array in zig-zag form*/
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 n;
cout<<"Enter size of array : ";
cin>>n;
int a[n];
cout<<"Enter array : "<<endl;
for(int i=0; i<n; i++)
cin>>a[i];
zigzag(a,n);
return 0;
}
/*
Sample Input/Output:
Input: Enter size of array : 7
Enter array :
4
3
7
8
6
2
1
Output:Zig-Zag array is : 4 7 3 6 8 1 2
Time Complexity : O(n)
Space Complexity : O(1)
*/