using namespace std;
/*
Write a C++ program to rearrange a given sorted array of positive integers .
Note: In final array, first element should be maximum value, second minimum value,
third second maximum value , fourth second minimum value, fifth third maximum and so o n
*/
main()
{
int x[] = {8, 7, 6, 5, 4, 3, 2, 1};
int l,u,v,n=0;
l = sizeof(x)/sizeof(x[0]);
cout<<"\n\tOriginal Array : ";
for(n=0;n<l;n++) cout<<x[n]<<", ";
u=0;
v=l-1;
cout<<"\n\n \tRearraged Array: ";
for(n=0;n<(l/2);n++)
{
cout<<x[u]<<", ";
cout<<x[v]<<", ";
u++;
v--;
}
if(l%2==1) cout<<x[l/2];
}
Comments
Leave a comment