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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:41:20+00:00 2026-05-26T18:41:20+00:00

I am supposed to create a O(n log(n)) algorithm that checks if sum of

  • 0

I am supposed to create a O(n log(n)) algorithm that checks if sum of 2 numbers in a int[] == given number.

eg. Given [1,4,7,2,3,4] there will be a sum 8 (1+7) but not 20

The given answer suggested Binary Sort or Merge Sort, but they just gave the merge sort algorithm without the logic processing this particular requirement. Then another answer was:

Suppose x is the sum we want to check, z is the set of elements in
this array: The following algorithm solves the problem:

  1. Sort the elements in S.
  2. Form the set S’ = {z : z = x − y for some y ∈ S}.
  3. Sort the elements in S’.
  4. If any value in S appears more than once, remove all but one instance. Do the same for S’.
  5. Merge the two sorted sets S and S’.
  6. There exist two elements in S whose sum is exactly x if and only if the same value appears in consecutive positions in the merged output.

To justify the claim in step 4, first observe that if any value
appears twice in the merged output, it must appear in consecutive
positions. Thus, we can restate the condition in step 5 as there exist
two elements in S whose sum is exactly x if and only if the same value
appears twice in the merged output. Suppose that some value w appears
twice. Then w appeared once in S and once in S’. Because w appeared in
S’, there exists some y ∈ S such that w = x − y, or x = w + y. Since w
∈ S, the elements w and y are in S and sum to x.

Conversely, suppose that there are values w, y ∈ S such that w + y =
x. Then, since x − y = w, the value w appears in S’. Thus, w is in
both S and S’, and so it will appear twice in the merged output.

Steps 1 and 3 require O(n log n) steps. Steps 2, 4, 5, and 6 require
O(n) steps. Thus the overall running time is O(n log n).

But I don’t really what they meant. In step 2, what are x and y?

But I created by own below, I wonder if its O(n log(n))?

class FindSum {

  public static void main(String[] args) {
    int[] arr = {6,1,2,3,7,12,10,10};
    int targetSum = 20;

    Arrays.sort(arr);
    System.out.println(Arrays.toString(arr));
    int end = arr.length - 1;
    if (FindSum.binarySearchSum(arr, targetSum, 0, end, 0, end)) {
      System.out.println("Found!");
    } else {
      System.out.println("Not Found :(");
    }
  } 

  public static boolean binarySearchSum(int[] arr, int targetSum,
                                        int from1, int end1,
                                        int from2, int end2) {
    // idea is to use 2 "pointers" (simulating 2 arrays) to (binary) search 
    // for target sum
    int curr1 = from1 + (end1-from1)/2;
    int curr2 = from2 + (end2-from2)/2;
    System.out.print(String.format("Looking between %d to %d, %d to %d: %d, %d", from1, end1, from2, end2, curr1, curr2));
    int currSum = arr[curr1] + arr[curr2];
    System.out.println(". Sum = " + currSum);

    if (currSum == targetSum) { 
      // base case
      return true;
    } else if (currSum > targetSum) { 
      // currSum more than targetSum
      if (from2 != end2) { 
        // search in lower half of 2nd "array"
        return FindSum.binarySearchSum(arr, targetSum, from1, end1, from2, curr2 - 1);
      } else if (from1 != end2) { 
        // search in lower half of 1st "array" (resetting the start2, end2 args)
        return FindSum.binarySearchSum(arr, targetSum, from1, curr1 - 1, 0, arr.length - 1);
      } else {
        // can't find
        return false;
      }
    } else {
      // currSum < targetSum
      if (from2 != end2) { 
        // search in upper half of 2nd "array"
        return FindSum.binarySearchSum(arr, targetSum, from1, end1, curr2 + 1, end2);
      } else if (from1 != end2) { 
        // search in upper half of 1st "array" (resetting the start2, end2 args)
        return FindSum.binarySearchSum(arr, targetSum, curr1 + 1, end1, 0, arr.length - 1);
      } else {
        // can't find
        return false;
      }
    }
  }

}
  • 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-26T18:41:21+00:00Added an answer on May 26, 2026 at 6:41 pm

    Similar to @user384706, however you can do this with in O(n).

    What they say is the following:
    S=[1,4,7,2,3,4]

    Add these to a HashSet, ideally TIntHashSet (but the time complexity is the same)

    int total = 9;
    Integer[] S = {1, 4, 7, 2, 3, 4, 6};
    Set<Integer> set = new HashSet<Integer>(Arrays.asList(S));
    for (int i : set)
        if (set.contains(total - i))
            System.out.println(i + " + " + (total - i) + " = " + total);
    

    prints

    2 + 7 = 9
    3 + 6 = 9
    6 + 3 = 9
    7 + 2 = 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an application that is supposed to create products (like shipping insurance
Let's assume, we create a stored procedure that is supposed to retrieve customer details
I have a trigger that supposed to update log out time[generate random log out
I read on other threads that this does not work: <h:commandButton value=Create New Account
I'm supposed to create a simple rule engine in C#. Any leads on how
I am working on an assignment for networking where we are supposed to create
everytime my page loads, im supposed to create a datatable (also a jquery plugin)
I want to create a very basic 3D modeling tool. The application is supposed
I'm trying to create an installer-like application. Here is what its supposed to do:
I have a simple Log model, that records the fact of calling controller's action.

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.