given an array {5,3,6,2,7,1,4}. sort the odd position elements into descending order and even position elements into ascending order.
O/P : { 7,6,5,4,1,2,3}
MY SOLUTION:
#include <stdio.h>
int fun(void *a ,void *b)
{
return (*(int *)a -*(int *)b );
}
int main(void) {
int value,x,y,i;
scanf("%d",&value);
if(value%2==0)
{
x=y=value/2;
}
else
{
x=(value/2)+1;
y=value/2;
}
int a[x],b[y];
int p=0,k=0;
for(i=0;i<value;i++)
{
if(i==0||i%2==0)
scanf("%d",&a[p++]);
else
scanf("%d",&b[k++]);
}
//printf("%d",b[i]);
qsort(a,p,sizeof(int),fun);
qsort(b,k,sizeof(int),fun);
p--;
for(i=p;i>=0;i--)
{
printf("%d",a[i]);
}
for(i=0;i<k;i++)
{
printf("%d",b[i]);
}
}
O/P : { 7,6,5,4,1,2,3}
MY SOLUTION:
#include <stdio.h>
int fun(void *a ,void *b)
{
return (*(int *)a -*(int *)b );
}
int main(void) {
int value,x,y,i;
scanf("%d",&value);
if(value%2==0)
{
x=y=value/2;
}
else
{
x=(value/2)+1;
y=value/2;
}
int a[x],b[y];
int p=0,k=0;
for(i=0;i<value;i++)
{
if(i==0||i%2==0)
scanf("%d",&a[p++]);
else
scanf("%d",&b[k++]);
}
//printf("%d",b[i]);
qsort(a,p,sizeof(int),fun);
qsort(b,k,sizeof(int),fun);
p--;
for(i=p;i>=0;i--)
{
printf("%d",a[i]);
}
for(i=0;i<k;i++)
{
printf("%d",b[i]);
}
}