Open In App

Maximize frequency of an element by at most one increment or decrement of all array elements | Set 2

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of size N, the task is to find the maximum frequency of any array element by incrementing or decrementing each array element by 1 at most once.

Examples: 

Input: arr[] = { 3, 1, 4, 1, 5, 9, 2 } 
Output:
Explanation: 
Decrementing the value of arr[0] by 1 modifies arr[] to { 2, 1, 4, 1, 5, 9, 2 } 
Incrementing the value of arr[1] by 1 modifies arr[] to { 2, 2, 4, 1, 5, 9, 2 } 
Incrementing the value of arr[3] by 1 modifies arr[] to { 2, 2, 4, 1, 5, 9, 2 } 
Therefore, the frequency of an array element(arr[0]) is 4 which is the maximum possible.

Input: arr[] = { 0, 1, 2, 3, 4, 5, 6 } 
Output:
Explanation: 
Incrementing the value of arr[0] by 1 modifies arr[] to { 1, 1, 2, 3, 4, 5, 6 } 
Decrementing the value of arr[2] by 1 modifies arr[] to { 1, 1, 1, 3, 4, 5, 6 } 
Therefore, the frequency of an array element(arr[0]) is 3 which is the maximum possible.

 

Greedy Approach: The greedy approach to solve this problem has been discussed in Set 1 of this article
 

Frequency Counting Approach: The idea is to create a frequency array and store the frequency of all elements of the array arr[i]. Now for each possible value of element try to merge the left and right values to this point i.e, freq[i] + freq[i-1] + freq[i+1]. Follow the steps below to solve the problem:

 

  • Define a variable MAXN with a value of 1e5.
  • Initialize an array freq[MAXN] with values 0.
  • Iterate over the range [0, N) using the variable i and perform the following tasks:
    • Increase the value of freq[arr[i]] by 1.
  • Initialize the variable max_freq with value -MAXN.
  • Iterate over the range [1, MAXN-1) using the variable i and perform the following tasks:
    • Set the value of max_freq as the maximum of max_freq or freq[i-1] + freq[i] + freq[i+1].
  • After performing the above steps, print the value of max_freq as the answer.

 

Below is the implementation of the above approach.

 

C++




// C++ program to implement the above approach
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
 
// Function to maximize the frequency
// of an array element by incrementing or
// decrementing array elements at most once
void max_freq(int arr[], int N)
{
 
    // Store the frequency of each element
    int freq[MAXN];
    memset(freq, 0, sizeof(freq));
 
    for (int i = 0; i < N; i++) {
        freq[arr[i]]++;
    }
 
    // Iterate through each value
    // try to merge left and
    // right values to it
    int max_freq = -MAXN;
    for (int i = 1; i < MAXN - 1; i++) {
        max_freq = max(max_freq,
                       freq[i] + freq[i - 1]
                           + freq[i + 1]);
    }
 
    cout << max_freq;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 3, 1, 4, 1, 5, 9, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    max_freq(arr, N);
    return 0;
}


Java




// Java program to implement the above approach
import java.util.Arrays;
 
class GFG
{
    public static int MAXN = 100005;
 
    // Function to maximize the frequency
    // of an array element by incrementing or
    // decrementing array elements at most once
    public static void max_freq(int arr[], int N)
    {
 
        // Store the frequency of each element
        int[] freq = new int[MAXN];
        Arrays.fill(freq, 0);
 
        for (int i = 0; i < N; i++) {
            freq[arr[i]]++;
        }
 
        // Iterate through each value
        // try to merge left and
        // right values to it
        int max_freq = -MAXN;
        for (int i = 1; i < MAXN - 1; i++) {
            max_freq = Math.max(max_freq, freq[i] + freq[i - 1] + freq[i + 1]);
        }
 
        System.out.println(max_freq);
    }
 
    // Driver Code
    public static void main(String args[]) {
 
        int arr[] = { 3, 1, 4, 1, 5, 9, 2 };
        int N = arr.length;
 
        // Function call
        max_freq(arr, N);
    }
}
 
// This code is contributed by Saurabh Jaiswal


Python3




# Python3 program to implement the above approach
MAXN = 100005;
 
# Function to maximize the frequency
# of an array element by incrementing or
# decrementing array elements at most once
def max_freq(arr, N) :
 
    # Store the frequency of each element
    freq = [0] * MAXN;
 
    for i in range(N) :
        freq[arr[i]] += 1;
 
    # Iterate through each value
    # try to merge left and
    # right values to it
    max_freq = -MAXN;
     
    for i in range(1, MAXN - 1) :
        max_freq = max(max_freq, freq[i] + freq[i - 1] + freq[i + 1]);
 
    print(max_freq);
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 3, 1, 4, 1, 5, 9, 2 ];
    N = len(arr);
 
    # Function call
    max_freq(arr, N);
 
    # This code is contributed by AnkThon


C#




// C# program to implement the above approach
using System;
class GFG
{
    static int MAXN = 100005;
 
    // Function to maximize the frequency
    // of an array element by incrementing or
    // decrementing array elements at most once
    static void max_freq(int []arr, int N)
    {
        // Store the frequency of each element
        int []freq = new int[MAXN];
          Array.Clear(freq, 0, freq.Length);
 
        for (int i = 0; i < N; i++) {
            freq[arr[i]]++;
        }
 
        // Iterate through each value
        // try to merge left and
        // right values to it
        int max_freq = -MAXN;
        for (int i = 1; i < MAXN - 1; i++) {
            max_freq = Math.Max(max_freq, freq[i] + freq[i - 1] + freq[i + 1]);
        }
 
        Console.Write(max_freq);
    }
 
    // Driver Code
    public static void Main() {
 
        int []arr = { 3, 1, 4, 1, 5, 9, 2 };
        int N = arr.Length;
 
        // Function call
        max_freq(arr, N);
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
        // JavaScript Program to implement
        // the above approach
        var MAXN = 100005
 
        // Function to maximize the frequency
        // of an array element by incrementing or
        // decrementing array elements at most once
        function max_freq(arr, N) {
 
            // Store the frequency of each element
            let freq = new Array(MAXN).fill(0);
 
            for (let i = 0; i < N; i++) {
                freq[arr[i]]++;
            }
 
            // Iterate through each value
            // try to merge left and
            // right values to it
            let max_freq = -MAXN;
            for (let i = 1; i < MAXN - 1; i++) {
                max_freq = Math.max(max_freq,
                    freq[i] + freq[i - 1]
                    + freq[i + 1]);
            }
 
            document.write(max_freq);
        }
 
        // Driver Code
        let arr = [3, 1, 4, 1, 5, 9, 2];
        let N = arr.length;
 
        // Function call
        max_freq(arr, N);
 
    // This code is contributed by Potta Lokesh
    </script>


 
 

Output

4

 

Time Complexity: O(N)
Auxiliary Space: O(|Max|), where Max is the maximum element in the array

 



Last Updated : 16 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads