Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Answer the questions in full detail! Explain the process in a step-by-step procedure and remember that the content is highly based on zybook chapters for
Answer the questions in full detail! Explain the process in a step-by-step procedure and remember that the content is highly based on zybook chapters for algorithms and data structures. The question is self explanatory as how the answer should be. Will rate afterwards accordingly!
The algorithm has been included in the two images above!
2. (10 pts) Recall the O(n Ig n)-time algorithm given in class that on an input array A of integers and a given integer intVal, finds two indices i and j, where A[i] + A[j] = intVal. Show how the algorithm can be modified in order to find indices i andj, where A[i] - A[j] = intVal instead. Analyze the running time of the modified algorithm. Input: (AT...n-1)), array of integers ,ko-Z. Output: i osisjen-s, when AG) + Auj] = 1 for lint i=o, ich, i++) folint jo.jen, jtt) y Aci ] + A13] =k+ - reearnli 15. return "not found" won th 1 1 total running time: (+1+) xnxn +1 = 3n+1=0(5) AGJt Arj]t Better also: Vi kang O(nlyn) 1. Morgesut A. Acil=k-AE) a for lintizo; isn, itt) ? Ollow xn 162. Val = K-Atid: 11., Do a Binary Search for val to) +1 Better algo: Acast AEG II Vi kang Olnlyn) 1 . Merge sut A. A[j] = K-A1 a for lint ico; ish, i++) - 7 Ollon) xn | 16 D. val= K-A ti]. honor ... Do a Binery Search for was I tolgo)+1 Chapter 3 Page 30 1 ton) +1 I 1E . bign) F4 I 1 5 h Do a Binery Search forval in A. to find j, whe A[i]=val- If such aj is found, return (i, j) return "not found". 16 6. total: = o (nen) + nx Olgn) + 1 0 Ingus 2. (10 pts) Recall the O(n Ig n)-time algorithm given in class that on an input array A of integers and a given integer intVal, finds two indices i and j, where A[i] + A[j] = intVal. Show how the algorithm can be modified in order to find indices i andj, where A[i] - A[j] = intVal instead. Analyze the running time of the modified algorithm. Input: (AT...n-1)), array of integers ,ko-Z. Output: i osisjen-s, when AG) + Auj] = 1 for lint i=o, ich, i++) folint jo.jen, jtt) y Aci ] + A13] =k+ - reearnli 15. return "not found" won th 1 1 total running time: (+1+) xnxn +1 = 3n+1=0(5) AGJt Arj]t Better also: Vi kang O(nlyn) 1. Morgesut A. Acil=k-AE) a for lintizo; isn, itt) ? Ollow xn 162. Val = K-Atid: 11., Do a Binary Search for val to) +1 Better algo: Acast AEG II Vi kang Olnlyn) 1 . Merge sut A. A[j] = K-A1 a for lint ico; ish, i++) - 7 Ollon) xn | 16 D. val= K-A ti]. honor ... Do a Binery Search for was I tolgo)+1 Chapter 3 Page 30 1 ton) +1 I 1E . bign) F4 I 1 5 h Do a Binery Search forval in A. to find j, whe A[i]=val- If such aj is found, return (i, j) return "not found". 16 6. total: = o (nen) + nx Olgn) + 1 0 IngusStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started