forked from ccgcv/Cplus-plus-for-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_A_Peak_Element_in_a_2D_Array
68 lines (58 loc) · 1.58 KB
/
Find_A_Peak_Element_in_a_2D_Array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Finding peak element in a 2D Array.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
// Function to find the maximum in column 'mid'
// 'rows' is number of rows.
int findMax(int arr[][MAX], int rows, int mid, int& max)
{
int max_index = 0;
for (int i = 0; i < rows; i++) {
if (max < arr[i][mid]) {
// Saving global maximum and its index
// to check its neighbours
max = arr[i][mid];
max_index = i;
}
}
return max_index;
}
// Function to find a peak element
int findPeakRec(int arr[][MAX], int rows, int columns,
int mid)
{
// Evaluating maximum of mid column. Note max is
// passed by reference.
int max = 0;
int max_index = findMax(arr, rows, mid, max);
// If we are on the first or last column,
// max is a peak
if (mid == 0 || mid == columns - 1)
return max;
// If mid column maximum is also peak
if (max >= arr[max_index][mid - 1] && max >= arr[max_index][mid + 1])
return max;
// If max is less than its left
if (max < arr[max_index][mid - 1])
return findPeakRec(arr, rows, columns, mid - ceil((double)mid / 2));
// If max is less than its left
// if (max < arr[max_index][mid+1])
return findPeakRec(arr, rows, columns, mid + ceil((double)mid / 2));
}
// A wrapper over findPeakRec()
int findPeak(int arr[][MAX], int rows, int columns)
{
return findPeakRec(arr, rows, columns, columns / 2);
}
// Driver Code
int main()
{
int arr[][MAX] = { { 10, 8, 10, 10 },
{ 14, 13, 12, 11 },
{ 15, 9, 11, 21 },
{ 16, 17, 19, 20 } };
// Number of Columns
int rows = 4, columns = 4;
cout << findPeak(arr, rows, columns);
return 0;
}