PHP Program to Print uncommon elements from two sorted arrays Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50}Output : 10 25 40 50We do not print 20 and 30 as theseelements are present in both arrays.Input : arr1[] = {10, 20, 30} arr2[] = {40, 50}Output : 10 20 30 40 50 The idea is based on merge process of merge sort. We traverse both arrays and skip common elements.Steps to find uncommon elements between two sorted arrays:Initialize Pointers: Set three-pointers, i, j, and k, to start at the beginning of arrays arr1, arr2, and the result set respectively.Compare Array Elements: Compare current elements of arr1 and arr2. Print the smaller element if they differ and move the respective pointer.Skip Common Elements: If elements of both arrays are the same, increase both i and j to skip these common elements.Print Remaining Elements: After exiting the loop, print any remaining elements in arr1 or arr2 if one array is longer than the other.Output the Results: Echo each unique element not shared by both arrays, ensuring only uncommon elements are printed.Below is the implementation of above approach: PHP <?php // PHP program to find uncommon // elements of two sorted arrays function printUncommon($arr1, $arr2, $n1, $n2) { $i = 0; $j = 0; $k = 0; while ($i < $n1 && $j < $n2) { // If not common, print smaller if ($arr1[$i] < $arr2[$j]) { echo $arr1[$i] . " "; $i++; $k++; } else if ($arr2[$j] < $arr1[$i]) { echo $arr2[$j] . " "; $k++; $j++; } // Skip common element else { $i++; $j++; } } // printing remaining elements while ($i < $n1) { echo $arr1[$i] . " "; $i++; $k++; } while ($j < $n2) { echo $arr2[$j] . " "; $j++; $k++; } } // Driver code $arr1 = array(10, 20, 30); $arr2 = array(20, 25, 30, 40, 50); $n1 = sizeof($arr1) ; $n2 = sizeof($arr2) ; printUncommon($arr1, $arr2, $n1, $n2); // This code is contributed by Anuj_67 ?> Output10 25 40 50Complexity Analysis:Time Complexity: O(n1 + n2), where n1 and n2 represents the size of the given two arrays.Auxiliary Space: O(1), no extra space is required, so it is a constant.Please refer complete article on Print uncommon elements from two sorted arrays for more details! Comment More infoAdvertise with us Next Article PHP Program to Print uncommon elements from two sorted arrays K kartik Follow Improve Article Tags : PHP two-pointer-algorithm Practice Tags : two-pointer-algorithm Similar Reads Two Pointers Technique Two pointers is really an easy and effective technique that is typically used for Two Sum in Sorted Arrays, Closest Two Sum, Three Sum, Four Sum, Trapping Rain Water and many other popular interview questions. Given a sorted array arr (sorted in ascending order) and a target, find if there exists an 11 min read PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages. It allows developers to handle database interactions, session management, and form handling tasks.PHP code is execute 9 min read Trapping Rain Water Problem - Tutorial with Illustrations Trapping Rainwater Problem states that given an array of n non-negative integers arr[] representing an elevation map where the width of each bar is 1, compute how much water it can trap after rain.Trapping Rainwater ProblemExamples: Input: arr[] = [3, 0, 1, 0, 4, 0, 2]Output: 10Explanation: The expe 15+ min read Top 60+ PHP Interview Questions and Answers -2025 PHP is a popular server-side scripting language, widely known for its efficiency in web development and versatility across various platforms. PHP is extensively utilized by top companies such as Facebook, WordPress, Slack, Wikipedia, MailChimp, and many more due to its robust features and high perfo 15+ min read 3 Sum - Triplet Sum in Array Given an array arr[] of size n and an integer sum, the task is to check if there is a triplet in the array which sums up to the given target sum.Examples: Input: arr[] = [1, 4, 45, 6, 10, 8], target = 13Output: true Explanation: The triplet [1, 4, 8] sums up to 13Input: arr[] = [1, 2, 4, 3, 6, 7], t 15 min read PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi 8 min read Minimum Platforms Required for Given Arrival and Departure Times Given two arrays, arr[] and dep[], that represent the arrival and departure times of trains respectively, the task is to find the minimum number of platforms required so that no train waits. Examples: Input: arr[] = [900, 940, 950, 1100, 1500, 1800], dep[] = [910, 1200, 1120, 1130, 1900, 2000]Output 15 min read Version Control Systems Version Control Systems (VCS) are essential tools used in software development and collaborative projects to track and manage changes to code, documents, and other files. Whether you're working alone or as part of a team, version control helps ensure that your work is safe, organized, and easy to co 7 min read Next Permutation Given an array of integers arr[] representing a permutation (i.e., all elements are unique and arranged in some order), find the next lexicographically greater permutation by rearranging the elements of the array.If such a permutation does not exist (i.e., the array is the last possible permutation) 15 min read PHP Arrays Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien 5 min read Like