Question

Suppose we are given two n-element sorted sequences A and B that may contain duplicate entries....

Suppose we are given two n-element sorted sequences A and B that may contain duplicate entries. Describe an O(n)-time method for computing a sequence representing all elements in A or B with no duplicates.(python)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

https://onlinegdb.com/r11tcnkf8

def printUnion(arr1, arr2, m, n):
i,j = 0,0
while i < m and j < n:
if arr1[i] < arr2[j]:
print(arr1[i])
i += 1
elif arr2[j] < arr1[i]:
print(arr2[j])
j+= 1
else:
print(arr2[j])
j += 1
i += 1
  
# Print remaining elements of the larger array
while i < m:
print(arr1[i])
i += 1
  
while j < n:
print(arr2[j])
j += 1
  
# Driver program to test above function
arr1 = [1, 2, 4, 5, 6]
arr2 = [2, 3, 5, 7]
m = len(arr1)
n = len(arr2)
printUnion(arr1, arr2, m, n)

Add a comment
Know the answer?
Add Answer to:
Suppose we are given two n-element sorted sequences A and B that may contain duplicate entries....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we are given two n-element sorted sequences A and B that may contain duplicate entries....

    Suppose we are given two n-element sorted sequences A and B that may contain duplicate entries. Describe an O(n)-time method for computing a sequence representing all elements in A or B with no duplicates.

  • (b) Suppose you are designing a search aggregator, which for a given query fetches search results from two different se...

    (b) Suppose you are designing a search aggregator, which for a given query fetches search results from two different search engines and presents an intersection of the two search results. Here is a simplified version of this problem: Given two sorted integer arrays of lengths m and n, return a new array with elements that are present in both input arrays. The input array may contain duplicates, but there should be no duplicates in the output array. For example, if...

  • 9. When we have two sorted lists of numbers in non-descending order, and we need to...

    9. When we have two sorted lists of numbers in non-descending order, and we need to merge them into one sorted list, we can simply compare the first two elements of the lists, extract the smaller one and attach it to the end of the new list, and repeat until one of the two original lists become empty, then we attach the remaining numbers to the end of the new list and it's done. This takes linear time. Now, try...

  • When we have two sorted lists of numbers in non-descending order, and we need to merge...

    When we have two sorted lists of numbers in non-descending order, and we need to merge them into one sorted list, we can simply compare the first two elements of the lists, extract the smaller one and attach it to the end of the new list, and repeat until one of the two original lists become empty, then we attach the remaining numbers to the end of the new list and it's done. This takes linear time. Now, try to...

  • 4. (10 points) Suppose we are given a sequence S of n elements, each of which...

    4. (10 points) Suppose we are given a sequence S of n elements, each of which is colored red or blue. Assuming S is represented by an array, give a linear-time in-place algorithm for ordering S so that all the blue elements are listed before all the red elements. What is the running time of your method?

  • Given two arrays A and B of n integers both of which are sorted in ascending...

    Given two arrays A and B of n integers both of which are sorted in ascending order. Write an algorithm to check whether or not A and B have an element in common. Find the worst case number of array element comparisons done by this algorithm as a function of n and its Big-O complexity

  • 2. (15 marks) Consider the abstract datatype SEQ whose objects are sequences of elements and whic...

    do (b) please 2. (15 marks) Consider the abstract datatype SEQ whose objects are sequences of elements and which supports two operations . PREPEND(x, S), which inserts element r at the beginning of the sequence S and . ACCESS(S, i), which returns the ith element in the sequence Suppose that we represent S by a singly linked list. Then PREPEND(, S) takes 1 step and ACCESS(S, i) takes i steps, provided S has at least i elements Suppose that S...

  • Please provide explanation. Suppose you are given a sorted list of N elements followed by f(N)...

    Please provide explanation. Suppose you are given a sorted list of N elements followed by f(N) randomly ordered values. How would you sort the entire list if: (a) f(N) = N/20 (b) f(N) = O(log N) (c) f(N) = O( √ N)

  • (25pts) You are given two sorted lists of size m and n. Give an O(log m...

    (25pts) You are given two sorted lists of size m and n. Give an O(log m log n) time algorithm for computing the k-th smallest element in the union of the two lists Note that the only way you can access these values is through queries to the databases. Ina single query, you can specify a value k to one of the two databases, and the chosen database will return the k-th smallest value that it contains. Since queries are...

  • 3. Suppose you have an array of n random elements. You are required to perform n...

    3. Suppose you have an array of n random elements. You are required to perform n different searches on the array. What is best big-oh time for your entire task? Explain how to achieve that time. 4. Suppose you are given two sorted integer arrays int[] A and int[] B. Write a method that returns an array which contains only the common elements (elements that are present in both A and B) of these two sorted arrays. Indicate the big-Oh...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT