Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- A peak element is an element that is strictly greater than its neighbors. Given an integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks. You may imagine that nums[-1] = nums[n] = -∞. You must write an algorithm that runs in O(log n) time.
- Input format
- The first line contains an integer N, the size of the input array. The second line contains N integers, the elements of the given array.
- Output format
- Return a single integer, the index of the peak element in the array (0-based).
- Sample Input 1
- 4
- 1 2 3 1
- Sample Output 1
- 2
- Explanation
- 3 is a peak element and your function should return the index number 2.
- Constraints
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 2^31 - 1
- nums[i] != nums[i + 1] for all valid i
- */
- /**
- * @param {number} N
- * @param {number[]} arr
- * @return {number}
- */
- /*
- Let's break down the intuition behind finding a peak element efficiently using binary search, making it easy to remember for interviews:
- 1. **Initialization**: We start with the entire array and define `low` as the left boundary and `high` as the right boundary.
- 2. **Binary Search Loop**: We enter a loop that continues until `low` is less than or equal to `high`. This loop helps us narrow down the search space.
- 3. **Midpoint Calculation**: In each iteration, we calculate the midpoint `mid` as `(low + high) / 2`.
- 4. **Comparison with Neighbors**: We compare the element at index `mid` with its neighbors, `arr[mid - 1]` and `arr[mid + 1]`.
- 5. **Peak Check**: If `arr[mid]` is greater than both its neighbors (`arr[mid] > arr[mid - 1]` and `arr[mid] > arr[mid + 1]`), we've found a peak element. We return `mid` as the index of the peak element.
- 6. **Move Left or Right**: If `arr[mid]` is not a peak (i.e., it's smaller than at least one of its neighbors), we decide whether to move left or right:
- - If `arr[mid]` is greater than `arr[mid - 1]`, we set `low = mid + 1` because the peak must be on the right side.
- - Otherwise, if `arr[mid]` is less than or equal to `arr[mid - 1]`, we set `high = mid - 1` because the peak must be on the left side.
- 7. **Loop Continues**: We continue this process, halving the search space in each iteration, until `low` becomes greater than `high`. This means we've narrowed down the search to a single element.
- 8. **Peak Found**: We return the index of the element pointed to by `low` as the peak element.
- The key insight here is that, at each step, we make a decision to move towards the side where the peak element is guaranteed to exist. This ensures that we efficiently converge to a peak element in O(log n) time complexity.
- */
- // https://www.youtube.com/watch?v=cXxmbemS6XM
- function peakElement(N,arr) {
- if(N==1)
- return N-1
- if(arr[0]>arr[1])
- return 0;
- if(arr[N-1]>arr[N-2])
- return N-1;
- let low=1;
- let high=N-2;
- let mid;
- while(low<=high){
- mid=Math.floor((low+high)/2);
- if(arr[mid]>arr[mid-1]&&arr[mid]>arr[mid+1])
- return mid;
- else if(arr[mid]>arr[mid-1])
- low=mid+1;
- else
- high=mid-1;
- }
- return -1;
- }
- function main() {
- const [N] = readLine().split(" ").map(Number);
- const nums = readIntArr();
- console.log(peakElement(N,nums));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement