Minimize the count of peaks and troughs in the given Array after at most one replacement


Peaks are defined as the point or index in the array where are both left and right sides values are smaller than the value of that index. And Troughs are defined as the point or index of the array where are both left and right sides values are greater than the value of that index. In this problem, we have given an array 'array' of size n of integers. Our task is to minimize or reduce the count of peaks and troughs of the given array by performing an operation. Operation is that we can replace at most one value of the given array with any value.

NOTE: Here in the above explanation of the problem we have mentioned peaks and troughs so be clear that the 0th index and last index of the given array are not considered in either peaks or troughs as they don't have both neighbors.

Sample Examples

Input

n: 5
array: [2, 1, 3, 2, 6]

Output

0

Explanation: In the above-given array of size 5 the count of peaks and troughs become 0 by replacing the 2nd index value 3 with 1. So the new array becomes [2, 1, 1, 2, 6].

Input

n: 6
array: [2, 1, 3, 4, 3, 4]

Output

1

Greedy Approach 

The idea of this approach is simple here we think greedily like as we know that any index ith can affect either i+1th or i-1th index so we try to change the value of the index ith which is affected by a maximum number of indexes.

Let's discuss this approach step by step below-

  • Here we have created three functions 'minCountOfPeaksTrougs', 'next', and 'previous'.

  • In the 'minCountOfPeaksTrougs' function

Create a boolean array to mark the peaks and troughs array by traversing the array and also counting it

Store the counts of peaks and troughs in the variable result

Again, Traverse the array in which we check for current index i for its next and previous index by assigning the current array value as the next array value and current array value as the previous array value respectively and update it using next and previous functions.

In the end update the result and return it.

  • In the Next and previous functions

Both functions take parameters such as current index, array, and length of the array and that verifies whether a current index has peaks and troughs, respectively.

Let's see the code below for a better understanding of the above approach.

Example

#include <bits/stdc++.h>
using namespace std;
 
// Create a function to check the next element for the current index
bool next(int i, int array[], int n){
   bool ok;
   ok = i > 0 && i < n - 1 && array[i] < array[i - 1] && array[i] < array[i + 1];
   return ok;
}
// Create a function to check the previous element for the current index
bool previous(int i, int array[], int n){
   bool ok;
   ok = i > 0 && i < n - 1 && array[i] > array[i - 1] && array[i] > array[i + 1];
   return ok;
}
// Create function minCountOfPeaksTrougs to minimize the count of Peaks and Trough  
int minCountOfPeaksTrougs(int array[], int n){
   // Create an array of boolean to store the index of peaks and troughs
   bool check[n] = { 0 };
   int cnt = 0;
   // Traverse given array to check the troughs and peaks
   for (int i = 1; i < n- 1; i++) {
      //If both the neighbors are greater than the current index is peaks
      if (array[i] > array[i - 1] && array[i] > array[i + 1]){
         check[i] = 1;
         cnt++;
      }
      //If both the neighbors are smaller than the current index is trough
      else if (array[i] < array[i - 1] && array[i] < array[i + 1]){
         check[i] = 1;
         cnt++;
      }
   }
   // Create variable result and initialized With the count of peaks and troughs
   int result = cnt;
   // Traverse the array
   for (int i = 1; i < n - 1; i++) {
      int curVal = array[i];
      // If we make the current array value as next array value
      array[i] = array[i + 1];
      int temp = cnt -check[i - 1] -check[i] -check[i + 1] +next(i - 1, array, n) +previous(i - 1, array, n) +next(i, array, n) +previous(i, array, n) +next(i + 1, array, n) +previous(i + 1, array, n);
      result= min( result, temp );
      // If we make current array value as the previous array value
      array[i] = array[i - 1];
      temp = cnt -check[i - 1] -check[i] -check[i + 1] +next(i - 1, array, n) +previous(i - 1, array, n) +next(i, array, n) +previous(i, array, n) +next(i + 1, array, n) +previous(i + 1, array, n);
      
      result= min( result, temp );
      array[i] = curVal;
   }
   return result;
}
int main(){
   // Given Array
   int array[] = { 2, 1, 3, 2, 6 };
   // Getting the size of the given array
   int n = sizeof(array) / sizeof(int);
   // Store minimum count of peaks and troughs by calling the minCountOfPeaksTrougs function
   int res = minCountOfPeaksTrougs(array, n);
   cout << "Minimum Count of Peaks and Troughs are: "<< res;
   return 0;
}

Output

Minimum Count of Peaks and Troughs are: 0

Time and Space Complexity

The time complexity of the above code is O(N), as we traverse the array.

The space complexity of the above code is O(N), as we are storing the count of peaks and troughs.

Where N is the size of the array.

Conclusion 

In this tutorial, we have implemented a C++ program to minimize the count of peaks and troughs in the given Array after at most one replacement. We have implemented a Greedy approach. The time complexity is O(N) and the space complexity of O(N). Where N is the size of the array.

Updated on: 31-Aug-2023

31 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements