Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Sort the input array ascending. For element at index i
do difference with element at index i+1
. Find the minimum difference. Check all elements and find all pair that have this minimum difference. Print found elements ascending.
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; public class ClosestNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); long[] numbers = new long[n]; String[] input = scanner.nextLine().trim().split("\\s+"); for (int i = 0; i < n; i++) { numbers[i] = Long.parseLong(input[i]); } Arrays.sort(numbers); long min = Long.MAX_VALUE; List<Long> result = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { long diff1 = numbers[i] - numbers[i + 1]; if (diff1 > 0) { if (diff1 < min) { result.clear(); result.add(numbers[i]); result.add(numbers[i + 1]); min = diff1; } else if (diff1 == min) { result.add(numbers[i]); result.add(numbers[i + 1]); } } long diff2 = numbers[i + 1] - numbers[i]; if (diff2 > 0) { if (diff2 < min) { result.clear(); result.add(numbers[i]); result.add(numbers[i + 1]); min = diff2; } else if (diff2 == min) { result.add(numbers[i]); result.add(numbers[i + 1]); } } } Collections.sort(result); StringBuilder sb = new StringBuilder(); for (long l : result) { sb.append(l); sb.append(" "); } System.out.println(sb.toString().trim()); scanner.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
'use strict'; const processData = input => { let lines = input.split('\n'); let array = lines[1].split(' ').map(i => parseInt(i)).sort((a, b) => a - b); let minDiff = 1000000000000000000; for(let i = 0; i < array.length - 1; i++) { let diff = array[i + 1] - array[i]; if(diff < minDiff) { minDiff = diff; } } let elements = []; for(let i = 0; i < array.length - 1; i++) { let diff = array[i + 1] - array[i]; if(diff == minDiff) { elements.push(array[i]); elements.push(array[i + 1]); } } elements = elements.sort((a, b) => a - b); console.log(elements.join(" ")); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 |
import scala.io.Source object ClosestNumbers extends App { val lines = Source.stdin.getLines().toList val array = lines(1).split(" ").map(_.toInt) val sortedArray = array.sorted val pairs = sortedArray.sliding(2).toList val minDifference = pairs.map(arr => arr(1) - arr(0)).min val elements = pairs.filter(arr => arr(1) - arr(0) == minDifference).flatten.sorted println(elements.mkString(" ")) } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
tests = gets.to_i tests.times do arr = gets.to_s.split.map{|s| s.to_i}.sort{|a, b| a <=> b} min_diff = 100000000; for i in 0...(arr.size - 1) diff = arr[i + 1] - arr[i] min_diff = diff if(diff < min_diff) end elements = [] for i in 0...(arr.size - 1) diff = arr[i + 1] - arr[i] if(diff == min_diff) elements << arr[i] elements << arr[i + 1] end end elements.sort!{|a, b| a <=> b} puts elements.join(" ") end |