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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:39:07+00:00 2026-05-13T14:39:07+00:00

Taken from Introduction to Algorithms Describe a Θ(n lg n)-time algorithm that, given a

  • 0

Taken from Introduction to Algorithms

Describe a Θ(n lg n)-time algorithm
that, given a set S of n integers and
another integer x, determines whether
or not there exist two elements in S
whose sum is exactly x.

This is my best solution implemented in Java so far:

    public static boolean test(int[] a, int val) {
    mergeSort(a);

    for (int i = 0; i < a.length - 1; ++i) {
        int diff = (val >= a[i]) ? val - a[i] : a[i] - val;

        if (Arrays.binarySearch(a, i, a.length, diff) >= 0) {
            return true;
        }
    }

    return false;
}

Now my 1st question is: Is this a correct solution? From my understanding, mergeSort should perform the sort in O(n lg n), the loop should take O(n lg n) (n for the iteration multiplied by O(lg n) for the binary search, resulting in O(2n lg n), so it should be correct.

My 2nd question is: Are there any better solutions? Is sorting the array essential?

  • 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-13T14:39:08+00:00Added an answer on May 13, 2026 at 2:39 pm

    Your solution seems fine. Yes you need to sort because its a pre requisite for binary search. You can make a slight modification to your logic as follows:

    public static boolean test(int[] a, int val) 
    {
        Arrays.sort(a);
    
        int i = 0;            // index of first element.
        int j = a.length - 1; // index of last element. 
    
        while(i<j)
        {
            // check if the sum of elements at index i and j equals val, if yes we are done.
            if(a[i]+a[j] == val)
                return true;
            // else if sum if more than val, decrease the sum.
            else if(a[i]+a[j] > val)
                j--;
            // else if sum is less than val, increase the sum.
            else
                i++;
        }
        // failed to find any such pair..return false. 
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
i have following problem from book introduction algorithm second edition by MIT university problem
Here is program for memoized version matrix chain multiplication program from Introduction to Algorithms
A quote from Python Programming: An Introduction to Computer Science We could have taken
this is an example taken from Effective C++ 3ed , it says that if
Given this example taken from http://php.net/manual/en/function.crypt.php crypt('rasmuslerdorf', '$2a$07$usesomesillystringforsalt$') Firstly: What is the length that
Idea taken from here - this time you can only use PHP. Is this
This is a function taken from Pro JavaScript techniques that I'm trying to understand,
Example taken from Mozilla's help page <script type="text/javascript"> re = /(\w+)\s(\w+)/; str = "John
The following code is taken from here . I removed all Windows NT part

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.