Skip to content
Related Articles

Related Articles

Minimum value to be added to maximize peak count

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Aug, 2022
View Discussion
Improve Article
Save Article

Given an array A[] of size N. The task is to find the minimum value that should be added to the array elements to maximize the number of peaks in the array:

Note: An element is called a peak when it has a higher value than both of its neighbours. 

Examples:

Input: N = 6, A = {3, 1, 4, 5, 5, 2}
Output: 3
Explanation:  Modified A = {3, 1, 6, 5, 6, 2},  
So additional value added  = 2(6 – 4) + 1(6 – 1) = 3

Input: N = 7, A = {5, 6, 3, 7, 8, 9, 3}
Output: 3

 

Approach:

If the length of the array(N) is odd:

  • The first and last elements can’t be chosen as they have no previous and next elements. In this case index 1 can be chosen then 3, 5, and so on. In this way, maximum good elements can be made in the minimum operation.
  • To minimize the addition, the optimal way is to make the peaks have value just greater than the max of its neighbours. 

If the length of the array(N) is even:

  • There are multiple configurations in which peaks can be maximized. If configurations are observed, it can be see that there are two adjacent elements in every configuration which can’t be made good. And if observed closely, it can be seen that on the left of those two elements, only even indices are chosen, and on the right, only odd indices are chosen. 
  • Store the prefix and suffix sum for even indexes and odd indexes. Then array can be iterated again, and the values to be added to make peaks can be calculated using the pre-calculated prefix and suffix sum.

Follow the steps mentioned below to implement the idea:

  • Check if the array length is even or odd.
  • If the array has an odd length there is only one way to maximize the peaks.
  • Otherwise, do the following:
    • Create prefix and suffix sum array.
    • The prefix array will store the sum of differences to make the odd indexed elements as peaks.
    • The suffix array will store the sum of differences to make the even indexed elements as peaks.
    • Iterate the array and find the minimum value as mentioned above.
  • Return the minimum value as the required answer.

Below is the implementation of the above approach:

C++




// C++ code to implement above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum additional value
int good_elements(int arr[], int n)
{
    int ans = INT_MAX;
    if (n % 2 == 1) {
        ans = 0;
 
        // If the length is odd
        for (int i = 1; i < n; i += 2) {
            int temp
                = max(arr[i + 1], arr[i - 1]) - arr[i] + 1;
            ans = ans + max(0, temp);
        }
    }
    else {
 
        // If the length is even
        int pref[n + 1] = { 0 };
        int suff[n + 1] = { 0 };
 
        for (int i = 1; i + 1 < n; i += 2) {
 
            // Calculating the prefix sum excluding first
            // and last element
            int temp
                = max(0, max(arr[i + 1], arr[i - 1])
                             - arr[i] + 1);
            pref[i + 1] = pref[i - 1] + temp;
        }
 
        for (int i = n - 2; i - 1 > 0; i -= 2) {
 
            // Calculating the suffix sum excluding first
            // and last element
            int temp
                = max(0, max(arr[i + 1], arr[i - 1])
                             - arr[i] + 1);
            suff[i - 1] = suff[i + 1] + temp;
        }
 
        // Calculating ans for every configuration and
        // finding the minimum from those
        for (int i = 0; i < n; i += 2) {
            ans = min(ans, pref[i] + suff[i + 1]);
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 1, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    int ans = good_elements(arr, N);
    cout << ans;
    return 0;
}


Java




// Java code to implement above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the minimum additional value
    static int good_elements(int[] arr, int n)
    {
        int ans = Integer.MAX_VALUE;
        if (n % 2 == 1) {
            ans = 0;
 
            // If the length is odd
            for (int i = 1; i < n; i += 2) {
                int temp = Math.max(arr[i + 1], arr[i - 1])
                           - arr[i] + 1;
                ans = ans + Math.max(0, temp);
            }
        }
        else {
 
            // If the length is even
            int pref[] = new int[n + 1];
            int suff[] = new int[n + 1];
            Arrays.fill(pref, 0);
            Arrays.fill(suff, 0);
            for (int i = 1; i + 1 < n; i += 2) {
 
                // Calculating the prefix sum excluding
                // first and last element
                int temp = Math.max(
                    0, Math.max(arr[i + 1], arr[i - 1])
                           - arr[i] + 1);
                pref[i + 1] = pref[i - 1] + temp;
            }
            for (int i = n - 2; i - 1 > 0; i -= 2) {
 
                // Calculating the suffix sum excluding
                // first and last element
                int temp = Math.max(
                    0, Math.max(arr[i + 1], arr[i - 1])
                           - arr[i] + 1);
                suff[i - 1] = suff[i + 1] + temp;
            }
 
            // Calculating ans for every configuration and
            // finding the minimum from those
            for (int i = 0; i < n; i += 2) {
                ans = Math.min(ans, pref[i] + suff[i + 1]);
            }
        }
        return ans;
    }
 
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 1, 1 };
        int N = arr.length;
 
        // Function call
        int ans = good_elements(arr, N);
        System.out.print(ans);
    }
}
 
// This code is contributed by lokeshmvs21.


Python3




# Python3 code to implement above approach
import sys
 
# Function to find the minimum additional value
def good_elements(arr, n) :
 
    ans = sys.maxsize;
     
    if (n % 2 == 1) :
        ans = 0;
 
        # If the length is odd
        for i in range(1, n, 2) :
            temp = max(arr[i + 1], arr[i - 1]) - arr[i] + 1;
            ans = ans + max(0, temp);
             
    else :
 
        # If the length is even
        pref = [0]*(n + 1) ;
        suff = [0]*(n + 1) ;
 
        for i in range(1, n-1, 2) :
 
            # Calculating the prefix sum excluding first
            # and last element
            temp = max(0, max(arr[i + 1], arr[i - 1]) - arr[i] + 1);
            pref[i + 1] = pref[i - 1] + temp;
 
        for i in range(n - 2, 1, -1) :
 
            # Calculating the suffix sum excluding first
            # and last element
            temp = max(0, max(arr[i + 1], arr[i - 1]) - arr[i] + 1);
            suff[i - 1] = suff[i + 1] + temp;
 
        # Calculating ans for every configuration and
        # finding the minimum from those
        for i in range(0, n , 2) :
            ans = min(ans, pref[i] + suff[i + 1]);
 
    return ans;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [1, 2, 3, 1, 1 ];
    N = len(arr);
 
    # Function call
    ans = good_elements(arr, N);
    print(ans);
    
   # This code is contributed by AnkThon


C#




// C# code to implement the approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  // Function to find the minimum additional value
  static int good_elements(int[] arr, int n)
  {
    int ans = Int32.MaxValue;
    if (n % 2 == 1) {
      ans = 0;
 
      // If the length is odd
      for (int i = 1; i < n; i += 2) {
        int temp = Math.Max(arr[i + 1], arr[i - 1])
          - arr[i] + 1;
        ans = ans + Math.Max(0, temp);
      }
    }
    else {
 
      // If the length is even
      int[] pref = new int[n + 1];
      int[] suff = new int[n + 1];
      for (int i = 1; i < n+1; i += 1) {
        pref[i] = 0;
      }
 
      for (int i = 1; i < n+1; i += 1) {
        suff[i] = 0;
      }
 
      for (int i = 1; i + 1 < n; i += 2) {
 
        // Calculating the prefix sum excluding
        // first and last element
        int temp = Math.Max(
          0, Math.Max(arr[i + 1], arr[i - 1])
          - arr[i] + 1);
        pref[i + 1] = pref[i - 1] + temp;
      }
      for (int i = n - 2; i - 1 > 0; i -= 2) {
 
        // Calculating the suffix sum excluding
        // first and last element
        int temp = Math.Max(
          0, Math.Max(arr[i + 1], arr[i - 1])
          - arr[i] + 1);
        suff[i - 1] = suff[i + 1] + temp;
      }
 
      // Calculating ans for every configuration and
      // finding the minimum from those
      for (int i = 0; i < n; i += 2) {
        ans = Math.Min(ans, pref[i] + suff[i + 1]);
      }
    }
    return ans;
  }
 
  // Driver Code
  static public void Main (){
 
    int[] arr = { 1, 2, 3, 1, 1 };
    int N = arr.Length;
 
    // Function call
    int ans = good_elements(arr, N);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by sanjoy_62.


Javascript




// Javascript code to implement above approach
 
<script>
 
// Function to find the minimum additional value
function good_elements(arr,n)
{
    let ans =Number.MAX_VALUE;
    if (n % 2 == 1) {
        ans = 0;
 
        // If the length is odd
        for (let i = 1; i < n; i += 2) {
            let temp
                = Math.max(arr[i + 1], arr[i - 1]) - arr[i] + 1;
            ans = ans + Math.max(0, temp);
        }
    }
    else {
 
        // If the length is even
        let pref = new Array(n+1);
        for (let i=0; i<n+1; ++i)
        pref[i] = 0;
        let suff = new Array(n+1);
        for (let i=0; i<n+1; ++i)
        suff[i] = 0;
 
        for (let i = 1; i + 1 < n; i += 2) {
 
            // Calculating the prefix sum excluding first
            // and last element
            let temp
                = Math.max(0, Math.max(arr[i + 1], arr[i - 1])
                             - arr[i] + 1);
            pref[i + 1] = pref[i - 1] + temp;
        }
 
        for (let i = n - 2; i - 1 > 0; i -= 2) {
 
            // Calculating the suffix sum excluding first
            // and last element
            let temp
                = Math.max(0, Math.max(arr[i + 1], arr[i - 1])
                             - arr[i] + 1);
            suff[i - 1] = suff[i + 1] + temp;
        }
 
        // Calculating ans for every configuration and
        // finding the minimum from those
        for (let i = 0; i < n; i += 2) {
            ans = Math.min(ans, pref[i] + suff[i + 1]);
        }
    }
    return ans;
}
 
// Driver code
 
    let arr = [ 1, 2, 3, 1, 1 ];
    let N = arr.length;
 
    // Function call
    let ans = good_elements(arr, N);
    document.write(ans);
     
    // This code is contributed by satwik4409.
    </script>


Output

5

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!