Open In App

Minimum replacements in given Array to remove all strictly peak elements

Last Updated : 30 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of length N, the task is to find the minimum number of replacements required to remove all peak elements of the array.

Note: An element is said to be a peak element if it is strictly greater than both of its neighbours. Corner elements cannot be peak elements, because they have only one neighbour.

Examples: 

Input: arr = { 3, 2, 3}
Output: operations = 0
arr = { 3, 2, 3}
Explanation : There is no peak element in array

Input: arr = { 2, 2, 3, 1, 3, 1 3, 1, 3}
Output: operations = 2
arr = {2, 2, 3, 3, 3, 1, 3, 3, 3 }
Explanation: There are three peak elements at arr[2], arr[4] and arr[6].
Replace arr[3] with arr[4] and arr[7] with arr[8]

 

Approach: The idea to solve this problem is to convert the valley elements to adjacent strictly peak elements. This can be done as shown below: 

If an element arr[i] is a peak element, replace arr[i +1] with max of arr[i+2] and arr[ i ]. This will eliminate the chances of arr[ i ] and arr[i+2] of becoming peak elements at the same time. 

If i+2 exceeds the array bound then replace arr[i] with max of arr[i-1] and arr[i+1] as this will eliminate chance of arr[i] being a peak.

Follow the steps mentioned below to solve the problem:

  • Iterate from the starting of the array.
  • Check if the ith element is peak or not:
    • If it is a peak increase the replacement count by 1 and change the as per the conditions mentioned above.
    • Otherwise, skip this element and continue iteration.
  • Return the final count and the final array.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the count
// of replacements and the final array
void solution(int* arr, int n)
{
 
  // Count of operations
  int cnt = 0;
  for (int i = 1; i < n - 1; i++) {
 
    // Check if it is peak or not
    if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
      cnt++;
      if (i < n - 2)
        arr[i + 1] = max(arr[i], arr[i + 2]);
 
      else
        arr[i] = max(arr[i + 1], arr[i - 1]);
    }
  }
  cout << (cnt) << "\n";
 
  for (int i = 0; i < n; ++i)
    cout << arr[i] << " ";
}
 
// Driver Code
int main()
{
  int N = 9;
  int arr[] = { 2, 2, 3, 1, 3, 1, 3, 1, 3 };
  solution(arr, N);
 
  return 0;
}
 
// This code is contributed by rakeshsahni


Java




// Java code to implement the approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the count
    // of replacements and the final array
    public static void solution(int[] arr,
                                int n)
    {
        // Count of operations
        int cnt = 0;
        for (int i = 1; i < n - 1; i++) {
 
            // Check if it is peak or not
            if (arr[i] > arr[i - 1]
                && arr[i] > arr[i + 1]) {
                cnt++;
                if (i < n - 2)
                    arr[i + 1]
                        = Math.max(arr[i],
                                   arr[i + 2]);
 
                else
                    arr[i]
                        = Math.max(arr[i + 1],
                                   arr[i - 1]);
            }
        }
        System.out.println(cnt);
 
        for (int i : arr)
            System.out.print(i + " ");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 9;
        int arr[] = { 2, 2, 3, 1, 3, 1, 3, 1, 3 };
        solution(arr, N);
    }
}


Python




# Python code to implement the approach
 
# Function to find the count
# of replacements and the final array
def solution(arr, n):
 
  # Count of operations
  cnt = 0
  i = 1
  for i in range(1, n - 1):
 
    # Check if it is peak or not
    if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]):
      cnt += 1
      if (i < n - 2):
        arr[i + 1] = max(arr[i], arr[i + 2])
 
      else:
        arr[i] = max(arr[i + 1], arr[i - 1])
         
  print(cnt)
 
  print(arr)
 
# Driver Code
 
N = 9
arr = [ 2, 2, 3, 1, 3, 1, 3, 1, 3 ]
solution(arr, N)
 
# This code is contributed by Samim Hossain Mondal.


C#




// C# code to implement the approach
using System;
 
class GFG {
 
  // Function to find the count
  // of replacements and the final array
  public static void solution(int[] arr,
                              int n)
  {
 
    // Count of operations
    int cnt = 0;
    for (int i = 1; i < n - 1; i++) {
 
      // Check if it is peak or not
      if (arr[i] > arr[i - 1]
          && arr[i] > arr[i + 1]) {
        cnt++;
        if (i < n - 2)
          arr[i + 1]
          = Math.Max(arr[i],
                     arr[i + 2]);
 
        else
          arr[i]
          = Math.Max(arr[i + 1],
                     arr[i - 1]);
      }
    }
    Console.WriteLine(cnt);
 
    foreach (int i in arr)
      Console.Write(i + " ");
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 9;
    int []arr = { 2, 2, 3, 1, 3, 1, 3, 1, 3 };
    solution(arr, N);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
// Javascript code to implement the approach
 
// Function to find the count
// of replacements and the final array
function solution(arr, n)
{
 
  // Count of operations
  let cnt = 0;
  for (let i = 1; i < n - 1; i++) {
 
    // Check if it is peak or not
    if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
      cnt++;
      if (i < n - 2)
        arr[i + 1] = Math.max(arr[i], arr[i + 2]);
 
      else
        arr[i] = Math.max(arr[i + 1], arr[i - 1]);
    }
  }
    document.write(cnt + "\n");
 
  for (let i = 0; i < n; ++i)
    document.write(arr[i] + " ");
}
 
// Driver Code
 
let N = 9;
let arr = [ 2, 2, 3, 1, 3, 1, 3, 1, 3 ];
solution(arr, N);
 
// This code is contributed by Samim Hossain Mondal.
</script>


 
 

Output

2
2 2 3 3 3 1 3 3 3 

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads