
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
#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.
- Related Articles
- Peak Element in 2D array
- Find a peak element in C++
- Find a peak element in Linked List in C++
- Find Peak Element in Python
- C# program to find K’th smallest element in a 2D array
- Python program to find k'th smallest element in a 2D array
- Find most common element in a 2D list in Python
- Program to find local peak element indices from a list of numbers in Python
- How to store a 2d Array in another 2d Array in java?
- Finding peak of a centrally peaked array in JavaScript
- C++ Program to Find the peak element of an array using Binary Search approach
- Compute the bit-wise XOR of a 1D and a 2D array element-wise in Numpy
- Compute the bit-wise AND of a 1D and a 2D array element-wise in Numpy
- Compute the bit-wise OR of a 1D and a 2D array element-wise in Numpy
- Scatter a 2D numpy array in matplotlib
