fork download
  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. cahr ch='a';
  5. }
  6. //Enter you code here.
  7. //Please indent properly.
  8.  
  9. <?php
  10. //program to find the common elements of the two array
  11. //here we have to array A and B from which w have to find the common element
  12. //first we sort then using merge sort and after then for traversing through
  13. //the array in one iteration we can find the comman elements the given array
  14. //this is an inspace algorithm meansno extra space is needed
  15.  
  16. //best case time complexity=O(nlogn)
  17. //O(nlogn)-> for sorting
  18. //O(n)-> for while loop to find comman element
  19.  
  20. //average case time complexity=O(nlogn)
  21. //O(nlogn)-> for sorting
  22. //O(n)-> for while loop to find comman element
  23.  
  24. //worst case time complexity =O(nlogn)
  25. //O(nlogn)-> for sorting
  26. //O(n)-> for while loop to find comman element
  27.  
  28.  
  29.  
  30. $commonArray=array();
  31. $A=array(3,4,5,6,7,8,9,10,36,58,27,48);
  32. $B=array(3,10,4,5,6,8,12,24,37,27,50);
  33. sort($A);
  34. sort($B);
  35. $size1=sizeof($A);
  36. $size2=sizeof($B);
  37. $counter1=0;
  38. $counter2=0;
  39. while(($counter1< $size1) && ($counter2)<($size2))//traversing through the array
  40. {
  41.  
  42. if ($A[$counter1] == $B[$counter2])
  43. {
  44. array_push($commonArray,$A[$counter1]); //to enter comman element in the output array
  45. $counter1=$counter1+1;
  46. $counter2=$counter2+1;
  47. }
  48. else if ($A[$counter1] < $B[$counter2])
  49. {
  50. $counter1=$counter1+1; }
  51.  
  52. else
  53. {
  54. $counter2=$counter2+1;
  55. }
  56. }
  57.  
  58. print_r($commonArray);//to print the output array
  59. ?>
  60.  
  61.  
Success #stdin #stdout 0.02s 25884KB
stdin
Standard input is empty
stdout
#include<iostream>
using namespace std;
int main(){
	cahr ch='a';
}
//Enter you code here.
//Please indent properly.

Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 8
    [5] => 10
    [6] => 27
)