Question

In: Computer Science

Question 3: (40 marks) You are requested to design an algorithm to check whether a key...

Question 3:

You are requested to design an algorithm to check whether a key pair (sum of two numbers only) exist in a given array (of size N) or not. If the key pair exists at least once then the algorithm should return “True”; otherwise “False”.

For example, given an array A[8, -3, 9, 5, -3, 11, 2, -7, 4, 11] where the key pair is 13, then the required algorithm should return “True” because such pair exist at least once ex:(9+4). Another example, given the same array, where the key pair is 21, the required algorithm should return “false”, as there is no any pair with sum equal to 21

  1. Another algorithm would first sort the array to take advantage of the sorted value to facilitate the search for the required key pair :  
    1. Design this algorithm [Hint: don't design a sorting function, simply use Sort(A) to do the job of sorting without going into the sorting details].
    2. In order to get a better performance, which sorting algorithm “Sort(A)” would you choose to do this task?

iii. What is the complexity of this algorithm in the best and worst case? (5 marks

Solutions

Expert Solution

Find the solution to the above question below, do read all the steps provided carefully and in case of any comments do comment below. If found helpful please upvote this.

Algorithm

  1. First sort the given array in ascendigng order
  2. now we will use two pointer algorithm that is take two indexes. first will point to 0 (start of array) and other at n-1(end of array), let them be a and b
  3. check if arr[a]+arr[b] == given sum , if yes then return true ,
  4. else check if the obtained sum is less than the given if yes then increment a by 1
  5. if the obtained sum was more than the given sum then decrement b by 1
  6. repeat the steps until a < b
  7. that's all if we don't find anything and reach the end of lopp then return False.

Code

A = [8, -3, 9, 5, -3, 11, 2, -7, 4, 11]

Sort(A)

a = 0

b = n-1

sum = 19

while a < b:

    if A[a]+A[b] == sum:

        return True

    if A[a]+A[b] < sum:

        a = a+1

    else:

        b = b-1

return False

Screenshot of code

Part ii

In order to get a better performance merge sort should be used, merge sort has worst case time complexity of O(nlog(n)) which is better than any other sorting algorithm

Part iii

Sorting will take nlog(n) time in any case

Now for best case finding the pair will take O(1) so overall it will be O(nlog(n))

In worst case findign teh pair will take O(n/2) so overall it will O(nlog(n)) + O(n/2) which is same as O(nlog(n))


Related Solutions

Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid...
Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid is shown below:We assume that there are 16 digits in a credit card number.Start from the first digit of the credit card number as in position 1; second digit as in position 2; so on until the last digit as in position 16;If a digit is in even numbered position, take the digit itself as its representative;If a digit is in odd numbered position,...
Question 22 options: Look up the requested values using book tables. You may check with a...
Question 22 options: Look up the requested values using book tables. You may check with a calculator, but enter the table value.    Let T denote a t-distribution variable with 10 degrees of freedom. Find: P(T<1.372)=?
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should be implemented: Constant space, i.e. without creating extra copies of the linked list Unrestricted space q1: / For this implementation you should use constant space   // Note: you are free to add any extra methods,   // but this method has to be used   public static boolean isPalindromeRestricted(ListNode head) {     // Your code goes here     return false;   } q2: // For this implementation the space...
Question 3 10 marks Barton Company requested a large loan from First National bank to acquire...
Question 3 10 marks Barton Company requested a large loan from First National bank to acquire a large piece of land for future expansion. Bart reported current assets of R1,900 ,000 (R430 000 in cash) and current liabilities of R1,075,000. FNB denied the request for a number of reasons. When the Company received the news, the financial controller immediately paid R420 000 that was owed to several trade creditors. He then asked the bank to reconsider the loan application. Based...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that can sort a singly-linked-list with Merge-sort. The program should read integers from file (hw-extra.txt) and create an unsorted singly-linked list. Then, the list should be sorted using merge sort algorithm. The merge-sort function should take the head of a linked list, and the size of the linked list as parameters. hw-extra.txt provided: 37 32 96 2 25 71 432 132 76 243 6 32...
Question 3 (17 marks) Freedom Ltd is considering whether to lease or buy an advanced machine...
Question 3 Freedom Ltd is considering whether to lease or buy an advanced machine for its new product. The following information is available for this decision: Buy: The purchase price of the machine is $4.85 million. The machine will be depreciated using straight-line method over 4 years with a zero salvage value. Lease: The annual lease payments will be $1.1 million, payable at the beginning of each of the four years of the lease. The annual interest rate of secured...
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
QUESTION 3 (20 MARKS) QUESTION 3 (20 MARKS) An analysis of the Business School graduates found...
QUESTION 3 QUESTION 3 An analysis of the Business School graduates found that 210 out of 318 randomly selected graduates used An analysis of the Business School graduates found that 210 out of 318 randomly selected graduates used  a statistical inference technique during their first year of employment.a statistical inference technique during their first year of employment. (a) Calculate a 90% confidence interval for the proportion of graduates who used a statistical inference (a) Calculate a 90% confidence interval for the...
Question 3 (12 marks) Part A ( 3 marks) A project (by a competitor) is way...
Question 3 Part A ( 3 marks) A project (by a competitor) is way behind development by five months. Your company's Project Manager Ellie suggested using the "exploit" strategy to supply to the customer. Explain to your team briefly what is this type of risk? Part B ( 9 marks) With reference to the strategy of exploitation above, now create a detailed plan on what you would execute with your team. [Assumption: Budget is not a problem.]
Question 3: Accounting & Finance (40 marks) Capital investment decisions look at how businesses should assess...
Question 3: Accounting & Finance Capital investment decisions look at how businesses should assess proposed investments in new plant, machinery, buildings and other long-term assets. The essential feature of investment decisions is time. There are different approaches/methods used by businesses to evaluate investment opportunities such as accounting rate of return, payback period, net present value, and internal rate of return. Suppose that you are an analyst for a company considering investments in three projects with the same class of risk...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT