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 minIndex=0;
        int maxIndex=0;
        int a[] = new int[n];
        for (int i = 0; i <n ; i++) {
            a[i]=scanner.nextInt();
            if (a[i] > a[maxIndex]) {
                maxIndex=i;
            } else if (a[i] < a[minIndex]) {
                minIndex=i;
            }
        }
        int temp=a[minIndex];
        a[minIndex]=a[0];
        a[0]=temp;
        temp = a[maxIndex];
        a[maxIndex] = 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