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

• Difficulty Level : Hard
• Last Updated : 25 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 ``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;``}`

## Java

 `// JAVA code to implement the above approach``import` `java.util.*;``class` `GFG``{``  ` `  ``// Function to find``  ``// the maximum possible minimum distance``  ``public` `static` `int` `find_max_distance(``int``[] a, ``int` `n)``  ``{``    ``int` `m = ``0``, max_freq = ``0``;``    ``int` `un_pos, interval, answer;` `    ``// To count frequency of``    ``// each integer value of array``    ``HashMap count_freq``      ``= ``new` `HashMap<>();` `    ``for` `(``int` `i = ``0``; i < n; i++) {``      ``if` `(count_freq.containsKey(a[i])) {``        ``count_freq.put(a[i],``                       ``count_freq.get(a[i]) + ``1``);``      ``}``      ``else` `{``        ``count_freq.put(a[i], ``1``);``      ``}``      ``max_freq``        ``= Math.max(max_freq, count_freq.get(a[i]));``    ``}` `    ``// Loop to find the number of integers``    ``// in array with maximum frequency``    ``for` `(Map.Entry iterator :``         ``count_freq.entrySet()) {``      ``if` `(iterator.getValue() == max_freq)``        ``m++;``    ``}` `    ``un_pos = n - m * max_freq;``    ``interval = un_pos / (max_freq - ``1``);` `    ``answer = interval + m;` `    ``return` `answer;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `N = ``7``;``    ``int``[] arr = ``new` `int``[] { ``1``, ``2``, ``1``, ``2``, ``9``, ``10``, ``9` `};` `    ``int` `result = find_max_distance(arr, N);``    ``System.out.print(result);``  ``}``}` `// This code is contributed by Taranpreet`

## Python3

 `# Python code to implement the above approach``# Function to find``# the maximum possible minimum distance``def` `find_max_distance(a, n):``    ``m ``=` `0``    ``max_freq ``=` `0``    ``un_pos ``=` `0``    ``interval ``=` `0``    ``answer ``=` `0` `    ``# To count frequency of``    ``# each integer value of array``    ``count_freq ``=` `{}` `    ``for` `i ``in` `range``(n):``        ``if` `a[i] ``not` `in` `count_freq:``            ``count_freq[a[i]] ``=` `1``        ``else``:``            ``count_freq[a[i]] ``+``=` `1``        ``max_freq ``=` `max``(max_freq,count_freq[a[i]])``    `  `    ``# Loop to find the number of integers``    ``# in array with maximum frequency``    ``for` `i ``in` `count_freq:``        ``if` `(count_freq[i] ``=``=` `max_freq):``            ``m ``+``=` `1``    ` `    ``un_pos ``=` `n ``-` `m ``*` `max_freq``    ``interval ``=` `un_pos ``/``/` `(max_freq ``-` `1``)` `    ``answer ``=` `interval ``+` `m` `    ``return` `answer` `# Driver Code``N ``=` `7``arr ``=`  `[``1``, ``2``, ``1``, ``2``, ``9``, ``10``, ``9``]``result ``=` `find_max_distance(arr, N)``print``(result)` `# This code is contributed by rohitsingh07052.`

## C#

 `// C# code to implement the above approach``using` `System;``using` `System.Collections.Generic;``class` `GFG {` `  ``// Function to find``  ``// the maximum possible minimum distance``  ``public` `static` `int` `find_max_distance(``int``[] a, ``int` `n)``  ``{``    ``int` `m = 0, max_freq = 0;``    ``int` `un_pos = 0, interval = 0, answer = 0;` `    ``// To count frequency of``    ``// each integer value of array``    ``Dictionary<``int``, ``int``> count_freq``      ``= ``new` `Dictionary<``int``, ``int``>();` `    ``for` `(``int` `i = 0; i < n; i++) {``      ``if` `(count_freq.ContainsKey(a[i])) {``        ``count_freq[a[i]] = count_freq[a[i]] + 1;``      ``}``      ``else` `{``        ``count_freq.Add(a[i], 1);``      ``}``      ``max_freq = Math.Max(max_freq, count_freq[a[i]]);``    ``}` `    ``// Loop to find the number of integers``    ``// in array with maximum frequency``    ``foreach``(``      ``KeyValuePair<``int``, ``int``> iterator ``in` `count_freq)``    ``{``      ``if` `(iterator.Value == max_freq)``        ``m++;``    ``}` `    ``un_pos = n - m * max_freq;``    ``interval = un_pos / (max_freq - 1);` `    ``answer = interval + m;` `    ``return` `answer;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main()``  ``{``    ``int` `N = 7;``    ``int``[] arr = ``new` `int``[] { 1, 2, 1, 2, 9, 10, 9 };` `    ``int` `result = find_max_distance(arr, N);``    ``Console.Write(result);``  ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`4`

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

My Personal Notes arrow_drop_up