View Code of Problem 49

#include<stdio.h>
#include<math.h>
#include<limits.h>
int main(){
	int n;
	scanf("%d",&n);
	int i;
	int a[n];
	for(i = 0;i < n; i++){
		scanf("%d",&a[i]);
	}
	int min = INT_MAX,max = 0,minIndex = 0,maxIndex = 0;
	for(i = 0;i < n; i++){
		if(a[i]<min){
			min = a[i];
			minIndex = i;
		}
		if(a[i]>max){
			max = a[i];
			maxIndex = i;
		}
	}
	int temp;
	temp = a[minIndex];
	a[minIndex] = a[0];
	a[0] = temp;
	if(maxIndex==0)
		maxIndex = minIndex;
	temp = a[maxIndex];
	a[maxIndex] = a[n-1];
	a[n-1] = temp;
	for(i = 0;i < n; i++){
		if(i!=(n-1))
			printf("%d ",a[i]);
		else
			printf("%d",a[i]);
	}
}

Double click to view unformatted code.


Back to problem 49