Advertisement
Guest User

Untitled

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