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)).
Recall the 2-sum problem which took an array of N integers and determined the number of...
Recall the 2-sum problem which took an array of N integers and determined the number of pairs that summed to 0. Now consider a 2-sum BST problem. Let B be a binary search tree with N integers as unique keys. Using only data structures and algorithms that have been discussed in this class, describe an algorithm that will return the number of pairs in B that add to 0. Analyze the space and runtime of your algorithm.
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,...
Describe an algorithm that, given a list of n numbers, decides in linear time whether the...
Describe an algorithm that, given a list of n numbers, decides in linear time whether the list contains at least ⌈n/2⌉ numbers, all with equal value. What if we want to know if there are at least ⌈ n/100 ⌉ numbers with equal value? Hint: suppose some number, x, occurs ⌈n/2⌉ times. Where could all of these copies end up if we sorted? (This is not a suggestion we should actually sort). Wherever they end up, find one invariant.