Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Solution {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int[] array = new int[n];
- sc.nextLine();
- String[] nums = sc.nextLine().split(" ");
- for (int i = 0; i < n; i++) {
- array[i] = Integer.parseInt(nums[i]);
- }
- int[] sortedarr= selSort(array);
- int[] cftrue = marvel(sortedarr);
- System.out.println(cftrue[0]+" "+ cftrue[1]);
- }
- public static int[] marvel(int[] arr) {
- //Finds the closest pair in the sorting list
- int[] cf = {(arr[0]), (arr[arr.length-1])};
- int globalcf = (arr[arr.length-1] - arr[0]);
- for(int i=(arr.length-1);i>0;i--){
- if(globalcf > (arr[i]-arr[i-1])) {
- cf[0]=arr[i-1];
- cf[1]=arr[i];
- globalcf=(arr[i]-arr[i-1]);
- }
- if(globalcf==0) {
- return(cf);
- }
- }
- return cf;
- }
- public static int[] selSort(int[] arr) {
- //Selection sort algorithm helper method
- int temp;
- for(int i=0; i<arr.length -1; i++) {
- int min = 0;
- for(int j=i+1; j<arr.length; j++) {
- if(arr[j] < arr[i]) {
- min= arr[j];
- temp = arr[i];
- arr[i]=arr[j];
- arr[j]=temp;
- }
- }
- }
- return arr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement