Problem statement
Write a program to sort out the maximum terms in odd position and minimum terms in even position.
Solution
#include<stdio.h>
#include<stdlib.h>
int main()
{
int count,n,t,i,j,d,c;
scanf("%d",&n);
int a[n],b[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
b[i]=a[i];
}
for(i=0,j=n-1,count=0;count<n;count++)
{
if(count%2==0)
{
printf("%d",b[j]);
j--;
}
else
{
printf("%d ",b[i]);
i++;
}
}
}
Input 1
7
1 2 3 4 5 6 7
Output 1
7 1 6 2 5 3 4
Input 2
5
100 12 15 80 5
Output 2
100 5 80 12 15
Program Explanation
- This is a program asked in zoho interview round.
- After declaring the header files for accessing input and output functions. Input integers are getting through the scanf function in an array.
- Input integers are sorted out in an ascending order using nested for loop and it is stored in an another array.
- Finally printing the output based on the desired aspects in the odd and even positions using count method and remaining variables.