Purchase Solution

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

Not what you're looking for?

Ask Custom Question

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.

Purchase this Solution

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.

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 ...

Purchase this Solution

Free BrainMass Quizzes
C# variables and classes

This quiz contains questions about C# classes and variables.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

C++ Operators

This quiz tests a student's knowledge about C++ operators.

Basic Networking Questions

This quiz consists of some basic networking questions.