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 6713707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:24:42+00:00 2026-05-26T08:24:42+00:00

Possible Duplicate: checking if 2 numbers of array add up to I My Problem

  • 0

Possible Duplicate:
checking if 2 numbers of array add up to I

My Problem is i have a given number no=10; assume and a Array A the i have to find those no who’s difference is the given no..
Ex:- A[5]-A[3]=10; then Print print(A[5]); Print(A[5])
I have a algorithm that do it in O(n^2) time but we need something better…
My intuition is may be do something after Shorting the Array….. But How.. because after shorting it also need two loop to check that condition….
I am little bit confused ……

  • 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-05-26T08:24:43+00:00Added an answer on May 26, 2026 at 8:24 am

    Original answer – O(n log n)

    How about:

    • Sort the array (or a copy). This is O(n log n)
    • Iterate over the array, and for each value x do a binary search for x + no
      • Each binary search is O(log n)
      • Overall O(n log n)

    Overall cost: O(n log n)


    Modification to the above, as per Steve Jessop’s comment:

    FWIW, you don’t have to do a binary search for x + no. As you iterate over the array, you can maintain a second iteration point in advance of the first one. Since the array is sorted, at each step x + no is greater than or equal to what x + no was in the previous step, so a linear search starting at the second iteration point from the previous step only ever goes forward and hence is O(n) total for the whole outer iteration, even though it isn’t O(1) for each of the n steps.

    This still requires a sort beforehand, which is O(n log n) though.


    Best answer IMO (simple to implement and O(n))

    EDIT: Or, of course, build a HashSet to make the containment check even quicker (assuming that creating the set is amortized O(n) and lookups are O(1)):

    HashSet<int> values = new HashSet<int>(array);
    foreach (int value in array)
    {
        if (values.Contains(value + no))
        {
            // Found a match
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Checking for an SQL result in VB.NET I have login form which
Possible Duplicate: Java string comparison? I have encounter the following problem, I have an
Possible Duplicate: Android (method for checking an arrays for repeated numbers?) I've just asked
Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given
Possible Duplicate: Solution for overloaded operator constraint in .NET generics I have a problem
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: How do I find the last modified file in a directory in
Possible Duplicate: Ruby 1.9.2 and Rails 3 cannot open rails console I have already
Possible Duplicate: Why does C have a distinction between -> and . ? What
Possible Duplicate: Regex for checking if a string has mismatched parentheses? I'm trying to

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.