📖 Scenario: Imagine you have a list of numbers representing 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 finds a peak element in a list of integers using binary search. The program will efficiently find a hill taller than its neighbors without checking every hill.
📋 What You'll Learn
Create an integer array called heights with specific values
Create two integer variables left and right to mark the search range
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 quickly helps in signal processing, stock market analysis, and terrain mapping where local maximum points are important.
💼 Career
Understanding binary search and peak finding algorithms is useful for software engineers working on performance-critical applications and algorithm design.
Progress0 / 4 steps
1
Create the Heights Array
Create an integer array called heights with these exact values: 1, 3, 20, 4, 1, 0, 5
DSA C#
Hint
Use int[] heights = { ... }; to create the array with the exact numbers.
2
Set Search Range Variables
Create two integer variables called left and right. Set left to 0 and right to heights.Length - 1
DSA C#
Hint
Use int left = 0; and int right = heights.Length - 1; to set the search range.
3
Implement Binary Search to Find Peak
Use a while loop with the condition left < right. Inside the loop, create an integer mid as the middle index between left and right. If heights[mid] is greater than heights[mid + 1], set right to mid. Otherwise, set left to mid + 1. After the loop ends, left will be the peak index.
DSA C#
Hint
Use binary search logic comparing heights[mid] and heights[mid + 1] to move left or right.
4
Print the Peak Element Index
Print the value of left, which is the index of the peak element found.
DSA C#
Hint
Use System.Console.WriteLine(left); to print the peak index.
Practice
(1/5)
1. What is a peak element in an array when using binary search to find it?
easy
A. The first element of the array
B. An element that is smaller than its neighbors
C. An element that is greater than its neighbors
D. The last element of the array
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 the search direction.
Final Answer:
An element that is greater than its neighbors -> Option C
Quick Check:
Peak element = greater than neighbors [OK]
Hint: Peak element > neighbors, use binary search to find it fast [OK]
Common Mistakes:
Confusing peak with minimum element
Thinking peak must be at array ends
Assuming peak equals maximum element in array
2. Which of the following is the correct C# syntax to calculate the middle index in binary search to avoid overflow?
easy
A. int mid = (low + high) / 2;
B. int mid = (high - low) / 2 + high;
C. int mid = (low - high) / 2;
D. int mid = low + (high - low) / 2;
Solution
Step 1: Understand overflow risk in mid calculation
Using (low + high) / 2 can overflow if low and high are large integers.
Step 2: Use safe calculation to avoid overflow
Calculate mid as low + (high - low) / 2 to prevent overflow.