-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwappingInsideArray
More file actions
72 lines (63 loc) · 896 Bytes
/
SwappingInsideArray
File metadata and controls
72 lines (63 loc) · 896 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int starts, ends , temp, pop,j=0;
temp = a[0];
a[0] = 0;
ends = n-1;
starts = 1;
while(j!=n-1){
pop = a[ends];
a[ends] = temp;
j++;
if(j==n-1){
break;
}
temp = a[starts];
a[starts]=pop;
ends--;
starts++;
j++;
}
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}
Input format:
n
1
2
.
.
.
n values
Make the first element zero and place the first element at the last. And the last element to the second. And the second element to the second last. Similarly have to proceed.
Input sample:
5
1
2
3
4
5
Output:
0 5 4 2 1
Input Sample:
7
1
2
3
4
5
6
7
Output:
0 7 6 5 3 2 1