{"id":576,"date":"2025-07-09T18:02:09","date_gmt":"2025-07-09T12:32:09","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=576"},"modified":"2025-07-09T18:02:11","modified_gmt":"2025-07-09T12:32:11","slug":"find-peak-element-leetcode-162","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/","title":{"rendered":"Find Peak Element | Leetcode 162 | Graph Binary Search"},"content":{"rendered":"\n<p>The &#8220;Find Peak Element&#8221; problem is a&nbsp;classic question&nbsp;that helps you&nbsp;practice binary&nbsp;search and array&nbsp;manipulation. In this blog, we\u2019ll explain&nbsp;the problem, walk&nbsp;you through both&nbsp;brute force and&nbsp;optimal solutions, and make everything&nbsp;easy to understand&nbsp;with code comments, dry runs, and&nbsp;clear explanations.<\/p>\n\n\n\n<p>A peak element is an element that is strictly greater than its neighbors.<\/p>\n\n\n\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, find a peak element, and return its index. If the array contains multiple peaks, return the index to&nbsp;<strong>any of the peaks<\/strong>.<\/p>\n\n\n\n<p>You may imagine that&nbsp;<code>nums[-1] = nums[n] = -\u221e<\/code>. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.<\/p>\n\n\n\n<p>You must write an algorithm that runs in&nbsp;<code>O(log n)<\/code>&nbsp;time.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/find-peak-element\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> nums = [1,2,3,1]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> 3 is a peak element and your function should return the index number 2.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> nums = [1,2,1,3,5,6,4]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= nums.length &lt;= 1000<\/code><\/li>\n\n\n\n<li><code>-2<sup>31<\/sup> &lt;= nums[i] &lt;= 2<sup>31<\/sup> - 1<\/code><\/li>\n\n\n\n<li><code>nums[i] != nums[i + 1]<\/code>&nbsp;for all valid&nbsp;<code>i<\/code>.<\/li>\n<\/ul>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-47642046-000e-4290-9143-95e3c80d27c4\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#0-brute-force-solution-linear-scan\" style=\"\">Brute Force Solution (Linear Scan)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#1-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#3-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#4-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#5-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#6-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#7-optimal-solution-binary-search\" style=\"\">Optimal Solution (Binary Search)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#8-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#9-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#10-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#11-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#12-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#13-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/find-peak-element-leetcode-162\/#14-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-brute-force-solution-linear-scan\">Brute Force Solution (Linear Scan)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s solve the problem in the most straightforward way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Check Edge Cases:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If the array has only one element, it&#8217;s a peak.<\/li>\n\n\n\n<li>If the first element is greater than the second, it&#8217;s a peak.<\/li>\n\n\n\n<li>If the last element is greater than the second last, it&#8217;s a peak.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Scan Each Element:<\/strong><br>For the rest, check if an element is greater than both its neighbors.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We check every possible peak, so we\u2019re sure to find one.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not the most efficient for large arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def findPeakElement(self, nums: List[int]) -&gt; int:\n        n = len(nums)\n        if n == 1:\n            return 0\n        elif nums[0] &gt; nums[1]:\n            return 0\n        elif nums[n - 1] &gt; nums[n - 2]:\n            return n - 1\n        for i in range(1, n - 1):\n            if nums[i] &gt; nums[i - 1] and nums[i] &gt; nums[i + 1]:\n                return i\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">findPeakElement<\/span><span style=\"color: #E1E4E8\">(self, nums: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">]) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> n <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">elif<\/span><span style=\"color: #E1E4E8\"> nums[<\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\">] <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> nums[<\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">elif<\/span><span style=\"color: #E1E4E8\"> nums[n <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">] <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> nums[n <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> n <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">, n <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> nums[i] <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> nums[i <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">] <\/span><span style=\"color: #F97583\">and<\/span><span style=\"color: #E1E4E8\"> nums[i] <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> nums[i <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> i<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Handle special cases for arrays of size 1, or if the peak is at the start or end.<\/li>\n\n\n\n<li>For the rest, check each element (except first and last) to see if it&#8217;s a peak.<\/li>\n\n\n\n<li>Return the index as soon as a peak is found.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>nums = [1][1]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>n = 4<\/li>\n\n\n\n<li>nums = 1 &lt; nums<a href=\"https:\/\/leetcode.com\/problems\/find-peak-element\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>&nbsp;= 2<\/li>\n\n\n\n<li>nums = 1 &lt; nums = 3<\/li>\n\n\n\n<li>i = 1: nums<a href=\"https:\/\/leetcode.com\/problems\/find-peak-element\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>&nbsp;= 2 &lt; nums = 3<\/li>\n\n\n\n<li>i = 2: nums = 3 &gt; nums<a href=\"https:\/\/leetcode.com\/problems\/find-peak-element\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>&nbsp;= 2 and nums = 3 &gt; nums = 1 \u2192 return 2<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>2<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O(n)<br>We may have to check every element.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(1)<br>Only a variable for looping.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple and works for small arrays, but it\u2019s not the best for large inputs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-optimal-solution-binary-search\">Optimal Solution (Binary Search)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>We can do much better using binary search:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Binary Search:<\/strong><br>Use two pointers (<code>low<\/code>&nbsp;and&nbsp;<code>high<\/code>) to perform binary search.<\/li>\n\n\n\n<li><strong>Check Neighbors:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If the middle element is a peak, return it.<\/li>\n\n\n\n<li>If the middle element is less than its right neighbor, the peak must be on the right.<\/li>\n\n\n\n<li>Otherwise, the peak is on the left.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>If you always move towards the greater neighbor, you are guaranteed to find a peak.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def findPeakElement(self, nums: List[int]) -&gt; int:\n        if len(nums) == 1:\n            return 0\n        if nums[0] &gt; nums[1]:\n            return 0\n        elif nums[-1] &gt; nums[-2]:\n            return len(nums) - 1\n        low = 1\n        high = len(nums) - 2\n        while low &lt;= high:\n            mid = (low + high) \/\/ 2\n            if nums[mid] &gt; nums[mid - 1] and nums[mid] &gt; nums[mid + 1]:\n                return mid\n            if nums[mid] &gt; nums[mid + 1]:\n                high = mid - 1\n            else:\n                low = mid + 1\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">findPeakElement<\/span><span style=\"color: #E1E4E8\">(self, nums: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">]) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums) <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> nums[<\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\">] <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> nums[<\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">elif<\/span><span style=\"color: #E1E4E8\"> nums[<\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">] <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> nums[<\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums) <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums) <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> low <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> high:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            mid <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> (low <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> high) <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> nums[mid] <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> nums[mid <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">] <\/span><span style=\"color: #F97583\">and<\/span><span style=\"color: #E1E4E8\"> nums[mid] <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> nums[mid <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> mid<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> nums[mid] <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> nums[mid <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Handle special cases for arrays of size 1, or if the peak is at the start or end.<\/li>\n\n\n\n<li>Use binary search between indices 1 and n-2.<\/li>\n\n\n\n<li>If the mid element is a peak, return its index.<\/li>\n\n\n\n<li>If the mid element is greater than its right neighbor, move left.<\/li>\n\n\n\n<li>Otherwise, move right.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>nums = [1][1]<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>len(nums) = 4<\/li>\n\n\n\n<li>nums = 1 &lt; nums<a href=\"https:\/\/leetcode.com\/problems\/find-peak-element\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>&nbsp;= 2<\/li>\n\n\n\n<li>nums = 1 &lt; nums = 3<\/li>\n\n\n\n<li>low = 1, high = 2<\/li>\n\n\n\n<li>mid = (1+2)\/\/2 = 1<\/li>\n\n\n\n<li>nums<a href=\"https:\/\/leetcode.com\/problems\/find-peak-element\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>&nbsp;= 2 &lt; nums = 3 \u2192 move right, low = 2<\/li>\n\n\n\n<li>mid = 2<\/li>\n\n\n\n<li>nums = 3 &gt; nums<a href=\"https:\/\/leetcode.com\/problems\/find-peak-element\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>&nbsp;= 2 and nums = 3 &gt; nums = 1 \u2192 return 2<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>2<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>&nbsp;O(log n)<br>Binary search divides the array each time.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>&nbsp;O(1)<br>Only a few pointers.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is efficient and works well for large arrays. It\u2019s the best approach for interviews and practical use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Find Peak Element&#8221; problem is a great example of how to optimize from brute force to an efficient binary search solution. Start with brute force to understand the basics, then use binary search for best performance.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;Find Peak Element&#8221; problem is a&nbsp;classic question&nbsp;that helps you&nbsp;practice binary&nbsp;search and array&nbsp;manipulation. In this blog, we\u2019ll explain&nbsp;the problem, walk&nbsp;you through both&nbsp;brute force and&nbsp;optimal solutions, and make everything&nbsp;easy to understand&nbsp;with code comments, dry runs, and&nbsp;clear explanations. A peak element is an element that is strictly greater than its neighbors. Given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums, find a peak<\/p>\n","protected":false},"author":1,"featured_media":578,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[7,27,18],"class_list":{"0":"post-576","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-array","10":"tag-binary-search","11":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/find-peak-element-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=576"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/576\/revisions"}],"predecessor-version":[{"id":580,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/576\/revisions\/580"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/578"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}