PHP Program for Third largest element in an array of distinct elements
Last Updated :
22 Jul, 2024
Given an array of n integers, find the third largest element. All the elements in the array are distinct integers.
Example :
Input: arr[] = {1, 14, 2, 16, 10, 20}
Output: The third Largest element is 14
Explanation: Largest element is 20, second largest element is 16
and third largest element is 14
Input: arr[] = {19, -10, 20, 14, 2, 16, 10}
Output: The third Largest element is 16
Explanation: Largest element is 20, second largest element is 19
and third largest element is 16
Naive Approach
The task is to first find the largest element, followed by the second-largest element and then excluding them both find the third-largest element. The basic idea is to iterate the array twice and mark the maximum and second maximum element and then excluding them both find the third maximum element, i.e the maximum element excluding the maximum and second maximum.
Algorithm:
- First, iterate through the array and find the the maximum.
- Store this as the first maximum along with its index.
- Now traverse the whole array finding the second max, excluding the maximum element.
- Finally, traverse the array the third time and find the third largest element i.e., excluding the maximum and second maximum.
PHP
<?php
// PHP program to find third
// Largest element in an array
// of distinct elements
function thirdLargest($arr, $arr_size)
{
/* There should be atleast
three elements */
if ($arr_size < 3)
{
echo " Invalid Input ";
return;
}
// Find first largest element
$first = $arr[0];
for ($i = 1; $i < $arr_size ; $i++)
if ($arr[$i] > $first)
$first = $arr[$i];
// Find second largest element
$second = PHP_INT_MIN;
for ($i = 0; $i < $arr_size ; $i++)
if ($arr[$i] > $second &&
$arr[$i] < $first)
$second = $arr[$i];
// Find third largest element
$third = PHP_INT_MIN;
for ($i = 0; $i < $arr_size ; $i++)
if ($arr[$i] > $third &&
$arr[$i] < $second)
$third = $arr[$i];
echo "The third Largest element is ",
$third,"
";
}
// Driver Code
$arr = array(12, 13, 1,
10, 34, 16);
$n = sizeof($arr);
thirdLargest($arr, $n);
// This code is contributed by m_kit
?>
OutputThe third Largest element is 13
Complexity Analysis:
- Time Complexity: O(n).
As the array is iterated thrice and is done in a constant time - Space complexity: O(1).
No extra space is needed as the indices can be stored in constant space.
Efficient Approach
The problem deals with finding the third largest element in the array in a single traversal. The problem can be cracked by taking help of a similar problem- finding the second maximum element. So the idea is to traverse the array from start to end and to keep track of the three largest elements up to that index (stored in variables). So after traversing the whole array, the variables would have stored the indices (or value) of the three largest elements of the array.
Algorithm:
- Create three variables, first, second, third, to store indices of three largest elements of the array. (Initially all of them are initialized to a minimum value).
- Move along the input array from start to the end.
- For every index check if the element is larger than first or not. Update the value of first, if the element is larger, and assign the value of first to second and second to third. So the largest element gets updated and the elements previously stored as largest becomes second largest, and the second largest element becomes third largest.
- Else if the element is larger than the second, then update the value of second,and the second largest element becomes third largest.
- If the previous two conditions fail, but the element is larger than the third, then update the third.
- Print the value of third after traversing the array from start to end
PHP
<?php
// PHP program to find third
// Largest element in an array
function thirdLargest($arr, $arr_size)
{
/* There should be atleast
three elements */
if ($arr_size < 3)
{
echo " Invalid Input ";
return;
}
// Initialize first, second and
// third Largest element
$first = $arr[0];
$second = PHP_INT_MIN;
$third = PHP_INT_MIN;
// Traverse array elements to
// find the third Largest
for ($i = 1; $i < $arr_size ; $i ++)
{
/* If current element is greater
than first, then update first,
second and third */
if ($arr[$i] > $first)
{
$third = $second;
$second = $first;
$first = $arr[$i];
}
/* If arr[i] is in between
first and second */
else if ($arr[$i] > $second)
{
$third = $second;
$second = $arr[$i];
}
/* If arr[i] is in between
second and third */
else if ($arr[$i] > $third)
$third = $arr[$i];
}
echo "The third Largest element is ",
$third;
}
// Driver Code
$arr = array (12, 13, 1,
10, 34, 16);
$n = sizeof($arr);
thirdLargest($arr, $n);
// This code is contributed by jit_t
?>
OutputThe third Largest element is 13
Complexity Analysis:
- Time Complexity: O(n).
As the array is iterated once and is done in a constant time - Space complexity: O(1).
No extra space is needed as the indices can be stored in constant space.
Please refer complete article on Third largest element in an array of distinct elements for more details!
Similar Reads
Javascript Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example : Input: arr[] = {1, 14, 2, 16, 10, 20}Output: The third Largest element is 14Explanation: Largest element is 20, second largest element is 16 and third largest element is 14Inp
6 min read
Third largest element in an array of distinct elements Given an array of n integers, the task is to find the third largest element. All the elements in the array are distinct integers. Examples : Input: arr[] = {1, 14, 2, 16, 10, 20}Output: 14Explanation: Largest element is 20, second largest element is 16 and third largest element is 14Input: arr[] = {
11 min read
Find the Second Largest Element in an Array in PHP We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further opera
3 min read
PHP Program To Find Mean and Median of an Unsorted Array Given an unsorted array, the task is to find the mean (average) and median of the array in PHP. we will see the approach and code example to solve this problem. ApproachTo find the mean of an array, we need to sum up all the elements in the array and then divide the sum by the total number of elemen
2 min read
PHP Second most frequent element in an array Given an array we have to find the second most frequent element present in it. Examples: Input : array(3, 3, 4, 5, 5, 5, 9, 8, 8, 8, 8, 8); Output : Second most frequent element is: 5 Input : array("geeks", "for", "geeks"); Output : Second most frequent element is: for Here are some common approache
4 min read
Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1,
2 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
Find k largest elements in an array Given an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order.Examples:Input: [1, 23, 12, 9, 30, 2, 50], k = 3Output: [50, 30, 23]Input: [11, 5, 12, 9, 44, 17, 2], k = 2Output: [44, 17]Table of Content[Nai
15+ min read
PHP | Remove duplicate elements from Array You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array. Examples: Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3} Output : array ( [6] => 2 [7] => 3 [4] => 1 [5] => 6 ) Input : array[] = {4, 2, 7, 3, 2, 7, 3} Output : arr
3 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read