18(May) Find the Highest number
18. Find the Highest Number
The problem can be found at the following link: Question Link
Problem Description
Given an integer array a[] of size n, find the highest element of the array. The array will either be strictly increasing or strictly increasing and then strictly decreasing.
Note: a[i] != a[i+1]
Example:
Input:
11
1 2 3 4 5 6 5 4 3 2 1Output:
6Explanation: Highest element of array a[] is 6.
My Approach
Initialization:
Use the standard library function
max_elementto find the maximum value in the array.
Peak Element Search:
The function
max_elementscans through the array to identify the largest element.
Return:
Return the value found by
max_element.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(log(n)), as the
max_elementfunction iterates through the array exactly once to find the maximum element.Expected Auxiliary Space Complexity: O(1), as no additional space is required beyond the input array.
Code (C++)
class Solution {
public:
int findPeakElement(vector<int>& a)
{
return *max_element(a.begin(), a.end());
}
};Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated