Skip to content

Latest commit

 

History

History
32 lines (28 loc) · 738 Bytes

PeakElement.md

File metadata and controls

32 lines (28 loc) · 738 Bytes
 int peakElement(int arr[], int n)
    {
       if (n == 1)
            return 0;

        for (int i = 0; i < n; i++)
        {      
            if (i == 0)
            {
                if (arr[i] >= arr[i + 1])
                    return i;
            }
            else if (i == n - 1)
            {
                if (arr[i] >= arr[i - 1])
                    return i;
            }
            else
            {
                if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
                    return i;
            }
        }

        // If no peak element is found, return -1.
        return -1;
    }