Pergunta de entrevista da empresa Tripadvisor

Find number of pairwise elements in an array with difference k.

Respostas da entrevista

Sigiloso

29 de out. de 2014

Assuming no duplicates, sort an array, iterate through it and find a number a[i]+k, code: public void find(int[]a, int k){ if(a==null||a.length==0)return; if(k == 0){ //since the elements are distinct return; } Arrays.sort(a); for(int i = 0; i =0){ System.out.println(a[i] + " " + a[idx]); } } }

Sigiloso

20 de nov. de 2015

No sorting required.. use hash map. it is O(n) algorithm

Sigiloso

20 de set. de 2018

def find(nums, k): dic = {} res = [] for i in nums: if i not in dic: dic[k+i]=i else: res.append([dic[i],i]) return res nums = [x for x in range(1,11)] print find(nums, 3)