...

/

Solution: Find the Peak Element

Solution: Find the Peak Element

Review various solution approaches to find the peak element in detail.

Solution 1

One simple way to solve this problem is the following:

using System;
class Program
{
/// <summary>
/// Finds a peak element in a array of integers.
/// </summary>
/// <param name="arr">Array of integers.</param>
/// <returns>A peak element in the given array.</returns>
public static int FindPeak(int[] arr)
{
// If the array is empty
if (arr.Length == 0)
{
return -1;
}
// If the array has only one element
if (arr.Length == 1)
{
return arr[0];
}
for (int i = 1; i < arr.Length - 1; i++)
{
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
{
return arr[i];
}
}
if (arr[0] >= arr[1])
{
return arr[0];
}
else if (arr[arr.Length - 1] >= arr[arr.Length - 2])
{
return arr[arr.Length - 1];
}
return -1;
}
// Driver code to test the above method
public static void Main(string[] args)
{
// Example 1
int[] arr1 = { 7, 11, 22, 13, 4, 0 };
Console.WriteLine("One peak point is: " + FindPeak(arr1));
// Example 2
int[] arr2 = { 0, 3, 100, 2, -1, 0 };
Console.WriteLine("One peak point is: " + FindPeak(arr2));
// Example 3
int[] arr3 = { 6, 5, 4, 3, 2, 1 };
Console.WriteLine("One peak point is: " + FindPeak(arr3));
}
}

Explanation

  1. We start from the beginning and compare each element with its neighbors.
  2. We return the peak element wherever we find it in the array.

If the array is sorted in an increasing order with no repetition, then the last element is ...