The Wayback Machine - https://web.archive.org/web/20220216181644/https://www.geeksforgeeks.org/maximise-distance-by-rearranging-all-duplicates-at-same-distance-in-given-array/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximise distance by rearranging all duplicates at same distance in given Array

  • Last Updated : 16 Feb, 2022

Given an array arr[] of N integers. Arrange the array in a way such that the minimum distance among all pairs of same elements is maximum among all possible arrangements. The task is to find this maximum value.

Examples:

Input: arr[] = {1, 1, 2, 3}
Output: 3
Explanation: All possible arrangements are: 
{1, 1, 2, 3}, {1, 1, 3, 2}, {1, 2, 1, 3}, {1, 2, 3, 1}, {1, 3, 1, 2}, {1, 3, 2, 1},  
{2, 1, 1, 3}, {2, 1, 3, 1}, {2, 3, 1, 1}, {3, 1, 1, 2}, {3, 1, 2, 1}, {3, 2, 1, 1}.
Here the arrangements {1, 2, 3, 1} and {1, 3, 2, 1} gives the answer which is 3.
As distance = |(index of first 1) – (index of second 1)|

Input: arr[] = {1, 2, 1, 2, 9, 10, 9}
Output: 4
Explanation: One such arrangement is {2, 9, 1, 10, 2, 9, 1}

 

Approach: The approach to solve this problem is based on the observation that the elements with maximum frequency must have as much distance between them as possible. Follow the steps mentioned below to solve the problem: 

  • Find out the element(s) with maximum frequency.
  • Suppose m elements have maximum frequency max_freq then conquer m*max_freq positions from the N positions. Now the positions unoccupied are un_pos = N – max_freq*m. 
  • Now, group the elements which do not have maximum frequency consecutively in the same order and have an equal interval.
  • The greatest interval can be given by interval = un_pos/(max_freq-1) because when all the occurrences of m elements are placed equidistant to each other it will break the arrangement in (max_freq – 1) segments.
  • And finally adding m for the highest frequency elements one each we get the answer to the given question as interval + m.

Below is the implementation of the above approach.

C++




// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find
// the maximum possible minimum distance
int find_max_distance(vector<int> a, int n)
{
    int m = 0, max_freq = 0;
    int un_pos, interval, answer;
  
    // To count frequency of
    // each integer value of array
    map<int, int> count_freq;
  
    for (int i = 0; i < n; i++) {
        count_freq[a[i]]++;
        max_freq = max(max_freq,
                       count_freq[a[i]]);
    }
  
    // Loop to find the number of integers
    // in array with maximum frequency
    for (auto iterator : count_freq) {
        if (iterator.second == max_freq)
            m++;
    }
  
    un_pos = n - m * max_freq;
    interval = un_pos / (max_freq - 1);
  
    answer = interval + m;
  
    return answer;
}
  
// Driver Code
int main()
{
    int N = 7;
    vector<int> arr = { 1, 2, 1, 2, 9, 10, 9 };
  
    int result = find_max_distance(arr, N);
    cout << result;
    return 0;
}
Output
4

Time Complexity: O(N)
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!