Advertisement
satishfrontenddev5

Untitled

Jan 6th, 2024
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. Input format
  5. 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.
  6.  
  7. Output format
  8. Return a single integer, the index of the peak element in the array (0-based).
  9.  
  10. Sample Input 1
  11. 4
  12.  
  13. 1 2 3 1
  14.  
  15. Sample Output 1
  16. 2
  17.  
  18. Explanation
  19. 3 is a peak element and your function should return the index number 2.
  20.  
  21. Constraints
  22. 1 <= nums.length <= 1000
  23.  
  24. 1 <= nums[i] <= 2^31 - 1
  25.  
  26. nums[i] != nums[i + 1] for all valid i
  27. */
  28.  
  29.  
  30. /**
  31.  * @param {number} N
  32.  * @param {number[]} arr
  33.  * @return {number}
  34.  */
  35.  
  36. /*
  37. Let's break down the intuition behind finding a peak element efficiently using binary search, making it easy to remember for interviews:
  38.  
  39. 1. **Initialization**: We start with the entire array and define `low` as the left boundary and `high` as the right boundary.
  40.  
  41. 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.
  42.  
  43. 3. **Midpoint Calculation**: In each iteration, we calculate the midpoint `mid` as `(low + high) / 2`.
  44.  
  45. 4. **Comparison with Neighbors**: We compare the element at index `mid` with its neighbors, `arr[mid - 1]` and `arr[mid + 1]`.
  46.  
  47. 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.
  48.  
  49. 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:
  50.    - If `arr[mid]` is greater than `arr[mid - 1]`, we set `low = mid + 1` because the peak must be on the right side.
  51.    - 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.
  52.  
  53. 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.
  54.  
  55. 8. **Peak Found**: We return the index of the element pointed to by `low` as the peak element.
  56.  
  57. 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.
  58.  
  59. */
  60.  
  61. // https://www.youtube.com/watch?v=cXxmbemS6XM
  62. function peakElement(N,arr) {
  63.     if(N==1)
  64.     return N-1
  65.     if(arr[0]>arr[1])
  66.     return 0;
  67.     if(arr[N-1]>arr[N-2])
  68.     return N-1;
  69.     let low=1;
  70.     let high=N-2;
  71.     let mid;
  72.     while(low<=high){
  73.         mid=Math.floor((low+high)/2);
  74.         if(arr[mid]>arr[mid-1]&&arr[mid]>arr[mid+1])
  75.         return mid;
  76.         else if(arr[mid]>arr[mid-1])
  77.         low=mid+1;
  78.         else
  79.         high=mid-1;
  80.     }
  81.     return -1;
  82.  
  83. }
  84.  
  85. function main() {
  86.     const [N] = readLine().split(" ").map(Number);
  87.     const nums = readIntArr();
  88.     console.log(peakElement(N,nums));
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement