PHP Program to find the Closest Pair from Two Sorted Arrays

PHPz
Release: 2024-08-28 13:34:10
Original
895 people have browsed it

PHP Program to find the Closest Pair from Two Sorted Arrays

What is PHP?

PHP (Hypertext Preprocessor) is a popular scripting language designed for web development. It is widely used for creating dynamic and interactive web pages. PHP code can be embedded directly into HTML, allowing developers to mix PHP and HTML seamlessly. PHP can connect to databases, process form data, generate dynamic content, handle file uploads, interact with servers, and perform various server-side tasks. It supports a wide range of web development frameworks, such as Laravel, Symfony, and CodeIgniter, which provide additional tools and features for building web applications. PHP is an open-source language with a large community, extensive documentation, and a rich ecosystem of libraries and extensions.

PHP Program to find the Closest Pair from Two Sorted Arrays

Given two sorted arrays and a number x, find the pair whose sum is closest to x and the pair has an element from each array.

Input

ar1 = [1, 3, 5, 7, 9];
ar2 = [2, 4, 6, 8, 10];
x = 12;<b>
</b>
Copy after login

Output

Output is 1 and 10 because 1+10=11 which is closer to 12.
Copy after login

Example

<?php

function printClosest($ar1, $ar2,
					$m, $n, $x)
{

	$diff = PHP_INT_MAX;
	$res_l;
	$res_r;
	$l = 0;
	$r = $n - 1;
	while ($l < $m and $r >= 0) {
	   if (abs($ar1[$l] + $ar2[$r] - $x) < $diff){
	      $res_l = $l;
		  $res_r = $r;
		  $diff = abs($ar1[$l] + $ar2[$r] - $x);
		}

		if ($ar1[$l] + $ar2[$r] > $x)
		   $r--;
		else
		   $l++;
   }
   echo "The closest pair is [", $ar1[$res_l], ", ", $ar2[$res_r], "] <br>";
}
	$ar1 = array(1, 4, 8, 10);
	$ar2 = array(2, 6, 9);
	$m = count($ar1);
	$n = count($ar2);
	$x = 20;
	printClosest($ar1, $ar2, $m, $n, $x);
?>
Copy after login

Output

The closest pair is [10, 9]
Copy after login

Conclusion

In Summary, The provided PHP program employs a two-pointer technique to efficiently find the closest pair from two sorted arrays. It begins by initializing the difference between the pair sum and the given number, "x," as a maximum value. The program maintains two result indexes, res_l and res_r, to store the indices of the closest pair. By comparing the absolute difference of the current pair's sum with x, the program continuously updates the closest pair whenever a closer pair is encountered. It adjusts the pointers based on whether the current sum is greater or smaller than x, effectively narrowing down the search space.

The program's time complexity is dependent on the sizes of the input arrays, m and n, as it performs a linear scan while traversing both arrays once. Therefore, it has a complexity of O(m + n). The PHP program offers an efficient solution to finding the closest pair from two sorted arrays with a sum closest to the given number x. By utilizing a two-pointer approach, it provides a streamlined method for identifying the pair with the minimum absolute difference, providing flexibility and accuracy in various scenarios.

The above is the detailed content of PHP Program to find the Closest Pair from Two Sorted Arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!