Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:28:08+00:00 2026-06-09T15:28:08+00:00

Given an integer x and a sorted array a of N distinct integers, design

  • 0

Given an integer x and a sorted array a of N distinct integers, design a linear-time algorithm to determine if there exists two distinct indices i and j such that a[i] + a[j] == x

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:28:09+00:00Added an answer on June 9, 2026 at 3:28 pm

    This is type of Subset sum problem

    Here is my solution. I don’t know if it was known earlier or not. Imagine 3D plot of function of two variables i and j:

    sum(i,j) = a[i]+a[j]
    

    For every i there is such j that a[i]+a[j] is closest to x. All these (i,j) pairs form closest-to-x line. We just need to walk along this line and look for a[i]+a[j] == x:

     int i = 0; 
     int j = lower_bound(a.begin(), a.end(), x)  -  a.begin(); 
     while (j >= 0 && j < a.size()  &&  i < a.size())  {  
        int sum = a[i]+a[j]; 
        if (sum == x)   { 
            cout << "found: " << i << " " << j << endl;  
            return;
        }
        if (sum > x)    j--;
        else            i++;
        if (i > j) break;
     }  
     cout << " not found\n";
    

    Complexity: O(n)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two sorted arrays of integers, a and b , and an integer c
You are given two sorted integer arrays, which have some common integers. Any common
Design an algorithm , FindElement(a,p), where a is two-dimensional square array of positive integers
Let A be an array of n positive integers, and k a given integer.
Given a sorted array of n integers, like the following: ary = [3, 5,
Given a sorted array of integers, how can we find a pair of integers
Given an integer k and an sorted array A (can consist of both positive
Given an unsorted integer array, and without making any assumptions on the numbers in
Given a distance (50km) as integer: 50 And a time as string in the
Given this string random_string= '4' i want to determine if its an integer, a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.