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 |
Before understanding the differences between the virtual function and pure virtual function in C++, we should know about the virtual function and pure virtual function in C++. What is virtual function? Virtual function is a member function that is declared within the base class and can be redefined...
5 min read
Introduction: The Memento pattern is a behavioral design pattern that is used to capture and externalize an object's internal state so that the object can be restored to this state later without violating encapsulation. This pattern is particularly useful when you need to implement undo mechanisms checkpoints,...
10 min read
In this article, we will discuss the difference between the Queue and Deque in C++. But before discussing their differences, we must know about the Queue and Deque. Introduction of queue A queue is a basic data structure in C++ that adheres to the First-In-First-Out (FIFO) concept. Elements...
9 min read
In C++, function overloading and function templates are flexible features used to improve the reusability of programs. However, they are aimed at different goals and applied in other contexts. This article explores the function overloading and function templates and how to use them through examples. What is...
4 min read
In this article, we will discuss the differences between Atomic Flags and Atomic Boolean in C++. Before discussing their differences, we must know about Atomic Flags and Atomic Booleans in C++. What is the Atomic Flag (std::atomic_flag)? The low-level C++ atomic type std::atomic_flag can be in one of...
4 min read
Convert int to string in C++ In C++, integer and string are two data types. An integer is mainly utilized to show the numbers, whereas the string is used to save a collection of characters. In C++, converting an integer (int) to a string (std::string) is...
6 min read
Proizvolov of Identity is an outstanding concept in combinatorial mathematics that combines permutations and arithmetic signatures of numbers. This is a juxtaposition that is purely theoretical, although frequently tackled for purposes of gaining more insights explicating summation, permutations, and between the two. Its identity stems...
8 min read
Sampling plays a role in data science and statistics, allowing us to extract a subset from a larger population. One efficient method is reservoir sampling, which involves selecting a fixed number of items (k) from a dataset or stream of size (n). This article aims to present...
6 min read
A hit counter thus has differential applications in a wide range of areas. For instance, performing web services is useful in tracking user flow, analyzing users' behavior, and managing resources. The main use of a hit counter is to count the number of times a particular...
20 min read
Introduction In computer science, sorting algorithms are fundamental tools that perform an important role in various applications, such as organizing data in databases and optimizing search operations. Burst Sort is one of the less known sorting algorithms, but it has some unique attributes and advantages for specific...
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