Now suppose our goal is to instead find whether there is a sub-array A[l, r] such that

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
(b) Now suppose our goal is to instead find whether there is a sub-array A[l, r] such that
Ali] = n².
i=l
Design a randomized algorithm for this case with worst-case expected runtime of O(n).
Transcribed Image Text:(b) Now suppose our goal is to instead find whether there is a sub-array A[l, r] such that Ali] = n². i=l Design a randomized algorithm for this case with worst-case expected runtime of O(n).
Expert Solution
Step 1

To find a sub-array where one array is the square of the other array, we need a minimum of O(N) space complexity.

Why? Because we need to find to the square root of the first array and store it in another array. In teh worst case we need to traverse the whole array (arr1) and match it with every position of arr2 till (i=0 to i<= length(arr2)). Here in this algorithm, I have taken arr1 as the main array, arr2 as sub-array and arr3 = [] as an empty array. There would be 2 conditions as mentioned below.

This is not a code. This is just an algorithm.

 

---------------------------------------------------- Algorithm------------------------------------------------------------

Case1: arr1 size is greater than arr2

For i=0 to length(arr1)-length(arr2)
        arr3 = Math.sqrt(arr1)
       If equal(arr2.begin(),arr2.end(), arr3.begin()+i) is true
             Then return true since we found a range 
       starting from arr3[i] which is equal to arr2
Return false if no such range is found.

 

Case 2: arr1 size is greater than arr2

For i=0 to length(arr2)-length(arr1)
        arr3 = Math.sqrt(arr1)
       If equal(arr3.begin(),arr3.end(), arr2.begin()+i) is true
             Then return true since we found a range 
       starting from arr2[i] which is equal to arr3
Return false if no such range is found.

----------------------------------------------------------------------------------------------------------------------------

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY