We have a 2 arrays e.g. arr1[]={2,3,4,4,5,6,7} , arr2[]={1,2,2,4,6,8,8} . Assuming that both the arrays are sorted , if we want to write a program to find common numbers present in both arrays , how do we do that ? Note that nested for loop has a complexity of n2 , hence avoid nested for loop.

Questions by Ajay Kanse   answers by Ajay Kanse

Showing Answers 1 - 15 of 15 Answers

Meghraj

  • Jun 14th, 2014
 

well check for how many 1s,2s, upto 9s in both the arrays and minimum of a particular digit(from 1 to 9) in an array will be the common numbers of that particular digit. we will check upto 9 in both arrays for total common numbers.

  Was this answer useful?  Yes

Assuming both arrays are sorted in ascending order, this is pretty easy to do in a single loop with two array counters. Start both counters at zero. Compare array elements at their respective counters ( a1[s1] == a2[s2] ). If they match, write the matched value to the result array, then advance both array counters until they both point to a new value (that way you dont match mulutpie occurences). If they dont match, advance the counter in the array with the lower value. Repeat until you reach the end of either array.

Code
  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #include <string.h>

  4. #include <math.h>

  5. #include <time.h>

  6.  

  7. /**

  8.   * callback function for qsort

  9.   */

  10. static int isort( const void *lhs, const void *rhs )

  11. {

  12.   const int ilhs = *(const int *) lhs;

  13.   const int irhs = *(const int *) rhs;

  14.  

  15.   if ( ilhs < irhs )

  16.     return -1;

  17.   else if ( ilhs > irhs )

  18.     return 1;

  19.   else

  20.     return 0;

  21. }

  22.  

  23. /**

  24.  * Display array contents to specified file stream

  25.  */

  26. void printarr( FILE *stream, const char *arrname, const int *arr, size_t size )

  27. {

  28.   char *sep = "";

  29.   fprintf( stream, "%s = {", arrname );

  30.   for ( size_t i = 0; i < size; ++i )

  31.   {

  32.     fprintf( stream, "%s%d", sep, arr[i] );

  33.     sep = ",";

  34.   }

  35.   fprintf( stream, "}\n" );

  36. }

  37.  

  38. /**

  39.  * Generate a sorted array with a random size no less than minsize and

  40.  * and random contents between 1 and 10.  

  41.  */

  42. void genarr( int **arr, size_t *arrsize, size_t minsize, size_t maxsize )

  43. {

  44.   size_t size = (size_t) ((rand() / (RAND_MAX + 1.0))*(maxsize - minsize + 1) + minsize);

  45.   *arr = calloc (size, sizeof **arr );

  46.   if ( *arr )

  47.   {

  48.     for ( size_t i = 0; i < size; ++i )

  49.       (*arr)[i] = (rand() % 10) + 1;

  50.  

  51.     qsort( *arr, size, sizeof **arr, isort );

  52.     *arrsize = size;

  53.   }

  54. }

  55.  

  56. /**

  57.  * Find matching values in the two arrays.

  58.  */

  59. void find_dups( const int *a1, size_t s1, const int *a2, size_t s2, int *result, size_t *rdx )

  60. {

  61.   size_t a1dx = 0, a2dx = 0;

  62.   *rdx = 0;

  63.  

  64.   while ( a1dx < s1 && a2dx < s2 )

  65.   {

  66.     if ( a1[a1dx] == a2[a2dx] )

  67.     {

  68.       result[(*rdx)++] = a1[a1dx];

  69.       while ( a1[++a1dx] == result[*rdx-1] )

  70.         ;

  71.       while ( a2[++a2dx] == result[*rdx-1] )

  72.         ;

  73.     }

  74.     else

  75.     {

  76.       if ( a1[a1dx] < a2[a2dx] )

  77.         a1dx++;

  78.       else

  79.         a2dx++;

  80.     }

  81.   }

  82. }

  83.  

  84. /**

  85.  * Test driver.

  86.  */

  87. int main( void )

  88. {

  89.   int *a1, *a2;

  90.   size_t s1 = 0, s2 = 0;

  91.  

  92.   srand( time(NULL) );

  93.   genarr( &a1, &s1, 5, 20 );

  94.   genarr( &a2, &s2, 5, 20 );

  95.  

  96.   if ( !a1 || !a2 )

  97.   {

  98.     fprintf( stderr, "allocation failure\n" );

  99.     return 0;

  100.   }

  101.  

  102.   printarr( stdout, " a1", a1, s1 );

  103.   printarr( stdout, " a2", a2, s2 );

  104.  

  105.   size_t rsize = s1 < s2 ? s1 : s2;

  106.   int *res = calloc( rsize, sizeof *res );

  107.   if ( !res )

  108.   {

  109.     free( a1 );

  110.     free( a2 );

  111.     fprintf( stderr, "allocation failure\n" );

  112.     return 0;

  113.   }

  114.  

  115.   find_dups( a1, s2, a2, s2, res, &rsize );

  116.   printarr( stdout, "res", res, rsize );

  117.  

  118.   free( a1 );

  119.   free( a2 );

  120.   free( res );

  121.   return 0;

  122. }

  123.  

  Was this answer useful?  Yes

Sagar Ys

  • Aug 13th, 2015
 

Sort both the arrays, remove duplicate in both the arrays.
Combine two arrays sort it and find the duplicate.

  Was this answer useful?  Yes

Nitin Garg

  • Dec 12th, 2015
 

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3. #include<math.h>

  4.  

  5. int main()

  6. {

  7.  

  8. int arr1[7]={2,3,4,4,5,6,7};

  9. int arr2[7]={1,2,2,4,6,8,8};

  10. int i,j=0,k=0;

  11.  

  12. while(k!=8){

  13. for(i=0;i<8;i++){

  14. if(arr1[i]==arr2[k]){

  15. printf("%d ",arr1[i]);

  16. break;

  17. }//if

  18.              

  19. }//for

  20. k++;

  21.  

  22. }//while

  23.  

  24.  

  25. getch();

  26. }

  Was this answer useful?  Yes

Alejandro Visiedo

  • Mar 5th, 2016
 

Code
  1.         int arr1[7] = {2,3,4,4,5,6,7};

  2.         int arr2[7] = {1,2,2,4,6,8,8};

  3.         int index1 = 0;

  4.         int index2 = 0;

  5.         int maxindex1 = (sizeof(arr1)/sizeof(int));

  6.         int maxindex2 = (sizeof(arr2)/sizeof(int));

  7.         while (index1 < maxindex1 && index2 < maxindex2)

  8.         {

  9.                 if (arr1[index1] == arr2[index2]) {

  10.                         printf("%d, ", arr1[index1]);

  11.                         index1++;

  12.                         index2++;

  13.                 } else {

  14.                         if (arr1[index1] < arr2[index2]) {

  15.                                 index1++;

  16.                         } else {

  17.                                 index2++;

  18.                         }

  19.                 }

  20.         }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions