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.
Approach: The problem can be solved using Greedy technique. The idea is to find the largest and the smallest element present in the array and calculate the maximum sum of the frequency of three consecutive numbers by iterating all the numbers over the range of array elements. Finally, print the maximum sum obtained. Follow the steps below to solve the problem:
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void max_freq( int arr[], int N)
{
int Max = *max_element(arr, arr + N);
int Min = *min_element(arr, arr + N);
int freq[Max - Min + 1] = { 0 };
for ( int i = 0; i < N; i++) {
freq[arr[i] - Min]++;
}
int maxSum = 0;
for ( int i = 0;
i < (Max - Min - 1); i++) {
int val = freq[i] + freq[i + 1] + freq[i + 2];
maxSum = max(maxSum, val);
}
cout << maxSum << "\n" ;
}
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{
static void max_freq( int arr[], int N)
{
Arrays.sort(arr);
int Max = arr[N - 1 ];
int Min = arr[ 0 ];
int freq[] = new int [Max - Min + 1 ];
for ( int i = 0 ; i < N; i++)
{
freq[arr[i] - Min]++;
}
int maxSum = 0 ;
for ( int i = 0 ; i < (Max - Min - 1 ); i++)
{
int val = freq[i] + freq[i + 1 ] +
freq[i + 2 ];
maxSum = Math.max(maxSum, val);
}
System.out.println(maxSum);
}
public static void main (String[] args)
{
int arr[] = { 3 , 1 , 4 , 1 , 5 , 9 , 2 };
int N = arr.length;
max_freq(arr, N);
}
}
|
Python3
def max_freq(arr, N):
Max = max (arr)
Min = min (arr)
freq = [ 0 ] * ( Max - Min + 1 )
for i in range (N):
freq[arr[i] - Min ] + = 1
maxSum = 0
for i in range ( Max - Min - 1 ):
val = freq[i] + freq[i + 1 ] + freq[i + 2 ]
maxSum = max (maxSum, val)
print (maxSum)
if __name__ = = "__main__" :
arr = [ 3 , 1 , 4 , 1 , 5 , 9 , 2 ]
N = len (arr)
max_freq(arr, N)
|
C#
using System;
class GFG{
static void max_freq( int [] arr, int N)
{
Array.Sort(arr);
int Max = arr[N - 1];
int Min = arr[0];
int [] freq = new int [Max - Min + 1];
for ( int i = 0; i < N; i++)
{
freq[arr[i] - Min]++;
}
int maxSum = 0;
for ( int i = 0; i < (Max - Min - 1); i++)
{
int val = freq[i] + freq[i + 1] +
freq[i + 2];
maxSum = Math.Max(maxSum, val);
}
Console.WriteLine(maxSum);
}
public static void Main()
{
int [] arr = { 3, 1, 4, 1, 5, 9, 2 };
int N = arr.Length;
max_freq(arr, N);
}
}
|
Javascript
<script>
function max_freq(arr, N)
{
arr.sort();
let Max = arr[N - 1];
let Min = arr[0];
let freq = [];
for (let i = 0; i < Max - Min + 1 ; i++)
{
freq[i] = 0;
}
for (let i = 0; i < N; i++)
{
freq[arr[i] - Min]++;
}
let maxSum = 0;
for (let i = 0; i < (Max - Min - 1); i++)
{
let val = freq[i] + freq[i + 1] +
freq[i + 2];
maxSum = Math.max(maxSum, val);
}
document.write(maxSum);
}
let arr = [ 3, 1, 4, 1, 5, 9, 2 ];
let N = arr.length;
max_freq(arr, N);
</script>
|
Time Complexity: O(N + |Max – Min|), where Max, Min denotes the largest and the smallest array elements respectively
Auxiliary Space: O(|Max – Min|)