• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

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

November 17, 2014 by Dhaval Dave

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

Examples:

Input: arr[] = {10, 22, 28, 29, 32, 40}, x = 54
Output: 22 and 32

A simple solution is to consider every pair and keep track of closest pair (absolute difference between pair sum and x is minimum). Finally print the closest pair. Time complexity of this solution is O(n2)

An efficient solution can find the pair in O(n) time. The idea is similar to method 2 of this post. Following is detailed algorithm.

#include <iostream>
#include <climits>
#include <cstdlib>
using namespace std;

// Prints the pair with sum cloest to x
void printClosest(int arr[], int n, int x)
{
    int res_l, res_r;  
    int l = 0, r = n-1, diff = INT_MAX;
    while (r > l)
    {
       if (abs(arr[l] + arr[r] - x) < diff)
       {
           res_l = l;
           res_r = r;
           diff = abs(arr[l] + arr[r] - x);
       }
       if (arr[l] + arr[r] > x)
           r--;
       else
           l++;
    }
    cout <<" The closest pair is " << arr[res_l] << " and " << arr[res_r];
}

int main()
{
    int arr[] =  {10, 22, 28, 29, 32, 40}, x = 54;
    int n = sizeof(arr)/sizeof(arr[0]);
    printClosest(arr, n, x);
    return 0;
}

See Running code at http://ideone.com/q3F4OQ

Similar Articles

Filed Under: Adobe Interview Questions, Amazon Interview Question, Flipkart Interview Questions, Interview Questions, problem Tagged With: Array

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Leetcode: Edit Distance

Count number of ways to reach a given score in a game

Find if two rectangles overlap

Printing intermediate Integers between one element & next element of array

Regular Expression Matching

Length of the longest substring without repeating characters

Find two non repeating elements in an array of repeating elements

Find Percentage of Words matching in Two Strings

The Magic HackerEarth Nirvana solutions Hiring Challenge

Given a string, find the first character which is non-repetitive

strtok()

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

LeetCode : Word Search

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Find if a binary tree is height balanced ?

SAP Off Campus Hiring_ March 2015 Computer Skills

Add Sub Multiply very large number stored as string

Reliance Jio Software Developer Interview Experience

C++ OOPs Part2

Fibonacci Hashing & Fastest Hashtable

Mirror of Tree

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Maximum size of square sub matrix with all 1’s in a binary matrix

Best Java Book | Top Java Programming Book for Beginners

Sort an array according to the order defined by another array

simple sql injection

Common Ancestor in a Binary Tree or Binary Search Tree

Find loop in Singly linked list

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Copyright © 2026 · Genesis Framework · WordPress · Log in