View Code of Problem 49

package com.company;


import java.util.*;


public class Main {
    public static void main(String[] args){
        int[] a = new int[12];
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        int minIndex = -1;
        int maxIndex = -1;
        for(int i = 0;i<n;i++){
            a[i] = scanner.nextInt();
            if(a[i] > max){
                max = a[i];
                maxIndex = i;
            }

            if(a[i] < min){
                min = a[i];
                minIndex = i;
            }
        }

        scanner.close();

        int  t = a[minIndex];
        a[minIndex] = a[0];
        a[0] = t;

        t = a[maxIndex];
        a[maxIndex] = a[n - 1];
        a[n - 1] = t;

        for(int i = 0;i<n-1;i++){
            System.out.print(a[i] + " ");
        }
        System.out.print(a[n-1]);

    }
}


Double click to view unformatted code.


Back to problem 49