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++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; int findMaxLen(vector<int>& a) { // Size of the array int n = a.size(); // Stores the frequency of // array elements 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) { // Find the highest frequency // in the array if (freq[i] > maxFreqElement) { maxFreqElement = freq[i]; maxFreqCount = 1; } // Increase count of max frequent element else if (freq[i] == maxFreqElement) maxFreqCount++; } int ans; // If no repetition is present if (maxFreqElement == 1) ans = 0; else { // Find the maximum distance ans = ((n - maxFreqCount) / (maxFreqElement - 1)); } // Return the max distance return ans; } // Driver Code int main() { vector<int> a = { 1, 2, 1, 2 }; cout << findMaxLen(a) << endl; } |
Java
// Java program to implement // the above approach class GFG{ static int findMaxLen(int a[], int n) { // Stores the frequency of // array elements 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) { // Find the highest frequency // in the array if (freq[i] > maxFreqElement) { maxFreqElement = freq[i]; maxFreqCount = 1; } // Increase count of max frequent element else if (freq[i] == maxFreqElement) maxFreqCount++; } int ans; // If no repetition is present if (maxFreqElement == 1) ans = 0; else { // Find the maximum distance ans = ((n - maxFreqCount) / (maxFreqElement - 1)); } // Return the max distance return ans; } // Driver Code public static void main(String [] args) { int a[] = { 1, 2, 1, 2 }; int n = a.length; System.out.print(findMaxLen(a, n)); } } // This code is contributed by chitranayal |
Python3
# Python3 program to implement # the above approach import sys def findMaxLen(a): # Size of the array n = len(a) # Stores the frequency of # array elements 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): # Find the highest frequency # in the array if(freq[i] > maxFreqElement): maxFreqElement = freq[i] maxFreqCount = 1 # Increase count of max frequent element elif(freq[i] == maxFreqElement): maxFreqCount += 1 # If no repetition is present if(maxFreqElement == 1): ans = 0 else: # Find the maximum distance ans = ((n - maxFreqCount) // (maxFreqElement - 1)) # Return the max distance return ans # Driver Code a = [ 1, 2, 1, 2 ] # Function call print(findMaxLen(a)) # This code is contributed by Shivam Singh |
C#
// C# program to implement // the above approach using System; class GFG{ static int findMaxLen(int[] a, int n) { // Stores the frequency of // array elements 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) { // Find the highest frequency // in the array if (freq[i] > maxFreqElement) { maxFreqElement = freq[i]; maxFreqCount = 1; } // Increase count of max // frequent element else if (freq[i] == maxFreqElement) maxFreqCount++; } int ans; // If no repetition is present if (maxFreqElement == 1) ans = 0; else { // Find the maximum distance ans = ((n - maxFreqCount) / (maxFreqElement - 1)); } // Return the max distance return ans; } // Driver Code public static void Main(String[] args) { int[] a = {1, 2, 1, 2}; int n = a.Length; Console.Write(findMaxLen(a, n)); } } // This code is contributed by Amit Katiyar |
2
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Smallest character in a string having minimum sum of distances between consecutive repetitions
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
- Maximize distance between any two consecutive 1's after flipping M 0's
- Minimum distance between any special pair in the given array
- Check if an Array is made up of Subarrays of continuous repetitions of every distinct element
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum distance between any two equal elements in an Array
- Minimum distance between any most frequent and least frequent element of an array
- Check if any permutation of N equals any power of K
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Represent N as sum of K odd numbers with repetitions allowed
- Represent N as sum of K even or K odd numbers with repetitions allowed
- Check if concatenation of any permutation of given list of arrays generates the given array
- Distance of chord from center when distance between center and another equal length chord is given
- Minimum distance between the maximum and minimum element of a given Array
- Maximize distance between two elements of Array by at most X swaps
- Maximize distance between smallest and largest Array elements by a single swap
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

