Pergunta de entrevista da empresa Cleartrip

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Resposta da entrevista

Sigiloso

20 de mar. de 2018

Start from both the ends of the array and check if sum of these 2 values is :--> a) Equal to x, then this is the pair. return. b) Less than x, then increment index from left side. and keep checking. c) Greater than x, then decrements index from right side and keep checking for pair. repeat steps a,b,c till both indexes match.