Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. /*Name: Mackenize Cooper
  2.  *V#:   00892515
  3.  *Latest Change: 9/24/2018
  4.  *due date: 9/24/2019
  5. */
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9. import java.util.Arrays;
  10.  
  11. public class Solution {
  12.  
  13.    public static void main(String[] args) {
  14.        //Main Method for testing and printing output
  15.        Scanner sc = new Scanner(System.in);
  16.        int n = sc.nextInt();
  17.        int[] array = new int[n];
  18.        sc.nextLine();
  19.        String[] nums = sc.nextLine().split(" ");
  20.        for (int i = 0; i < n; i++) {
  21.            array[i] = Integer.parseInt(nums[i]);
  22.        }
  23.        Arrays.sort(array);
  24.        //int[] sortedarr= selSort(array);
  25.        int[] cftrue = marvel(array);
  26.        System.out.println(cftrue[0]+" "+ cftrue[1]);
  27.    }
  28.  
  29.  
  30.  
  31.    public static int[] marvel(int[] arr) {
  32.         //Finds the closest pair in the sorting list
  33.         int[] cf = {(arr[0]), (arr[arr.length-1])};
  34.         int globalcf = (arr[arr.length-1] - arr[0]);
  35.         for(int i=(arr.length-1);i>0;i--){
  36.             if(globalcf > (arr[i]-arr[i-1])) {
  37.                 cf[0]=arr[i-1];
  38.                 cf[1]=arr[i];
  39.                 globalcf=(arr[i]-arr[i-1]);
  40.             }
  41.             if(globalcf==0) {
  42.                 return(cf);
  43.             }
  44.         }
  45.         return cf;
  46.    }
  47.    //Removed Selection sort algorithm because I opted for the arrays.sort java function.
  48.    /*
  49.    public static int[] selSort(int[] arr) {
  50.         //Selection sort algorithm helper method
  51.         int temp;
  52.         for(int i=0; i<arr.length -1; i++) {
  53.             int min = 0;
  54.             for(int j=i+1; j<arr.length; j++) {
  55.                 if(arr[j] < arr[i]) {
  56.                     min= arr[j];
  57.                     temp = arr[i];
  58.                     arr[i]=arr[j];
  59.                     arr[j]=temp;
  60.                 }
  61.             }
  62.         }
  63.         return arr;
  64.    }
  65.   */
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement