View Code of Problem 49

#include<bits/stdc++.h> 
using namespace std;
int main()
{
	int n, f1, f2;
	cin >> n;
	int a[n], max=-1, min=100000;
	for(int i=0; i<n; i++)
	{
		cin >> a[i];
	}
	for(int i=0; i<n; i++)
	{
		if(a[i] < min)
		{
			min = a[i];
			f1 = i;
		}
			
	}
	for(int i=0; i<n; i++)
	{
		if(a[i] > max)
		{
			max = a[i];
			f2 = i;
		}
			
	}
	swap(a[0], a[f1]);
	swap(a[n-1], a[f2]);
	for(int i=0; i < n-1; i++)
	{
		if(i == 0)
			printf("%d", a[i]);
		else
		    printf(" %d", a[i]);
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 49