PHP Program for Number of local extrema in an array Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report You are given an array on n-elements. An extrema is an element that is either greater than its both of neighbors or less than its both neighbors. You have to calculate the number of local extrema in given array. Note: 1st and last elements are not extrema.Examples : Input : a[] = {1, 5, 2, 5}Output : 2Input : a[] = {1, 2, 3}Output : 0Approach:For calculating number of extrema we have to check whether an element is maxima or minima i.e. whether it is greater than both of its neighbors or less than both neighbors. For this simply iterate over the array and for each elements check its possibility of being an extrema.Note: a[0] and a[n-1] has exactly one neighbour each, they are neither minima nor maxima. PHP // PHP to find number // of extrema // function to find // local extremum function extrema($a, $n) { $count = 0; // start loop from position 1 // till n-1 for ($i = 1; $i < $n - 1; $i++) { // only one condition // will be true at a // time either a[i] // will be greater than // neighbours or less // than neighbours // check if a[i] is greater // than both its neighbours // then add 1 to x $count += ($a[$i] > $a[$i - 1] and $a[$i] > $a[$i + 1]); // check if a[i] is // less than both its // neighbours, then // add 1 to x $count += ($a[$i] < $a[$i - 1] and $a[$i] < $a[$i + 1]); } return $count; } // Driver Code $a = array( 1, 0, 2, 1 ); $n = count($a); echo extrema($a, $n); // This code is contributed by anuj_67. ?> Output2Complexity Analysis:Time Complexity: O(n) where n is size of input array. This is because a for loop is executing from 1 to n.Space Complexity: O(1) as no extra space has been used.Please refer complete article on Number of local extrema in an array for more details! Comment More infoAdvertise with us Next Article PHP Program for Number of local extrema in an array kartik Follow Improve Article Tags : PHP Similar Reads How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra 2 min read PHP program to find the maximum and the minimum in array Finding the maximum and minimum in an array involves determining the largest and smallest values within a given set of numbers. This task is crucial for analyzing data, identifying trends, or filtering outliers. Various methods, from simple traversal to optimized algorithms, can achieve this.Example 7 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 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 PHP program to find the Standard Deviation of an array Given an array of elements. We need to find the Standard Deviation of the elements of the array in PHP. Examples: Input : array(2, 3, 5, 6, 7)Output : 1.5620499351813Input : array(1, 2, 3, 4, 5)Output : 1 The following problem can be solved using the PHP inbuilt functions. The inbuilt functions used 2 min read How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte 3 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 PHP Program to Find the Maximum Element in a Matrix Given a matrix, the task is to find the maximum element of the matrix (arrays of arrays) in PHP. Finding the maximum element in a matrix is a common task that involves iterating through all the elements of the matrix and keeping track of the largest value found. Example: Input: matrix = [ [1, 2, 3], 4 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 the Number Occurring Odd Number of Times Write a PHP program for a given array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space. Examples : Input: arr = {1, 2, 3, 2, 3, 1, 3}Output : 3 Input: arr = {5, 7, 2, 7, 5, 2, 5}Ou 3 min read Like