Zoho interview question Question and Answer
Given an array of integers, rearrange the array in such a way that the first element is first maximum and second element is first minimum.
Example —> Input: {1, 2, 3, 4, 5, 6, 7} | Output : {7, 1, 6, 2, 5, 3, 4}
C++ Program
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[100],n=0,temp,max,min;
cin >> n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n";
for(int i=0 ;i<n;i++)
{
for( int j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
max=0;
min=n-1;
for(int i=1 ;i<=n;i++)
{
if(i%2==0)
{
cout<<a[min--];
}
else
{
cout<<a[max++];
}
}
getch();
return 0;
}
Happy Coding!!!!
Comments
Post a Comment