Explore BrainMass

Explore BrainMass

    Find two integers in a set that sum to given value.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Suppose you are given a set P of integers and another integer x. We wish to use a O(n*lg n) algorithm to decide whether there are 2 integers in P whose sum equals to x. Show your algorithm. You can use pseudo code or verbose description to explain your algorithm.

    If you need to use any well known standard algorithm in your solution, you need not give details of that, just mention how that is helping you in devising solution for the given problem.

    Note that there is no restriction on integers in set P and integer x, that is, we are not restricting ourselves to positive or negative integers.

    © BrainMass Inc. brainmass.com October 10, 2019, 5:16 am ad1c9bdddf

    Solution Preview

    We can use following steps to achieve the goal.

    Step 1: Sort the given set P using Merge Sort algorithm, that has the time complexity (average and worst-case performance) of O(n*lg n), where n is the number of elements in set P.

    Let us say that we sorted the given set in increasing order. After this step, when we are referring to set P, we mean the sorted set P and not the original set P.

    Step 2: Now we go over the sorted set elements, current element referred to as Pi, from smallest to the largest ...

    Solution Summary

    Solution proceeds in conversational style and instead of giving a flat pseudocode, it gives verbose algorithm mixed with complexity analysis explanations. Idea is to help the reader understand the logic that he can express in his own words.