//Enter you code here.
//Please indent properly.
<?php
//program to find the common elements of the two array
//here we have to array A and B from which w have to find the common element
//first we sort then using merge sort and after then for traversing through
//the array in one iteration we can find the comman elements the given array
//this is an inspace algorithm meansno extra space is needed
//best case time complexity=O(nlogn)
//O(nlogn)-> for sorting
//O(n)-> for while loop to find comman element
//average case time complexity=O(nlogn)
//O(nlogn)-> for sorting
//O(n)-> for while loop to find comman element
//worst case time complexity =O(nlogn)
//O(nlogn)-> for sorting
//O(n)-> for while loop to find comman element
$A=array(3,4,5,6,7,8,9,10,36,58,27,48); $B=array(3,10,4,5,6,8,12,24,37,27,50); $counter1=0;
$counter2=0;
while(($counter1< $size1) && ($counter2)<($size2))//traversing through the array
{
if ($A[$counter1] == $B[$counter2])
{
array_push($commonArray,$A[$counter1]); //to enter comman element in the output array $counter1=$counter1+1;
$counter2=$counter2+1;
}
else if ($A[$counter1] < $B[$counter2])
{
$counter1=$counter1+1; }
else
{
$counter2=$counter2+1;
}
}
print_r($commonArray);//to print the output array ?>
Ly9FbnRlciB5b3UgY29kZSBoZXJlLgovL1BsZWFzZSBpbmRlbnQgcHJvcGVybHkuCgo8P3BocAovL3Byb2dyYW0gdG8gZmluZCB0aGUgY29tbW9uIGVsZW1lbnRzIG9mIHRoZSB0d28gYXJyYXkKLy9oZXJlIHdlIGhhdmUgdG8gYXJyYXkgQSBhbmQgQiBmcm9tIHdoaWNoIHcgaGF2ZSB0byBmaW5kIHRoZSBjb21tb24gZWxlbWVudAovL2ZpcnN0IHdlIHNvcnQgdGhlbiB1c2luZyBtZXJnZSBzb3J0IGFuZCBhZnRlciB0aGVuIGZvciB0cmF2ZXJzaW5nIHRocm91Z2gKLy90aGUgYXJyYXkgaW4gb25lIGl0ZXJhdGlvbiB3ZSBjYW4gZmluZCB0aGUgY29tbWFuIGVsZW1lbnRzIHRoZSBnaXZlbiBhcnJheQovL3RoaXMgaXMgYW4gaW5zcGFjZSBhbGdvcml0aG0gbWVhbnNubyBleHRyYSBzcGFjZSBp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