Question

In: Computer Science

(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n)...

(Linear time algorithm for finding duplicates of two sorted arrays, 20pt)

Devise a linear time O(m+n) algorithm to find the duplicates between two sorted arrays (m, n are the sizes of two arrays). For instance, for inputs {1,3,5,7} and {1,2,3,4}, the correct outputs should be {1,3

Solutions

Expert Solution

/* Code of the above question in C language */
  
#include<stdio.h>
/* This function prints Duplicates of arr1[] and arr2[]
where,
    m is the number of elements in arr1[]
   n is the number of elements in arr2[]
*/
   
void printDuplicates(int arr1[], int arr2[], int m, int n)
{
   int i = 0;
  
   int j = 0;
  
   while (i < m && j < n)
   {
       if(arr1[i] == arr2[j]) /* if both the elements are equal (Duplicates)*/
       {
           /* printing the element */
           printf(" %d ", arr1[i]);
             
           /* incrementing both i,j */
           i++;
           j++;
       }
          
       else if(arr1[i] < arr2[j]) /* if arr1[i] is smaller than arr2[j] */
       {
           /* incrementing index i */
           i++;
       }
          
       else if (arr2[j] < arr1[i]) /* if arr1[i] is larger than arr2[j] */
       {
           /* incrementing index j */
           j++;
       }
   }
}
  

int main()
{
   int m,n; /* Declaring the two variables, which are sizes of two arrays*/
  
   /* Reading the size of array1 from User*/
   printf(" Enter m : ");
   scanf("%d",&m);

   /* Reading the size of array2 from User*/
   printf(" Enter n : ");
   scanf("%d",&n);
      
    int arr1[m]; /* Declaring array1 of size 'm' */
   
    int arr2[n]; /* Declaring array2 of size 'n' */
   
    printf(" Enter elements for array1 : \n");
   
    /* Reading the array1 from the User*/
    for(int i=0; i<m; ++i)
        scanf("%d",&arr1[i]);
   
    printf(" Enter elements for array2 : \n");
       
    /* Reading the array2 from the User*/
    for(int i=0; i<n; ++i)
        scanf("%d",&arr2[i]);
  
   /* Calling our required function */
   printDuplicates(arr1, arr2, m, n);
}


Related Solutions

Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted)...
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size...
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
Suppose you have two sorted arrays of n integers: X0[1..n] and X1[1..n]. Devise and analyze an...
Suppose you have two sorted arrays of n integers: X0[1..n] and X1[1..n]. Devise and analyze an efficient algorithm for finding the median of all numbers in arrays X0 and X1. Now, suppose you have not two, but three such arrays, each with n elements. Devise and analyze an efficient algorithm for finding the median. Do the same for n arrays, each with n elements.
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
Write the algorithm, following the ideas given in class, for merging two sorted arrays into one...
Write the algorithm, following the ideas given in class, for merging two sorted arrays into one sorted array. Note the algorithm is not the one for merge sort. 2) What is the best asymptotic upper bound for the algorithm? List reasoning steps.
Recall that in MergeSort algorithm, we used a linear-time subroutine called Merge which merges two sorted...
Recall that in MergeSort algorithm, we used a linear-time subroutine called Merge which merges two sorted lists into a single sorted list. Now suppose there are k sorted lists and there are n elements in total in these k lists. Design an efficient algorithm that merges these k sorted lists into one sorted list. The running time of your algorithm should be a function of both n and k.
1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there...
1. Given three arrays of N names each, describe an O(nlogn) algorithm to determine if there is any name common to all three arrays, and if so, return the first such name.? 2. Given an input array with all equal values, compare the following sorting algorithm using big-O notation. Running Time Space Complexity Merge Sort Quick Sort Heap Sort
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.) You can assume...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT