Count Hills and Valleys in an Array LeetCode solution in C++
1class Solution {2public:3int countHillValley(vector<int> &nums) {4// taking a new vector5vector<int> v;6v.push_back(nums[0]);7// pushing unique elements into new vector8for (int i = 1; i < nums.size(); i++) {9if (nums[i] != nums[i - 1]) {10v.push_back(nums[i]);11}12}13int c = 0;14// checking for valley or hill15for (int i = 1; i < v.size() - 1; i++) {16if (v[i] > v[i - 1] and v[i] > v[i + 1] or v[i] < v[i - 1] and v[i] < v[i + 1]) {17c++;18}19}20return c;21}22};