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

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).
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of finding median of unordered array in linear time.
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...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
Given a sorted array with lot of duplicates, write a problem to search specific element m....
Given a sorted array with lot of duplicates, write a problem to search specific element m. If it’s included in the A, return its minimal index, otherwise return -1. For example A = {0, 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4}, if we search 4, you program should return 8. You are only allowed to use binary search. Linear search is not allowed here.
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly...
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly linked lists L1 and L2, where L1 contains the odd-number-th elements in L and L2 contains the even-number-th elements in L. Elements in both L1 and L2 should appear in the same order as they appear in L. For example, if L contains the numbers 4, 7, 9, 1, and -3, in that order, then the output L1 should contain 4, 9, and -3,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT