Advertisement
Guest User

Marvel

a guest
Sep 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Solution {
  5.  
  6.    public static void main(String[] args) {
  7.        Scanner sc = new Scanner(System.in);
  8.        int n = sc.nextInt();
  9.        int[] array = new int[n];
  10.        sc.nextLine();
  11.        String[] nums = sc.nextLine().split(" ");
  12.        for (int i = 0; i < n; i++) {
  13.            array[i] = Integer.parseInt(nums[i]);
  14.        }
  15.        int[] sortedarr= selSort(array);
  16.        int[] cftrue = marvel(sortedarr);
  17.        System.out.println(cftrue[0]+" "+ cftrue[1]);
  18.    }
  19.  
  20.  
  21.  
  22.    public static int[] marvel(int[] arr) {
  23.         //Finds the closest pair in the sorting list
  24.         int[] cf = {(arr[0]), (arr[arr.length-1])};
  25.         int globalcf = (arr[arr.length-1] - arr[0]);
  26.         for(int i=(arr.length-1);i>0;i--){
  27.             if(globalcf > (arr[i]-arr[i-1])) {
  28.                 cf[0]=arr[i-1];
  29.                 cf[1]=arr[i];
  30.                 globalcf=(arr[i]-arr[i-1]);
  31.             }
  32.             if(globalcf==0) {
  33.                 return(cf);
  34.             }
  35.         }
  36.         return cf;
  37.    }
  38.  
  39.    public static int[] selSort(int[] arr) {
  40.         //Selection sort algorithm helper method
  41.         int temp;
  42.         for(int i=0; i<arr.length -1; i++) {
  43.             int min = 0;
  44.             for(int j=i+1; j<arr.length; j++) {
  45.                 if(arr[j] < arr[i]) {
  46.                     min= arr[j];
  47.                     temp = arr[i];
  48.                     arr[i]=arr[j];
  49.                     arr[j]=temp;
  50.                 }
  51.             }
  52.         }
  53.         return arr;
  54.    }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement