📖 Scenario: Imagine you have a list of numbers representing the heights of hills along a trail. You want to find a hill that is taller than its neighbors, called a peak. This helps hikers find the highest point nearby quickly.
🎯 Goal: Build a program that uses binary search to find any peak element in a list of numbers efficiently.
📋 What You'll Learn
Create an array called heights with specific numbers
Create a variable left and right to hold the start and end indexes
Use a while loop with binary search logic to find a peak element index
Print the index of the peak element found
💡 Why This Matters
🌍 Real World
Finding peak elements is useful in signal processing, stock price analysis, and terrain mapping where local maximum points matter.
💼 Career
Understanding binary search and peak finding algorithms is important for software engineers working on performance-critical applications and data analysis.
Progress0 / 4 steps
1
Create the heights array
Create an array called heights with these exact numbers: [1, 3, 20, 4, 1, 0]
DSA Typescript
Hint
Use const heights = [1, 3, 20, 4, 1, 0]; to create the array.
2
Set up binary search boundaries
Create two variables called left and right. Set left to 0 and right to the last index of heights.
DSA Typescript
Hint
Use let left = 0; and let right = heights.length - 1;.
3
Implement binary search to find peak
Use a while loop with the condition left < right. Inside the loop, calculate mid as the middle index between left and right. If heights[mid] is less than heights[mid + 1], set left to mid + 1. Otherwise, set right to mid.
DSA Typescript
Hint
Use Math.floor((left + right) / 2) to find mid. Compare heights[mid] and heights[mid + 1] to decide which side to keep.
4
Print the peak element index
Print the value of left, which is the index of a peak element.
DSA Typescript
Hint
The peak element index is stored in left. Use console.log(left); to print it.
Practice
(1/5)
1. What is a peak element in an array when using binary search to find it?
easy
A. An element that is greater than its neighbors
B. The smallest element in the array
C. The first element of the array
D. An element equal to its neighbors
Solution
Step 1: Understand the definition of a peak element
A peak element is one that is strictly greater than its neighbors on both sides (or just one side if at the boundary).
Step 2: Apply this to binary search context
Binary search finds such an element by comparing mid element with neighbors to decide which half to search next.
Final Answer:
An element that is greater than its neighbors -> Option A
Quick Check:
Peak element = greater than neighbors [OK]
Hint: Peak element is bigger than neighbors, not smallest [OK]
Common Mistakes:
Thinking peak is smallest element
Confusing peak with first or last element always
Assuming peak equals neighbors
2. Which of the following is the correct TypeScript syntax to start a binary search loop for finding a peak element in array nums?
easy
A. while(left < right) { ... }
B. if(nums[mid] > nums[mid+1]) { ... }
C. for(let i = 0; i < nums.length; i++)
D. switch(nums[mid]) { ... }
Solution
Step 1: Identify the loop type for binary search
Binary search uses a while loop with condition left < right to narrow down the search range.
Step 2: Check options for correct binary search syntax
while(left < right) { ... } shows the correct while loop structure for binary search.