PHP Program To Find Next Greater Element Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in the array. Elements for which no greater element exist, consider the next greater element as -1. For an array, the rightmost element always has the next greater element as -1. For an array that is sorted in decreasing order, all elements have the next greater element as -1.For the input array [4, 5, 2, 25], the next greater elements for each element are as follows.Element NGE 4 --> 5 5 --> 25 2 --> 25 25 --> -1For the input array [13, 7, 6, 12}, the next greater elements for each element are as follows. Element NGE 13 --> -1 7 --> 12 6 --> 12 12 --> -1Simple Method - Using Two LoopsThe outer loop picks all the elements one by one. The inner loop looks for the first greater element for the element picked by the outer loop. If a greater element is found then that element is printed as next, otherwise, -1 is printed.Below is the implementation of the above approach: PHP <?php // Simple PHP program to print next // greater elements in a given array /* Prints element and NGE pair for all elements of arr[] of size n */ function printNGE($arr, $n) { for ($i = 0; $i < $n; $i++) { $next = -1; for ($j = $i + 1; $j < $n; $j++) { if ($arr[$i] < $arr[$j]) { $next = $arr[$j]; break; } } echo $arr[$i] . " -- " . $next . "\n"; } } // Driver Code $arr = array(11, 13, 21, 3); $n = count($arr); printNGE($arr, $n); ?> Output11 -- 13 13 -- 21 21 -- -1 3 -- -1 Time Complexity: O(N2) Auxiliary Space: O(1)Please refer complete article on Next Greater Element for more details! Comment More infoAdvertise with us Next Article PHP Program to Find closest number in array K kartik Follow Improve Article Tags : Stack Web Technologies PHP Programs DSA Arrays Amazon Samsung Snapdeal Payu Zoho Informatica CouponDunia +8 More Practice Tags : AmazonCouponDuniaInformaticaPayuSamsungSnapdealZohoArraysStack +5 More Similar Reads PHP Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row.Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21]Output :3976Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]Output :216556Approach : The approach is very simple. The idea is to run the loop for no_of_rows. Check each element in 2 min read PHP Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples:Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input : arr[] = {2, 5, 6, 7, 8, 8, 9}; 3 min read PHP Program to Find lost element from a duplicated array Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element.Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[] 4 min read PHP Program to Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find an element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanati 3 min read PHP Program to Sort an Array Elements in Descending Order Given an array containing some elements, the task is to sort the array elements in descending order in PHP. Sorting elements of an array is a common operation in programming, and PHP provides several methods to accomplish this task.Table of ContentUsing rsort() FunctionUsing array_reverse() with sor 3 min read PHP Program to Delete Middle Element from an Array Given an array, the task is to delete the middle element of the array. If the array has an even number of elements, the middle element can be considered as the one closer to the start of the array (the lower middle).These are the following approaches:Table of ContentUsing array_splice() functionUsin 3 min read Like