Sorting is often useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses also.

Challenge 
Given a list of unsorted numbers, can you find the numbers that have the smallest absolute difference between them? If there are multiple pairs, find them all.

Input Format 
There will be two lines of input:

  • n - the size of the list
  • array - the n numbers of the list

Output Format 
Output the pairs of numbers with the smallest difference. If there are multiple pairs, output all of them in ascending order, all on the same line (consecutively) with just a single space between each pair of numbers. If there's a number which lies in two pair, print it two times (see sample case #3 for explanation).

Constraints 
10 <= n <= 200000 
-(107) <= x <= (107), where x ∈ array 
array[i] != array[j], 0 <= ij < N, and i != j


水题:维护一个ArrayList和记录当前最小距离的变量miniDist,每当i和i+1两个数的距离和miniDist相等时,把i放到ArrayList里面;当两个数距离小于miniDist时,把ArrayList清空,i放进去,并更新miniDist;最后根据ArrayList输出答案。

代码如下:

 import java.util.*;

 public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] ar = new int[n];
for(int i = 0;i < n;i++)
ar[i]= in.nextInt(); Arrays.sort(ar);
ArrayList<Integer> index = new ArrayList<Integer>();
int miniDis = Integer.MAX_VALUE;
for(int i = 0;i < n-1;i++){
if(ar[i+1]-ar[i] < miniDis){
index.clear();
index.add(i);
miniDis = ar[i+1]-ar[i];
}
else if(ar[i+1]-ar[i]==miniDis)
index.add(i);
}
for(int i:index){
System.out.printf("%d %d ", ar[i],ar[i+1]);
}
System.out.println(); }
}

【HackerRank】Closest Numbers的更多相关文章

  1. 【HackerRank】Missing Numbers

    Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...

  2. 【HDU3117】Fibonacci Numbers

    [HDU3117]Fibonacci Numbers 题面 求斐波那契数列的第\(n\)项的前四位及后四位. 其中\(0\leq n<2^{32}\) 题解 前置知识:线性常系数齐次递推 其实后 ...

  3. 【CF55D】Beautiful numbers(动态规划)

    [CF55D]Beautiful numbers(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...

  4. 【CF628D】Magic Numbers 数位DP

    [CF628D]Magic Numbers 题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).$a,b<10^{2 ...

  5. 【CF55D】Beautiful numbers

    [CF55D]Beautiful numbers 题面 洛谷 题解 考虑到如果一个数整除所有数那么可以整除他们的\(lcm\),而如果数\(x\)满足\(x\bmod Lcm(1,2...,9)=r\ ...

  6. 【POJ1470】Closest Common Ancestors

    Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For e ...

  7. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  8. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  9. 【HackerRank】Find the Median(Partition找到数组中位数)

    In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific informati ...

随机推荐

  1. 我们可以用JAX-WS轻松实现JAVA平台与其他编程环境(.net等)的互操作

    在 JAX-WS中,一个远程调用可以转换为一个基于XML的协议例如SOAP,在使用JAX-WS过程中,开发者不需要编写任何生成和处理SOAP消息的代码.JAX-WS的运行时实现会将这些API的调用转换 ...

  2. python升级后pip 不可用 卸载pip

    python版本由2.6升级到2.7之后,用pip提示报错 找了一下原因,网上的版本很多.弄来弄去比较麻烦 来点简单粗暴的 1.卸载pip yum remove python-pip 2.下载 cur ...

  3. setTimeout里无法调用鼠标事件的event

    问题的由来是下面这段代码: middleOnmouseLeave: function (event) { setTimeout(function () { event.currentTarget.st ...

  4. 使用PHP创建一个socket服务端

    与常规web开发不同,使用socket开发可以摆脱http的限制.可自定义协议,使用长连接.PHP代码常驻内存等.学习资料来源于workerman官方视频与文档. 通常创建一个socket服务包括这几 ...

  5. CodeIgniter框架——CI的执行流程

    应用程序流程图 CodeIgniter执行流程 源码分析——CI到底做了些什么 (由welcome的例子出发——讲解index.php——讲解CodeIgniter.php) (load_class的 ...

  6. SQL获取某个时间字符串里的月和日,获取某天是周几

    select datename(weekday,'2016-11-4') as '周' select convert(varchar,datepart(month,'2016-11-4')) as ' ...

  7. C语言-数组篇

    C语言数组 一.数组的概念 用来存储一组数据的构造数据类型 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. 二.数组的定义 格式: 类型 数组名[元素个数] ...

  8. 【Python之路】第十三篇--DOM

    文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...

  9. add jars、add external jars、add library、add class folder的区别

    add external jars = 增加工程外部的包add jars = 增加工程内包add library = 增加一个库add class folder = 增加一个类文件夹 add jar是 ...

  10. $(document).ready() $(window).load 及js的window.onload

    1.$(document).ready()  简写为$(function(){}) DOM结构绘制完成执行,而无需等到图片或其他媒体下载完毕. 2.$(window).load  在有时候确实我们有需 ...