Question

Algorithm problem 2 [2.3-7] Describe a Θ(nlgn) algorithm that, given a set S of n integers...

Algorithm problem

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

Homework Answers

Answer #1

Algorithm: //to find whether in a given set S of n integers and another integer x, there exist two elements in S whose sum is exactly x.
INPUTS:
A[]//ARRAY
X //sum
STEP 1) Sort the array in increasing order.//use mergesort or heapsort which runs in theta(nlogn)
STEP 2) declare two variables :l,r
(a) Initialize variable l to the leftmost index: l = 0
(b) Initialize variable r the rightmost index: r = array_size-1
STEP 3) Loop while l < r //run until l<r
(a) If (A[l] + A[r] == x) then return TRUE//means found
(b) Else if( A[l] + A[r] < x ) then l++
(c) Else r--
STEP 4) No candidates in whole array - return FALSE//MEANS NOT FOUND
total complexity : nlogn+n = theta(nlogn)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Describe an algorithm that, given a set S of n integers and another integer x, determines...
Describe an 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. Your algorithm must be nlogn. Evaluate how long each step in the algorithm takes to demonstrate that it meets this requirement.
a. Find an algorithm for solving the following problem: Given a positive integer n, find the...
a. Find an algorithm for solving the following problem: Given a positive integer n, find the list of positive integers whose product is the largest among all the lists of positive integers whose sum is n. For example, if n is 4, the desired list is 2, 2 because 2 × 2 is larger than 1 × 1 × 1 × 1, 2 × 1 × 1, and 3 × 1. If n is 5, the desired list is 2,...
2. Design a deterministic algorithm to solve the following problem. input: An array A[1...n] of n...
2. Design a deterministic algorithm to solve the following problem. input: An array A[1...n] of n integers. output: Two different indices i and j such that A[i] = A[j], if such indices exist. Otherwise, return NONE. Your algorithm must take O(n log(n)) time. You must describe your algorithm in plain English (no pseudocode) and you must explain why the running time of your algorithm is O(n log(n)).
2. There is a famous problem in computation called Subset Sum: Given a set S of...
2. There is a famous problem in computation called Subset Sum: Given a set S of n integers S = {a1, a2, a3, · · · , an} and a target value T, is it possible to find a subset of S that adds up to T? Consider the following example: S = {−17, −11, 22, 59} and the target is T = 65. (a) What are all the possible subsets I can make with S = {−17, −11, 22,...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
1.) Given any set of 53 integers, show that there are two of them having the...
1.) Given any set of 53 integers, show that there are two of them having the property that either their sum or their difference is evenly divisible by 103. 2.) Let 2^N denote the set of all infinite sequences consisting entirely of 0’s and 1’s. Prove this set is uncountable.
Given an array, A, of n−2 unique integers in the range from 1 to n, describe...
Given an array, A, of n−2 unique integers in the range from 1 to n, describe an O(n)-time method for finding the two integers in the range from 1 to n that are not in A. You may use only O(1) space in addition to the space used by A.
We are given an array A of size n containing n positive and negative integers (the...
We are given an array A of size n containing n positive and negative integers (the array is indexed starting from 0). Our goal is to find two indices i and j such that 0 ≤ i ≤ j ≤ n and Pk=j k=i A[k] is maximized. Input sequence: [2, -4, 1, 9, -6, 7, -3] Output: [1, 9, -6, 7] or i = 2 and j = 5 Input sequence: [1, 2, 3, 4, 5, 6, -3] Output: [1,...
Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is...
Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is given a positive integer N and determines whether N is a prime number, that is, not evenly divisible by any value other than 1 and itself. The output of your algorithm is either the message ‘not prime’, along with a factor of N, or the message ‘prime ‘ Many excellent simulations of sorting algorithms are available, examine them if they have questions about this...
(a) Show that the following algorithm computes the greatest common divisor g of the positive integers...
(a) Show that the following algorithm computes the greatest common divisor g of the positive integers a and b, together with a solution (u, v) in integers to the equation au + bv = gcd(a, b). 1. Set u = 1, g = a, x = 0, and y = b 2. If y = 0, set v = (g − au)/b and return the values (g, u, v) 3. Divide g by y with remainder, g = qy +...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT