Find Peak Element

Binary Search 2D Arrays Hard
  • This problem statement to find a peak element in a 2D grid can be seen as a core concept behind many terrain mapping systems in software like geographic information systems (GIS)
  • In these scenarios, different altitudes can be represented as different values in the matrix, a 'peak' would represent the highest point in surrounding region (like a hill or mountain)
  • Algorithms for finding such peak elements are crucial when it comes to features like terrain visualization, path planning or shortest path computations in uneven landscapes

Given a 0-indexed n x m matrix mat where no two adjacent cells are equal, find any peak element mat[i][j] and return the array [i, j].A peak element in a 2D grid is an element that is strictly greater than all of its adjacent neighbours to the left, right, top, and bottom.


Assume that the entire matrix is surrounded by an outer perimeter with the value -1 in each cell.

Examples:

Input: mat=[[10, 20, 15], [21, 30, 14], [7, 16, 32]]

Output: [1, 1]

Explanation: The value at index [1, 1] is 30, which is a peak element because all its neighbours are smaller or equal to it. Similarly, {2, 2} can also be picked as a peak.

Input: mat=[[10, 7], [11, 17]]

Output : [1, 1]

Explanation: Both 3 and 4 are peak elements so [1,0] and [0,1] are both acceptable answers.

Input: mat=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Constraints

  •   n == mat.length
  •   m == mat[i].length
  •   1 <= m, n <= 500
  •   1 <= mat[i][j] <= 105
  •   No two adjacent cells are equal

Hints

  • For a given column or row, find the global maximum and compare it with its adjacent elements in the matrix.
  • "Start by selecting the middle column (mid) of the matrix. Find the row index of the maximum element in that column. Compare this element with its left and right neighbors: If it is greater than its neighbors, it is a peak. If a neighbor is greater, move to the left or right half of the columns based on the larger neighbor and repeat the process."

Company Tags

Zynga Target American Express Mastercard Snowflake Zoho MongoDB Goldman Sachs Byju's Nutanix Optum Rakuten Electronic Arts KPMG HCL Technologies Deloitte Rockstar Games Roche Bungie Ubisoft Bain & Company Red Hat Riot Games Reddit Freshworks Google Microsoft Amazon Meta Apple Netflix Adobe