C++ code to count local extrema of given array22 Mar 2025 | 4 min read Extrema are such numbers that either have a minimum or a maximum. In other words, it refers to a value or element that is either larger or smaller than both of its neighboring values. Assume we have n elements in the array A. A local minimum is an element of this arrayof A[i] if and only if it is strictly less than both of its neighbors. A local maximum will also be reached if it is strictly greater than its neighbors. As they have a single neighbor, A[0] and A[n-1] are neither maxima nor minima. We must determine the number of local extrema within the provided array. Example:Approach 1: Using For LoopWe will use a for loop to iterate through the elements of the array starting from the second and ending with the second-to-last. This prevents the first and last elements from ever being at extremes. We will use the for loop and if statements to find out if each element is greater or less than both of its neighboring elements. Either of the two conditions results in a one-time increase in the variable num. Example: Output: Number of local extrema in the array: 3 Approach 2: C++ Standard Template LibraryIn this approach, we may make use of the STL (Standard Template Library) algorithm's maximum and minimum operations. Predefined functions called std::max() and std::min() are utilised to return the largest and smallest element of a given range of an array. Syntax: It has the following syntax: Parameters:
For std::max({element 1, element 2, element 3}): The current element and both of its neighbors are the three elements we must take into consideration at once in order to locate the local extrema. Thus, we have three elements in our range. Because of this, we use curly braces to specify those three items. Here, we repeat the loop from the second to the last element in the supplied array. Among x[i - 1], x[i], and x[i + 1], we determine the maximum and minimum element. Number is increased by one if the current element equals either of the two. Example: Let us take an example to count local extrema of a given array in C++. Output: Number of local extrema in the array: 2 Conclusion:In conclusion, we discussed about various methods for locating the local extrema within an array. We learned about the basic iterative method in the first approach, which uses a for loop. In the second method, we have utilized the std::min() and std::max() functions. Next TopicHypercube-sort-in-cpp |
C++ is an object-oriented programming language that offers developers great control over how they want to structure their code. One of the advantages arising from this flexibility and reusability is the template mechanism, through which various functional and class-like concepts can contain these types. However,...
13 min read
Introduction When looking for a solution to a problem that requires tackling numerous options or a considerable volume of data, a brute force method can take far too long. The Meet-in-the-Middle approach is an efficient way to split a problem, which makes it simpler than attempting...
4 min read
Connecting to a serial port in C++ is a common requirement for applications involving hardware communication, such as interfacing with sensors, modems, or embedded systems. Serial communication allows data to be transmitted one bit at a time over a communication channel, making it ideal for simple,...
10 min read
A string representing the final contest matches of n teams is returned. The teams are ranked from 1 to 𝑛, with Rank 1 being the best team and Rank n being the worst. The labels correspond to the teams' initial ranks. The matching procedure represents team...
4 min read
Thabit numbers, named after the renowned Arab mathematician Thābit ibn Qurra (826−901 CE), are an intriguing class of numbers in number theory. These numbers, defined by a simple mathematical formula, have been studied for centuries due to their interesting properties, connection to primality testing, and...
8 min read
In this article, we will discuss the differences between Const and Mutable in C++. In C++, const and mutable are keywords that play significant roles in defining the circumstances under which data is modified. Without the knowledge of their basic operational functionalities, any programmer will...
6 min read
C++ has two behavioral design patterns: the State Pattern and the Strategy Pattern. However, their functions are distinct. An object can choose from a variety of algorithms at runtime to change its behavior due to the Strategy Pattern. Disregarding the internal state of the object, its...
11 min read
Introduction Templates and Generics give us some powerful abilities to write flexible and reusable codes in C++. Nevertheless, such techniques can become quite involved when it comes to dealing with types. One of the most common problems is associated with references as variables. When faced with this...
7 min read
Introduction HITS is a link analysis algorithm used predominantly in web search; it was stemming from the work of Jon Kleinberg and the algorithm is named Hyperlink-Induced Topic Search. Unlike PageRank, which considers overall popularity, HITS identifies two types of pages: hubs and authorities. Any given...
14 min read
In the realm of number theory and combinatorics, the Frobenius number is a well-known concept that emerges from a classic mathematical problem known as the coin problem or Chicken McNugget problem in recreational mathematics. This problem revolves around the idea of determining the largest integer...
8 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India