• 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

Subset Sum Problem Dynamic programming

SAP Interview Questions

Printing Longest Common Subsequence

Maximum difference between two elements s.t larger element appears after the smaller number

Find the smallest window in a string containing all characters of another string

Check Binary Tree is Binary Search Tree or not

Knight Tour Problem (Graph – Breadth First Search)

Find Percentage of Words matching in Two Strings

Interfaces in C++ (Abstract Classes in C++)

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

Python List

The greedy coins game Dynamic Programming

strtok()

Apriori algorithm C Code Data Mining

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

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

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Possible sizes of bus to carry n groups of friends

LeetCode : Word Search

Mirror of Tree

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Leetcode: Edit Distance

1014 Practice Question of New GRE – Princeton

Code Chef PRGIFT Solution

Find next greater number with same set of digits

Maximum of all subarrays of size k

Find min element in Sorted Rotated Array (Without Duplicates)

Regular Expression Matching

Get Minimum element in O(1) from input numbers or Stack

Copyright © 2023 · Genesis Framework · WordPress · Log in