An array of N numbers is passed as the input. The program
must sort the odd numbers and even numbers separately in ascending order. The
odd and even numbers must retain their original odd even slots in the input.
Input Format:
The first line contains N indicating the count of numbers
in the array.
The second line contains the N array elements separated
by a space.
Output Format:
The first line
contains the N sorted array elements separated by a space.
Boundary
Conditions:
- 2 <= N <= 100
Example Input/Output 1:
Input:
9
169 181
298 16 147 263 102 155 141
Output:
141 147 16 102 155 169 298 181 263
Coding:
#include<stdio.h>
int main()
{
int n,i,j=0,k=0,t=0,x,t1=0;
scanf("%d",&n);
int a[n],b[100],c[100];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
b[j]=a[i];
j++;
}
else
{
c[k]=a[i];
k++;
}
}
for(i=0;i<j;i++)
{
for(x=i+1;x<j;x++)
{
if(b[i]>b[x])
{
t=b[i];
b[i]=b[x];
b[x]=t;
}
}
}
for(i=0;i<k;i++)
{
for(x=i+1;x<k;x++)
{
if(c[i]>c[x])
{
t1=c[i];
c[i]=c[x];
c[x]=t1;
}
}
}
int s=0,s1=0;
for(i=0;i<n;i++)
{
if(a[i]%2!=0)
{
while(s<k)
{
printf("%d\t",c[s]);
s++;
break;
}
}
else
{
while(s1<j)
{
printf("%d\t",b[s1]);
s1++;
break;
}
}
}
}
No comments:
Post a Comment