-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc11.c
More file actions
46 lines (46 loc) · 728 Bytes
/
c11.c
File metadata and controls
46 lines (46 loc) · 728 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
//Rearrange array in alternating positive & negative items
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int a[n],i=0;
while(i<n)
{
scanf("%d",&a[i]);
i++;
}
int pos[n],neg[n];
int p=0,q=0;
//sepparate +ve and -ve elements
for(i=0;i<n;i++)
{
if(a[i]>=0)
pos[p++]=a[i];
else
neg[q++]=a[i];
}
//merge 2 array alternatively
i=0;
int j=0,k=0;
while(i<p&&j<q)
{
a[k++]=pos[i++];
a[k++]=neg[j++];
}
//fill remaining +ve
while(i<p)
a[k++]=pos[i++];
//fill remaining -ve
while(j<q)
a[k++]=neg[j++];
printf("rearranged array: { ");
for(int m=0;m<n;m++)
{
printf("%d",a[m]);
if(m<n-1)
printf(", ");
}
printf("}");
return 0;
}