View Code of Problem 49

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] a = new int[n];
		for(int i = 0;i < n;i++) {
			a[i] = scanner.nextInt();
		}
		int minPos = 0,maxPos = 0;
		int min = a[0];
		for(int i = 1;i <n;i++) {
			if(a[i] < min) {
				min = a[i];
				minPos = i;
			}
		}
		int temp = a[minPos];
		a[minPos] = a[0];
		a[0] = temp;
		int max = a[0];
		for(int i = 1;i <n;i++) {
			if(a[i] > max) {
				max = a[i];
				maxPos = i;
			}
		}
		temp = a[maxPos];
		a[maxPos] = a[n - 1];
		a[n - 1] = temp;
		for(int i = 0;i < n;i++) {
			if(i == n -1) {
				System.out.print(a[i]);
			}
			else {
				System.out.print(a[i] + " ");
			}
		}
	}
}

Double click to view unformatted code.


Back to problem 49