interviewstreet上的一个coding题

Problem Statement:
Given N numbers , [N<=10^5] we need to count the total pairs of numbers that have a difference of K. [K>0 and K<1e9]


Input Format:
1st line contains N & K (integers).
2nd line contains N numbers of the set. All the N numbers are assured to be distinct.
Output Format:
One integer saying the no of pairs of numbers that have a diff K.

Sample Input #00:
5 2
1 5 3 4 2

Sample Output #00:
3

 
Sample Input #01:
10 1
363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793 
 
Sample Output #01:
0
 
Note: Java/C# code should be in a class named "Solution"
Read input from STDIN and write output to STDOUT. 

------------------------code-----------------------------------------

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner br=new Scanner(System.in);
int N = br.nextInt();
int K = br.nextInt();
int[] array = new int[N];
int count = 0;
Map map = new HashMap();
for (int i = 0 ; i < N ; i++   ){
array[i] = br.nextInt();
if(map.get(array[i])==null){
map.put(array[i],1);
}
else{
map.put(array[i],map.get(array[i])+1);
}
}
for(int i = 0 ; i < N ; i++   ){
if(map.get(array[i]+K)!=null){
count+=map.get(array[i]+K);
}
}
System.out.println(count);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值