Maximize minimum distance between repetitions from any permutation of the given Array
Last Updated :
26 Apr, 2021
Given an array arr[], consisting of N positive integers in the range [1, N], the task is to find the largest minimum distance between any consecutive repetition of an element from any permutation of the given array.
Examples:
Input: arr[] = {1, 2, 1, 3}
Output: 3
Explanation: The maximum possible distance between the repetition is 3, from the permutation {1, 2, 3, 1} or {1, 3, 2, 1}.
Input: arr[] = {1, 2, 3, 4}
Output: 0
Approach: Follow the steps below to solve the problem:
- Store the frequency of each array element.
- Find the element which contains the maximum frequency, say maxFreqElement.
- Count the number of occurrences of elements having a maximum frequency, say maxFreqCount.
- Calculate the required distance by the equation (N- maxFreqCount)/( maxFreqElement- 1))
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxLen(vector< int >& a)
{
int n = a.size();
int freq[n + 1];
memset (freq, 0, sizeof freq);
for ( int i = 0; i < n; ++i) {
freq[a[i]]++;
}
int maxFreqElement = INT_MIN;
int maxFreqCount = 1;
for ( int i = 1; i <= n; ++i) {
if (freq[i] > maxFreqElement) {
maxFreqElement = freq[i];
maxFreqCount = 1;
}
else if (freq[i] == maxFreqElement)
maxFreqCount++;
}
int ans;
if (maxFreqElement == 1)
ans = 0;
else {
ans = ((n - maxFreqCount)
/ (maxFreqElement - 1));
}
return ans;
}
int main()
{
vector< int > a = { 1, 2, 1, 2 };
cout << findMaxLen(a) << endl;
}
|
Java
class GFG{
static int findMaxLen( int a[], int n)
{
int freq[] = new int [n + 1 ];
for ( int i = 0 ; i < n; ++i)
{
freq[a[i]]++;
}
int maxFreqElement = Integer.MIN_VALUE;
int maxFreqCount = 1 ;
for ( int i = 1 ; i <= n; ++i)
{
if (freq[i] > maxFreqElement)
{
maxFreqElement = freq[i];
maxFreqCount = 1 ;
}
else if (freq[i] == maxFreqElement)
maxFreqCount++;
}
int ans;
if (maxFreqElement == 1 )
ans = 0 ;
else
{
ans = ((n - maxFreqCount) /
(maxFreqElement - 1 ));
}
return ans;
}
public static void main(String [] args)
{
int a[] = { 1 , 2 , 1 , 2 };
int n = a.length;
System.out.print(findMaxLen(a, n));
}
}
|
Python3
import sys
def findMaxLen(a):
n = len (a)
freq = [ 0 ] * (n + 1 )
for i in range (n):
freq[a[i]] + = 1
maxFreqElement = - sys.maxsize - 1
maxFreqCount = 1
for i in range ( 1 , n + 1 ):
if (freq[i] > maxFreqElement):
maxFreqElement = freq[i]
maxFreqCount = 1
elif (freq[i] = = maxFreqElement):
maxFreqCount + = 1
if (maxFreqElement = = 1 ):
ans = 0
else :
ans = ((n - maxFreqCount) / /
(maxFreqElement - 1 ))
return ans
a = [ 1 , 2 , 1 , 2 ]
print (findMaxLen(a))
|
C#
using System;
class GFG{
static int findMaxLen( int [] a, int n)
{
int [] freq = new int [n + 1];
for ( int i = 0; i < n; ++i)
{
freq[a[i]]++;
}
int maxFreqElement = int .MinValue;
int maxFreqCount = 1;
for ( int i = 1; i <= n; ++i)
{
if (freq[i] > maxFreqElement)
{
maxFreqElement = freq[i];
maxFreqCount = 1;
}
else if (freq[i] == maxFreqElement)
maxFreqCount++;
}
int ans;
if (maxFreqElement == 1)
ans = 0;
else
{
ans = ((n - maxFreqCount) /
(maxFreqElement - 1));
}
return ans;
}
public static void Main(String[] args)
{
int [] a = {1, 2, 1, 2};
int n = a.Length;
Console.Write(findMaxLen(a, n));
}
}
|
Javascript
<script>
function findMaxLen(a)
{
var n = a.length;
var freq = Array(n+1).fill(0);
var i;
for (i = 0; i < n; ++i) {
freq[a[i]]++;
}
var maxFreqElement = -2147483648;
var maxFreqCount = 1;
for (i = 1; i <= n; ++i) {
if (freq[i] > maxFreqElement) {
maxFreqElement = freq[i];
maxFreqCount = 1;
}
else if (freq[i] == maxFreqElement)
maxFreqCount++;
}
var ans;
if (maxFreqElement == 1)
ans = 0;
else {
ans = ((n - maxFreqCount)
/ (maxFreqElement - 1));
}
return ans;
}
var a = [1, 2, 1, 2];
document.write(findMaxLen(a));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...