Maximise distance by rearranging all duplicates at same distance in given Array
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++
#include <bits/stdc++.h>
using namespace std;
int find_max_distance(vector< int > a, int n)
{
int m = 0, max_freq = 0;
int un_pos, interval, answer;
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]]);
}
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;
}
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
import java.util.*;
class GFG
{
public static int find_max_distance( int [] a, int n)
{
int m = 0 , max_freq = 0 ;
int un_pos, interval, answer;
HashMap<Integer, Integer> 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]));
}
for (Map.Entry<Integer, Integer> 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;
}
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);
}
}
|
Python3
def find_max_distance(a, n):
m = 0
max_freq = 0
un_pos = 0
interval = 0
answer = 0
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]])
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
N = 7
arr = [ 1 , 2 , 1 , 2 , 9 , 10 , 9 ]
result = find_max_distance(arr, N)
print (result)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int find_max_distance( int [] a, int n)
{
int m = 0, max_freq = 0;
int un_pos = 0, interval = 0, answer = 0;
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]]);
}
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;
}
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);
}
}
|
Javascript
<script>
const find_max_distance = (a, n) => {
let m = 0, max_freq = 0;
let un_pos, interval, answer;
let count_freq = {};
for (let i = 0; i < n; i++) {
if (a[i] in count_freq) count_freq[a[i]]++;
else count_freq[a[i]] = 1;
max_freq = Math.max(max_freq,
count_freq[a[i]]);
}
for (let iterator in count_freq) {
if (count_freq[iterator] == max_freq)
m++;
}
un_pos = n - m * max_freq;
interval = parseInt(un_pos / (max_freq - 1));
answer = interval + m;
return answer;
}
let N = 7;
let arr = [1, 2, 1, 2, 9, 10, 9];
let result = find_max_distance(arr, N);
document.write(result);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...