Pregunta de entrevista de Cleartrip

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

Respuesta de la entrevista

Anónimo

20 mar 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.