Find a peak element in a 2D array in C++


In this tutorial, we are going to write a program that finds the peak element in a 2D array.

An element is called a peak element if all the elements around it are smaller than the element.

Let's see the steps to solve the problem.

  • Initialize the 2D array with dummy data.

  • Iterate over the 2D array.

    • First, check the corner elements of the 2D array.

    • Next, write the conditions for the first and last rows of the 2D array.

    • Now, check for the first and last columns of the 2D array.

    • And finally, check the middle elements.

    • In each case, we have to compare the current element with its surrounding elements. It varies based on the above conditions.

    • Return the value wherever you find the result.

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
const int MAX = 256;
int findPeakElement(int arr[][MAX], int rows, int columns) {
   for (int i = 0; i < rows; i++) {
      for (int j = 0; j < columns; j++) {
         if (i == 0 && j == 0) {
            if (arr[i][j] > arr[i + 1][j] && arr[i][j] > arr[i][j + 1]) {
               return arr[i][j];
            }
         }
         else if (i == 0 && j == columns - 1) {
            if (arr[i][j] > arr[i + 1][j] && arr[i][j] > arr[i][j - 1]) {
               return arr[i][j];
            }
         }
         else if (i == rows - 1 && j == 0) {
            if (arr[i][j] > arr[i - 1][j] && arr[i][j] > arr[i][j + 1]) {
               return arr[i][j];
            }
         }
         else if (i == rows - 1 && j == columns - 1) {
            if (arr[i][j] > arr[i - 1][j] && arr[i][j] > arr[i][j - 1]) {
               return arr[i][j];
            }
         }
         else if (i == 0) {
            if (arr[i][j] > arr[i][j - 1] && arr[i][j] > arr[i][j + 1] && arr[i][j] > arr[i + 1][j]) {
               return arr[i][j];
            }
         }
         else if (i == rows - 1) {
            if (arr[i][j] > arr[i][j - 1] && arr[i][j] > arr[i][j + 1] && arr[i][j] > arr[i - 1][j]) {
               return arr[i][j];
            }
         }
         else if (j == 0) {
            if (arr[i][j] > arr[i - 1][j] && arr[i][j] > arr[i + 1][j] && arr[i][j] > arr[i][j + 1]) {
               return arr[i][j];
            }
         }
         else if (j == columns - 1) {
            if (arr[i][j] > arr[i - 1][j] && arr[i][j] > arr[i + 1][j] && arr[i][j] > arr[i][j - 1]) {
               return arr[i][j];
            }
         }
         else {
            if (arr[i][j] > arr[i][j - 1] && arr[i][j] > arr[i][j + 1] && arr[i][j] > arr[i - 1][j] && arr[i][j] > arr[i + 1][j]) {
               return arr[i][j];
            }
         }
      }
   }
   return -1;
}
int main() {
   int arr[][MAX] = {
      { 1, 2, 3, 4 },
      { 2, 3, 4, 5 },
      { 1, 3, 7, 5 },
      { 1, 2, 6, 6 } };
   int rows = 4, columns = 4;
   cout << findPeakElement(arr, rows, columns) << endl;
   return 0;
}

Output

If you run the above code, then you will get the following result.

7

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Updated on: 01-Feb-2021

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements