Maximize frequency of an element by at most one increment or decrement of all array elements | Set 2
Last Updated :
16 Nov, 2021
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: 4
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: 3
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++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
void max_freq( int arr[], int N)
{
int freq[MAXN];
memset (freq, 0, sizeof (freq));
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
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;
}
int main()
{
int arr[] = { 3, 1, 4, 1, 5, 9, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
max_freq(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
public static int MAXN = 100005 ;
public static void max_freq( int arr[], int N)
{
int [] freq = new int [MAXN];
Arrays.fill(freq, 0 );
for ( int i = 0 ; i < N; i++) {
freq[arr[i]]++;
}
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);
}
public static void main(String args[]) {
int arr[] = { 3 , 1 , 4 , 1 , 5 , 9 , 2 };
int N = arr.length;
max_freq(arr, N);
}
}
|
Python3
MAXN = 100005 ;
def max_freq(arr, N) :
freq = [ 0 ] * MAXN;
for i in range (N) :
freq[arr[i]] + = 1 ;
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);
if __name__ = = "__main__" :
arr = [ 3 , 1 , 4 , 1 , 5 , 9 , 2 ];
N = len (arr);
max_freq(arr, N);
|
C#
using System;
class GFG
{
static int MAXN = 100005;
static void max_freq( int []arr, int N)
{
int []freq = new int [MAXN];
Array.Clear(freq, 0, freq.Length);
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
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);
}
public static void Main() {
int []arr = { 3, 1, 4, 1, 5, 9, 2 };
int N = arr.Length;
max_freq(arr, N);
}
}
|
Javascript
<script>
var MAXN = 100005
function max_freq(arr, N) {
let freq = new Array(MAXN).fill(0);
for (let i = 0; i < N; i++) {
freq[arr[i]]++;
}
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);
}
let arr = [3, 1, 4, 1, 5, 9, 2];
let N = arr.length;
max_freq(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(|Max|), where Max is the maximum element in the array
Share your thoughts in the comments
Please Login to comment...