How to find peak in c++

In this code, I calculate the number of peaks and check the result with the Findpeaks Matlab function [https://www.mathworks.com/help/signal/ref/findpeaks.html].
My code count is greater than the Matlab result, the number of peaks in my code is 180, but in matlab it is 172, can anyone help improve my code?

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <vector>
using namespace std;
int count = 0;
// Function that returns true if num is
// greater than both arr[i] and arr[j]
static bool isPeak(std::vector<int> arr, int n, int num, int i, int j) {

  // If num is smaller than the element
  // on the left (if exists)
  if (i > 0 && arr[i] >= num)
    return false;

  // If num is smaller than the element
  // on the right (if exists)
  if (j < n && arr[j] > num)
    return false;
  return true;
}

void printPeaksTroughs(std::vector<int> arr, int n) {
  //cout << "Peaks : ";
  // For every element
  for (int i = 0; i < n; i++) {

    // If the current element is a peak
    if (isPeak(arr, n, arr[i], i - 1, i + 1)) {
      // cout << arr[i] << " ";
      count++;
    }
  }
  cout << endl;
}

// Driver code
int main() {
  std::vector<int> arr = {
      5569, 5563, 5560, 5568, 5567, 5564, 5568, 5567, 5571, 5570, 5570, 5562,
      5551, 5556, 5566, 5576, 5579, 5564, 5563, 5567, 5568, 5572, 5551, 5552,
      5559, 5552, 5563, 5562, 5559, 5567, 5559, 5559, 5573, 5569, 5569, 5559,
      5554, 5560, 5558, 5569, 5571, 5563, 5551, 5559, 5571, 5565, 5556, 5559,
      5562, 5569, 5566, 5565, 5569, 5558, 5568, 5578, 5566, 5554, 5563, 5274,
      5264, 5554, 5563, 5566, 5561, 5571, 5558, 5550, 5562, 5554, 5555, 5556,
      5553, 5550, 5549, 5554, 5546, 5544, 5542, 5552, 5563, 5573, 5566, 5558,
      5563, 5571, 5558, 5559, 5574, 5569, 5560, 5563, 5569, 5555, 5564, 5562,
      5562, 5569, 5574, 5564, 5567, 5568, 5564, 5559, 5558, 5549, 5559, 5567,
      5564, 5554, 5558, 5563, 5563, 5559, 5562, 5563, 5553, 5567, 5560, 5561,
      5571, 5569, 5271, 5271, 5567, 5568, 5563, 5558, 5560, 5558, 5562, 5564,
      5554, 5562, 5557, 5556, 5558, 5565, 5569, 5572, 5561, 5568, 5564, 5564,
      5564, 5569, 5566, 5569, 5568, 5565, 5572, 5586, 5570, 5574, 5574, 5559,
      5557, 5563, 5565, 5562, 5573, 5569, 5569, 5556, 5559, 5553, 5561, 5552,
      5559, 5562, 5555, 5565, 5554, 5550, 5555, 5564, 5563, 5553, 5563, 5574,
      5569, 5566, 5560, 5562, 5558, 5273, 5264, 5554, 5551, 5553, 5564, 5561,
      5546, 5553, 5563, 5560, 5569, 5563, 5554, 5549, 5552, 5559, 5573, 5563,
      5572, 5568, 5556, 5568, 5556, 5556, 5548, 5556, 5563, 5566, 5569, 5571,
      5571, 5566, 5559, 5570, 5564, 5553, 5561, 5563, 5564, 5564, 5566, 5555,
      5554, 5561, 5566, 5566, 5583, 5573, 5571, 5579, 5571, 5571, 5558, 5556,
      5559, 5559, 5558, 5558, 5556, 5561, 5559, 5561, 5282, 5263, 5563, 5564,
      5557, 5549, 5551, 5564, 5566, 5556, 5555, 5561, 5559, 5566, 5558, 5563,
      5550, 5551, 5553, 5546, 5554, 5559, 5564, 5565, 5566, 5562, 5566, 5568,
      5563, 5557, 5566, 5563, 5553, 5559, 5559, 5568, 5575, 5568, 5556, 5563,
      5548, 5551, 5563, 5556, 5550, 5564, 5576, 5579, 5576, 5560, 5551, 5557,
      5558, 5569, 5558, 5564, 5566, 5566, 5566, 5567, 5554, 5558, 5569, 5263,
      5263, 5552, 5578, 5584, 5569, 5544, 5551, 5575, 5569, 5573, 5573, 5564,
      5563, 5562, 5564, 5562, 5557, 5552, 5557, 5566, 5559, 5546, 5556, 5563,
      5568, 5568, 5561, 5567, 5581, 5561, 5563, 5556, 5544, 5558, 5553, 5551,
      5558, 5571, 5564, 5572, 5569, 5561, 5568, 5555, 5543, 5553, 5562, 5557,
      5571, 5563, 5548, 5563, 5558, 5551, 5571, 5565, 5559, 5564, 5563, 5557,
      5562, 5561, 5269, 5266, 5569, 5576, 5567, 5559, 5554, 5553, 5554, 5554,
      5558, 5566, 5581, 5574, 5566, 5570, 5567, 5558, 5562, 5561, 5550, 5562,
      5566, 5564, 5565, 5562, 5563, 5571, 5558, 5556, 5560, 5567, 5563, 5566,
      5564, 5565, 5559, 5554, 5561, 5573, 5571, 5560, 5565, 5560, 5566, 5573,
      5569, 5568, 5563, 5556, 5563, 5568, 5570, 5563, 5554, 5561, 5558, 5559,
      5554, 5566, 5564, 5564, 5568, 5273, 5275, 5560, 5570, 5571, 5558, 5561,
      5564, 5561, 5559, 5563, 5568, 5554, 5554, 5545, 5564, 5566, 5571, 5563,
      5556, 5562, 5559, 5557, 5559, 5556, 5558, 5559, 5564, 5562, 5559, 5551,
      5546, 5563, 5553, 5559, 5556, 5560, 5556, 5559, 5551, 5548, 5566, 5564,
      5563, 5556, 5557, 5566, 5570, 5555, 5558, 5561, 5567, 5561, 5566, 5563,
      5564, 5564, 5558, 5566, 5560, 5564, 5564, 5560, 5270, 5271, 5565, 5554,
      5572, 5558, 5553, 5558, 5552, 5553, 5560, 5561, 5547, 5556, 5561, 5559,
      5563, 5561, 5555, 5559, 5564, 5556, 5563, 5571, 5549, 5553, 5551, 5554,
      5558, 5576, 5573, 5568, 5564, 5558, 5560, 5578, 5576, 5568, 5555, 5555,
      5564, 5576, 5563, 5565, 5556, 5561, 5563, 5559, 5551, 5568, 5568, 5569,
      5570, 5564, 5564, 5569, 5552, 5554, 5563, 5557, 5549, 5561, 5574, 5273,
      5262, 5561, 5548, 5553, 5564, 5563, 5572, 5561, 5558, 5554, 5561, 5576,
      5574, 5568, 5565, 5553, 5551, 5559, 5569, 5558, 5564, 5564, 5557, 5559,
      5546, 5555, 5564, 5559, 5561, 5563, 5569, 5571, 5563, 5556, 5548, 5541,
      5551, 5559, 5553, 5543, 5546, 5563, 5563, 5553, 5568, 5559, 5551, 5555,
      5556, 5554, 5562, 5563, 5546, 5551, 5564, 5565, 5568, 5563, 5564, 5568,
      5571, 5568, 5270, 5284, 5568, 5553};

  printPeaksTroughs(arr, arr.size());

  cout << "\nsize:" << count << "\n";
  return 0;
}
You aren't using the same definition as the matlab function in the case where there are plateaus; (several identical values in succession). Your LHS comparison is also not starting at the correct place.

One possibility - not optimised - is the following.

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
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>
using namespace std;

int countPeaks( const vector<int> &arr )
{
   int counter = 0;
   int n = arr.size();
   // Count as a peak from the left of any plateau; i.e. the LHS is strictly smaller and the first non-equal RHS is smaller
   for (int i = 1; i < n - 1; i++ )
   {
      if ( arr[i-1] < arr[i] && arr[i] >= arr[i+1] )          // Trigger possibility of a peak if the LHS is strictly smaller
      {
         while( i < n - 1 && arr[i] == arr[i+1] ) i++;        // Move across any plateau before comparing RHS
         if ( i < n - 1 && arr[i] > arr[i+1] ) counter++;     // Finally, check the RHS for strictly greater
      }
   }
   return counter;
}

int main()
{
   vector<int> arr = {
      5569, 5563, 5560, 5568, 5567, 5564, 5568, 5567, 5571, 5570, 5570, 5562,
      5551, 5556, 5566, 5576, 5579, 5564, 5563, 5567, 5568, 5572, 5551, 5552,
      5559, 5552, 5563, 5562, 5559, 5567, 5559, 5559, 5573, 5569, 5569, 5559,
      5554, 5560, 5558, 5569, 5571, 5563, 5551, 5559, 5571, 5565, 5556, 5559,
      5562, 5569, 5566, 5565, 5569, 5558, 5568, 5578, 5566, 5554, 5563, 5274,
      5264, 5554, 5563, 5566, 5561, 5571, 5558, 5550, 5562, 5554, 5555, 5556,
      5553, 5550, 5549, 5554, 5546, 5544, 5542, 5552, 5563, 5573, 5566, 5558,
      5563, 5571, 5558, 5559, 5574, 5569, 5560, 5563, 5569, 5555, 5564, 5562,
      5562, 5569, 5574, 5564, 5567, 5568, 5564, 5559, 5558, 5549, 5559, 5567,
      5564, 5554, 5558, 5563, 5563, 5559, 5562, 5563, 5553, 5567, 5560, 5561,
      5571, 5569, 5271, 5271, 5567, 5568, 5563, 5558, 5560, 5558, 5562, 5564,
      5554, 5562, 5557, 5556, 5558, 5565, 5569, 5572, 5561, 5568, 5564, 5564,
      5564, 5569, 5566, 5569, 5568, 5565, 5572, 5586, 5570, 5574, 5574, 5559,
      5557, 5563, 5565, 5562, 5573, 5569, 5569, 5556, 5559, 5553, 5561, 5552,
      5559, 5562, 5555, 5565, 5554, 5550, 5555, 5564, 5563, 5553, 5563, 5574,
      5569, 5566, 5560, 5562, 5558, 5273, 5264, 5554, 5551, 5553, 5564, 5561,
      5546, 5553, 5563, 5560, 5569, 5563, 5554, 5549, 5552, 5559, 5573, 5563,
      5572, 5568, 5556, 5568, 5556, 5556, 5548, 5556, 5563, 5566, 5569, 5571,
      5571, 5566, 5559, 5570, 5564, 5553, 5561, 5563, 5564, 5564, 5566, 5555,
      5554, 5561, 5566, 5566, 5583, 5573, 5571, 5579, 5571, 5571, 5558, 5556,
      5559, 5559, 5558, 5558, 5556, 5561, 5559, 5561, 5282, 5263, 5563, 5564,
      5557, 5549, 5551, 5564, 5566, 5556, 5555, 5561, 5559, 5566, 5558, 5563,
      5550, 5551, 5553, 5546, 5554, 5559, 5564, 5565, 5566, 5562, 5566, 5568,
      5563, 5557, 5566, 5563, 5553, 5559, 5559, 5568, 5575, 5568, 5556, 5563,
      5548, 5551, 5563, 5556, 5550, 5564, 5576, 5579, 5576, 5560, 5551, 5557,
      5558, 5569, 5558, 5564, 5566, 5566, 5566, 5567, 5554, 5558, 5569, 5263,
      5263, 5552, 5578, 5584, 5569, 5544, 5551, 5575, 5569, 5573, 5573, 5564,
      5563, 5562, 5564, 5562, 5557, 5552, 5557, 5566, 5559, 5546, 5556, 5563,
      5568, 5568, 5561, 5567, 5581, 5561, 5563, 5556, 5544, 5558, 5553, 5551,
      5558, 5571, 5564, 5572, 5569, 5561, 5568, 5555, 5543, 5553, 5562, 5557,
      5571, 5563, 5548, 5563, 5558, 5551, 5571, 5565, 5559, 5564, 5563, 5557,
      5562, 5561, 5269, 5266, 5569, 5576, 5567, 5559, 5554, 5553, 5554, 5554,
      5558, 5566, 5581, 5574, 5566, 5570, 5567, 5558, 5562, 5561, 5550, 5562,
      5566, 5564, 5565, 5562, 5563, 5571, 5558, 5556, 5560, 5567, 5563, 5566,
      5564, 5565, 5559, 5554, 5561, 5573, 5571, 5560, 5565, 5560, 5566, 5573,
      5569, 5568, 5563, 5556, 5563, 5568, 5570, 5563, 5554, 5561, 5558, 5559,
      5554, 5566, 5564, 5564, 5568, 5273, 5275, 5560, 5570, 5571, 5558, 5561,
      5564, 5561, 5559, 5563, 5568, 5554, 5554, 5545, 5564, 5566, 5571, 5563,
      5556, 5562, 5559, 5557, 5559, 5556, 5558, 5559, 5564, 5562, 5559, 5551,
      5546, 5563, 5553, 5559, 5556, 5560, 5556, 5559, 5551, 5548, 5566, 5564,
      5563, 5556, 5557, 5566, 5570, 5555, 5558, 5561, 5567, 5561, 5566, 5563,
      5564, 5564, 5558, 5566, 5560, 5564, 5564, 5560, 5270, 5271, 5565, 5554,
      5572, 5558, 5553, 5558, 5552, 5553, 5560, 5561, 5547, 5556, 5561, 5559,
      5563, 5561, 5555, 5559, 5564, 5556, 5563, 5571, 5549, 5553, 5551, 5554,
      5558, 5576, 5573, 5568, 5564, 5558, 5560, 5578, 5576, 5568, 5555, 5555,
      5564, 5576, 5563, 5565, 5556, 5561, 5563, 5559, 5551, 5568, 5568, 5569,
      5570, 5564, 5564, 5569, 5552, 5554, 5563, 5557, 5549, 5561, 5574, 5273,
      5262, 5561, 5548, 5553, 5564, 5563, 5572, 5561, 5558, 5554, 5561, 5576,
      5574, 5568, 5565, 5553, 5551, 5559, 5569, 5558, 5564, 5564, 5557, 5559,
      5546, 5555, 5564, 5559, 5561, 5563, 5569, 5571, 5563, 5556, 5548, 5541,
      5551, 5559, 5553, 5543, 5546, 5563, 5563, 5553, 5568, 5559, 5551, 5555,
      5556, 5554, 5562, 5563, 5546, 5551, 5564, 5565, 5568, 5563, 5564, 5568,
      5571, 5568, 5270, 5284, 5568, 5553};

   cout << countPeaks( arr ) << '\n';
}


172
Last edited on
ispeak is woefully overcomplicated, but its probably ok.
why not pass it the vector and index, and return vec[index-1] < vec[index] && vec[index +1] < vec[index] or whatever you want? Your version putting in num allows num to NOT BE the middle index, it could be 42 or whatever, a possible bug by misuse case.

your code has off the end errors. you can't check 0 and i-1 and you can't check n and n+1
you guard for that in inspeak, but it STILL RETURNS TRUE for the ends. You have to NOT SEND the endpoints or it will miscount.
Last edited on
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
69
70
#include <vector>
#include <iostream>

int main() {
    const std::vector<int> arr {
        5569, 5563, 5560, 5568, 5567, 5564, 5568, 5567, 5571, 5570, 5570, 5562,
        5551, 5556, 5566, 5576, 5579, 5564, 5563, 5567, 5568, 5572, 5551, 5552,
        5559, 5552, 5563, 5562, 5559, 5567, 5559, 5559, 5573, 5569, 5569, 5559,
        5554, 5560, 5558, 5569, 5571, 5563, 5551, 5559, 5571, 5565, 5556, 5559,
        5562, 5569, 5566, 5565, 5569, 5558, 5568, 5578, 5566, 5554, 5563, 5274,
        5264, 5554, 5563, 5566, 5561, 5571, 5558, 5550, 5562, 5554, 5555, 5556,
        5553, 5550, 5549, 5554, 5546, 5544, 5542, 5552, 5563, 5573, 5566, 5558,
        5563, 5571, 5558, 5559, 5574, 5569, 5560, 5563, 5569, 5555, 5564, 5562,
        5562, 5569, 5574, 5564, 5567, 5568, 5564, 5559, 5558, 5549, 5559, 5567,
        5564, 5554, 5558, 5563, 5563, 5559, 5562, 5563, 5553, 5567, 5560, 5561,
        5571, 5569, 5271, 5271, 5567, 5568, 5563, 5558, 5560, 5558, 5562, 5564,
        5554, 5562, 5557, 5556, 5558, 5565, 5569, 5572, 5561, 5568, 5564, 5564,
        5564, 5569, 5566, 5569, 5568, 5565, 5572, 5586, 5570, 5574, 5574, 5559,
        5557, 5563, 5565, 5562, 5573, 5569, 5569, 5556, 5559, 5553, 5561, 5552,
        5559, 5562, 5555, 5565, 5554, 5550, 5555, 5564, 5563, 5553, 5563, 5574,
        5569, 5566, 5560, 5562, 5558, 5273, 5264, 5554, 5551, 5553, 5564, 5561,
        5546, 5553, 5563, 5560, 5569, 5563, 5554, 5549, 5552, 5559, 5573, 5563,
        5572, 5568, 5556, 5568, 5556, 5556, 5548, 5556, 5563, 5566, 5569, 5571,
        5571, 5566, 5559, 5570, 5564, 5553, 5561, 5563, 5564, 5564, 5566, 5555,
        5554, 5561, 5566, 5566, 5583, 5573, 5571, 5579, 5571, 5571, 5558, 5556,
        5559, 5559, 5558, 5558, 5556, 5561, 5559, 5561, 5282, 5263, 5563, 5564,
        5557, 5549, 5551, 5564, 5566, 5556, 5555, 5561, 5559, 5566, 5558, 5563,
        5550, 5551, 5553, 5546, 5554, 5559, 5564, 5565, 5566, 5562, 5566, 5568,
        5563, 5557, 5566, 5563, 5553, 5559, 5559, 5568, 5575, 5568, 5556, 5563,
        5548, 5551, 5563, 5556, 5550, 5564, 5576, 5579, 5576, 5560, 5551, 5557,
        5558, 5569, 5558, 5564, 5566, 5566, 5566, 5567, 5554, 5558, 5569, 5263,
        5263, 5552, 5578, 5584, 5569, 5544, 5551, 5575, 5569, 5573, 5573, 5564,
        5563, 5562, 5564, 5562, 5557, 5552, 5557, 5566, 5559, 5546, 5556, 5563,
        5568, 5568, 5561, 5567, 5581, 5561, 5563, 5556, 5544, 5558, 5553, 5551,
        5558, 5571, 5564, 5572, 5569, 5561, 5568, 5555, 5543, 5553, 5562, 5557,
        5571, 5563, 5548, 5563, 5558, 5551, 5571, 5565, 5559, 5564, 5563, 5557,
        5562, 5561, 5269, 5266, 5569, 5576, 5567, 5559, 5554, 5553, 5554, 5554,
        5558, 5566, 5581, 5574, 5566, 5570, 5567, 5558, 5562, 5561, 5550, 5562,
        5566, 5564, 5565, 5562, 5563, 5571, 5558, 5556, 5560, 5567, 5563, 5566,
        5564, 5565, 5559, 5554, 5561, 5573, 5571, 5560, 5565, 5560, 5566, 5573,
        5569, 5568, 5563, 5556, 5563, 5568, 5570, 5563, 5554, 5561, 5558, 5559,
        5554, 5566, 5564, 5564, 5568, 5273, 5275, 5560, 5570, 5571, 5558, 5561,
        5564, 5561, 5559, 5563, 5568, 5554, 5554, 5545, 5564, 5566, 5571, 5563,
        5556, 5562, 5559, 5557, 5559, 5556, 5558, 5559, 5564, 5562, 5559, 5551,
        5546, 5563, 5553, 5559, 5556, 5560, 5556, 5559, 5551, 5548, 5566, 5564,
        5563, 5556, 5557, 5566, 5570, 5555, 5558, 5561, 5567, 5561, 5566, 5563,
        5564, 5564, 5558, 5566, 5560, 5564, 5564, 5560, 5270, 5271, 5565, 5554,
        5572, 5558, 5553, 5558, 5552, 5553, 5560, 5561, 5547, 5556, 5561, 5559,
        5563, 5561, 5555, 5559, 5564, 5556, 5563, 5571, 5549, 5553, 5551, 5554,
        5558, 5576, 5573, 5568, 5564, 5558, 5560, 5578, 5576, 5568, 5555, 5555,
        5564, 5576, 5563, 5565, 5556, 5561, 5563, 5559, 5551, 5568, 5568, 5569,
        5570, 5564, 5564, 5569, 5552, 5554, 5563, 5557, 5549, 5561, 5574, 5273,
        5262, 5561, 5548, 5553, 5564, 5563, 5572, 5561, 5558, 5554, 5561, 5576,
        5574, 5568, 5565, 5553, 5551, 5559, 5569, 5558, 5564, 5564, 5557, 5559,
        5546, 5555, 5564, 5559, 5561, 5563, 5569, 5571, 5563, 5556, 5548, 5541,
        5551, 5559, 5553, 5543, 5546, 5563, 5563, 5553, 5568, 5559, 5551, 5555,
        5556, 5554, 5562, 5563, 5546, 5551, 5564, 5565, 5568, 5563, 5564, 5568,
        5571, 5568, 5270, 5284, 5568, 5553};

    size_t cnt {};

    for (auto it {arr.begin() + 1}, ite {arr.end() - 1}; it < ite; ++it)
        if (*(it - 1) < *it  && *(it + 1) <= *it) {
            for (; it < ite && *it == *(it + 1); ++it);
            if (*it > *(it + 1))
                ++cnt;
        }

    std::cout << cnt << '\n';
}

@lastchane thank you
Or with a state machine:

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
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <vector>
using namespace std;

int countPeaks( const vector<int> &arr )
{
   int counter = 0;
   bool rising = false;
   // Count as a peak from the left of any plateau; i.e. the LHS is strictly smaller and the first non-equal RHS is smaller
   for ( int i = 1; i < arr.size() - 1; i++  )
   {
      if ( arr[i-1] < arr[i] ) rising = true;
      if ( arr[i] > arr[i+1] )
      {
         if ( rising ) counter++;
         rising = false;
      }
   }
   return counter;
}

int main()
{
   vector<int> arr = {
      5569, 5563, 5560, 5568, 5567, 5564, 5568, 5567, 5571, 5570, 5570, 5562,
      5551, 5556, 5566, 5576, 5579, 5564, 5563, 5567, 5568, 5572, 5551, 5552,
      5559, 5552, 5563, 5562, 5559, 5567, 5559, 5559, 5573, 5569, 5569, 5559,
      5554, 5560, 5558, 5569, 5571, 5563, 5551, 5559, 5571, 5565, 5556, 5559,
      5562, 5569, 5566, 5565, 5569, 5558, 5568, 5578, 5566, 5554, 5563, 5274,
      5264, 5554, 5563, 5566, 5561, 5571, 5558, 5550, 5562, 5554, 5555, 5556,
      5553, 5550, 5549, 5554, 5546, 5544, 5542, 5552, 5563, 5573, 5566, 5558,
      5563, 5571, 5558, 5559, 5574, 5569, 5560, 5563, 5569, 5555, 5564, 5562,
      5562, 5569, 5574, 5564, 5567, 5568, 5564, 5559, 5558, 5549, 5559, 5567,
      5564, 5554, 5558, 5563, 5563, 5559, 5562, 5563, 5553, 5567, 5560, 5561,
      5571, 5569, 5271, 5271, 5567, 5568, 5563, 5558, 5560, 5558, 5562, 5564,
      5554, 5562, 5557, 5556, 5558, 5565, 5569, 5572, 5561, 5568, 5564, 5564,
      5564, 5569, 5566, 5569, 5568, 5565, 5572, 5586, 5570, 5574, 5574, 5559,
      5557, 5563, 5565, 5562, 5573, 5569, 5569, 5556, 5559, 5553, 5561, 5552,
      5559, 5562, 5555, 5565, 5554, 5550, 5555, 5564, 5563, 5553, 5563, 5574,
      5569, 5566, 5560, 5562, 5558, 5273, 5264, 5554, 5551, 5553, 5564, 5561,
      5546, 5553, 5563, 5560, 5569, 5563, 5554, 5549, 5552, 5559, 5573, 5563,
      5572, 5568, 5556, 5568, 5556, 5556, 5548, 5556, 5563, 5566, 5569, 5571,
      5571, 5566, 5559, 5570, 5564, 5553, 5561, 5563, 5564, 5564, 5566, 5555,
      5554, 5561, 5566, 5566, 5583, 5573, 5571, 5579, 5571, 5571, 5558, 5556,
      5559, 5559, 5558, 5558, 5556, 5561, 5559, 5561, 5282, 5263, 5563, 5564,
      5557, 5549, 5551, 5564, 5566, 5556, 5555, 5561, 5559, 5566, 5558, 5563,
      5550, 5551, 5553, 5546, 5554, 5559, 5564, 5565, 5566, 5562, 5566, 5568,
      5563, 5557, 5566, 5563, 5553, 5559, 5559, 5568, 5575, 5568, 5556, 5563,
      5548, 5551, 5563, 5556, 5550, 5564, 5576, 5579, 5576, 5560, 5551, 5557,
      5558, 5569, 5558, 5564, 5566, 5566, 5566, 5567, 5554, 5558, 5569, 5263,
      5263, 5552, 5578, 5584, 5569, 5544, 5551, 5575, 5569, 5573, 5573, 5564,
      5563, 5562, 5564, 5562, 5557, 5552, 5557, 5566, 5559, 5546, 5556, 5563,
      5568, 5568, 5561, 5567, 5581, 5561, 5563, 5556, 5544, 5558, 5553, 5551,
      5558, 5571, 5564, 5572, 5569, 5561, 5568, 5555, 5543, 5553, 5562, 5557,
      5571, 5563, 5548, 5563, 5558, 5551, 5571, 5565, 5559, 5564, 5563, 5557,
      5562, 5561, 5269, 5266, 5569, 5576, 5567, 5559, 5554, 5553, 5554, 5554,
      5558, 5566, 5581, 5574, 5566, 5570, 5567, 5558, 5562, 5561, 5550, 5562,
      5566, 5564, 5565, 5562, 5563, 5571, 5558, 5556, 5560, 5567, 5563, 5566,
      5564, 5565, 5559, 5554, 5561, 5573, 5571, 5560, 5565, 5560, 5566, 5573,
      5569, 5568, 5563, 5556, 5563, 5568, 5570, 5563, 5554, 5561, 5558, 5559,
      5554, 5566, 5564, 5564, 5568, 5273, 5275, 5560, 5570, 5571, 5558, 5561,
      5564, 5561, 5559, 5563, 5568, 5554, 5554, 5545, 5564, 5566, 5571, 5563,
      5556, 5562, 5559, 5557, 5559, 5556, 5558, 5559, 5564, 5562, 5559, 5551,
      5546, 5563, 5553, 5559, 5556, 5560, 5556, 5559, 5551, 5548, 5566, 5564,
      5563, 5556, 5557, 5566, 5570, 5555, 5558, 5561, 5567, 5561, 5566, 5563,
      5564, 5564, 5558, 5566, 5560, 5564, 5564, 5560, 5270, 5271, 5565, 5554,
      5572, 5558, 5553, 5558, 5552, 5553, 5560, 5561, 5547, 5556, 5561, 5559,
      5563, 5561, 5555, 5559, 5564, 5556, 5563, 5571, 5549, 5553, 5551, 5554,
      5558, 5576, 5573, 5568, 5564, 5558, 5560, 5578, 5576, 5568, 5555, 5555,
      5564, 5576, 5563, 5565, 5556, 5561, 5563, 5559, 5551, 5568, 5568, 5569,
      5570, 5564, 5564, 5569, 5552, 5554, 5563, 5557, 5549, 5561, 5574, 5273,
      5262, 5561, 5548, 5553, 5564, 5563, 5572, 5561, 5558, 5554, 5561, 5576,
      5574, 5568, 5565, 5553, 5551, 5559, 5569, 5558, 5564, 5564, 5557, 5559,
      5546, 5555, 5564, 5559, 5561, 5563, 5569, 5571, 5563, 5556, 5548, 5541,
      5551, 5559, 5553, 5543, 5546, 5563, 5563, 5553, 5568, 5559, 5551, 5555,
      5556, 5554, 5562, 5563, 5546, 5551, 5564, 5565, 5568, 5563, 5564, 5568,
      5571, 5568, 5270, 5284, 5568, 5553};

   cout << countPeaks( arr ) << '\n';
}
Last edited on
Topic archived. No new replies allowed.