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

  • SEARCH
  • Home
  • 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 1838234
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:27:07+00:00 2026-05-17T06:27:07+00:00

I am looking for the solution of following algorithm with minimal time and space

  • 0

I am looking for the solution of following algorithm with minimal time and space complexity.

Given two arrays a and b, find all pairs of elements (a1,b1) such that a1 belongs to Array A and b1 belongs to Array B whose sum a1+b1 = k (any integer).

I was able to come up with O(n log n) approach where we will sort one of the array say A and for each of the element b in array B, do binary search on sorted array A for value (K-b) .

Can we improve it any further?

  • 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-17T06:27:07+00:00Added an answer on May 17, 2026 at 6:27 am

    If you have a limit on the maximum possible number (let’s name it M) then you can have a solution in O(M+n).

    Boolean array of false and mark as true all value for element of A. Then for each element b of B check if the element number K-b is marked as true.

    You can improve it if you are using an hash-map instead of a big array. But I would not consider that in this kind of questions hash-map is kind of cheating.

    Anyway it would give you O(n) for insertion and then O(n) for query, O(n) in total.

    EDIT :

    One case where this might be useful.

    • You have un-sorted vectors of size 10^6, so sorting them and doing the match is in O(n log n) with n = 10^6.
    • You need to do this operation 10^6 times (different vectors), complexity of O(n*n*log n).
    • Maximum value is 10^9.

    Using my idea not with boolean but integer (incremented at each run) gives you a complexity of :

    • “O(10^9)” to create the array (also same complexity of space)
    • O(n) at each run, so O(n*n) for the total.

    You are using more space but you’ve increased speed by a factor log(n) ~=20 in this case !

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

Sidebar

Related Questions

I'm looking for an elegant, high performance solution to the following problem. There are
i am looking for opinions if the following problem maybe has a better/different/common solution:
Looking for a solution in bash (will be part of a larger script). Given
I'm looking for an algorithm to solve the following in a reasonable amount of
I am looking for an elegant solution for the following problem. Let's assume we
I'm looking for solution of the following situation. Let's say I have a business
I was looking for a solution for client side printing of reports and pre
Just looking for the first step basic solution here that keeps the honest people
I'm looking for an online solution for generating .ICO files. I'd like the ICO
I'm looking for a quick-and-dirty solution to this, I have to set up 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.