Find Peak Element
Learning 1D Array Binary Search DSA practice problem on Onlearn.
Difficulty: hard.
Topics: Finding the index of a peak element in an array, where a peak is defined as an element greater than both its neighbors considering array boundaries., Arrays, Binary Search, Time Complexity, Space Complexity, Linear Search, Loops, Conditional Statements, edge cases, complexity analysis, array traversal, brute force, binary search, array algorithms, Peak & Minimum Element Finding, Edge Case Handling in Arrays.
Given an array arr of length N, find the index (0 based) of a peak element. A peak element is defined as an element that is greater than both of its neighbors. Formally, if arr[i] is a peak element, then arr[i 1] < arr[i] and arr[i + 1] < arr[i]. If there are multiple peak elements, return the index of any one of them. Note: For the first element (arr[0]), its previous element should be considered as negative infinity. Similarly, for the last element (arr[N 1]), its next element should be considered as negative infinity. Input Specification: The input consists of a single line containing space separated integers representing the elements of the array arr. Output Specification: Return the 0 based index of any peak element found in the array. Examples: Example 1: Example 2: Example 3: Example 4: