-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap_array_elements_pairwise.c
74 lines (53 loc) · 1.01 KB
/
swap_array_elements_pairwise.c
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
73
74
#include<stdio.h>
int solution(int *A, int N)
{
int i=0;
int j=1;
int save=A[N-1];
if(N==1){
printf("%d",A[0]);
return A[0];
}
if(N==0){
printf("no element present");
return 0;
}
// if the elements are even ;
if(N%2==0){
while(i<N){
int temp=A[j];
A[j]=A[i];
A[i]=temp;
i=i+2;
j=j+2;
}
}
// if the elements are odd;
else{
while(i<N){
//swapping the elements;
int temp=A[j];
A[j]=A[i];
A[i]=temp;
i=i+2;
j=j+2;
}
A[N-1]=save;
}
for(int k=0;k<N;k++){
printf("%d ",A[k]);
}
return 0;
}
//Your program will be evaluated by this main method and several test cases.
int main()
{
int *A,N,i;
scanf("%d",&N);
A = (int *)malloc(sizeof(int)*N);
for(i=0;i<N;i++)
{
scanf("%d",&A[i]);
}
solution(A,N);
}