Rearrange numbers in an array such that no two adjacent numbers are same
Given an array of integers. The task is to rearrange elements of the array such that no two adjacent elements in the array are same.
Examples:
Input: arr[] = {1, 1, 1, 2, 2, 2}
Output: {2, 1, 2, 1, 2, 1}
Input: arr[] = {1, 1, 1, 1, 2, 2, 3, 3}
Output: {1, 3, 1, 3, 2, 1, 2, 1}
The idea is to put the highest frequency element first (a greedy approach). We use a priority queue (Or Binary Max Heap) and put all elements and ordered by their frequencies (highest frequency element at the root). We then one by one take the highest frequency element from the heap and add it to result. After we add, we decrease the frequency of the element and temporarily move this element out of priority queue so that it is not picked next time.
We have to follow the step to solve this problem, they are:
- Build a Priority_queue or max_heap, pq that stores elements and their frequencies.
…… Priority_queue or max_heap is built on the basis of the frequency of elements.
- Create a temporary Key that will be used as the previously visited element (the previous element in the resultant array. Initialize it { num = -1, freq = -1 }
- While pq is not empty.
- Pop an element and add it to the result.
- Decrease frequency of the popped element by ‘1’.
- Push the previous element back into the priority_queue if it’s frequency > ‘0’.
- Make the current element as the previous element for the next iteration.
- If the length of resultant string and original are not equal, print “not possible”. Else print result.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void rearrangeArray( int arr[], int N)
{
map< int , int >mp, visited;
for ( int i = 0; i < N; i++)
{
mp[arr[i]]++;
}
priority_queue<pair< int , int >>pq;
for ( int i = 0; i < N ; i++)
{
int val = arr[i];
if (mp[val] > 0 and visited[val] != 1)
{
pq.push({mp[val], val});
}
visited[val] = 1;
}
vector< int >result(N);
pair< int , int >prev = { -1, -1 };
int l = 0;
while (pq.size() != 0)
{
pair< int , int >k = pq.top();
pq.pop();
result[l] = k.second;
if (prev.first > 0)
{
pq.push(prev);
}
k.first--;
prev = k;
l++;
}
for ( auto it : result)
{
if (it == 0)
{
cout << "Not valid Array" << endl;
return ;
}
}
for ( auto it : result)
{
cout << it << ", " ;
}
}
int main()
{
int A[] = { 1, 1, 1, 1, 2, 2, 3, 3 };
int N = sizeof (A) / sizeof (A[0]);
rearrangeArray(A, N);
}
|
Java
import java.util.Comparator;
import java.util.PriorityQueue;
class KeyComparator implements Comparator<Key>
{
public int compare(Key k1, Key k2)
{
if (k1.freq < k2.freq)
return 1 ;
else if (k1.freq > k2.freq)
return - 1 ;
return 0 ;
}
}
class Key
{
int freq;
int num;
Key( int freq, int num)
{
this .freq = freq;
this .num = num;
}
}
public class GFG
{
static void rearrangeArray( int [] arr)
{
int n = arr.length;
int [] count = new int [ 10000 ];
int visited[] = new int [ 10000 ];
for ( int i = 0 ; i < n; i++)
count[arr[i]]++;
PriorityQueue<Key> pq
= new PriorityQueue<>( new KeyComparator());
for ( int i = 0 ; i < n; i++)
{
int val = arr[i];
if (count[val] > 0 && visited[val] != 1 )
pq.add( new Key(count[val], val));
visited[val] = 1 ;
}
int result[] = new int [n];
Key prev = new Key(- 1 , - 1 );
int l = 0 ;
while (pq.size() != 0 )
{
Key k = pq.peek();
pq.poll();
result[l] = k.num;
if (prev.freq > 0 )
pq.add(prev);
(k.freq)--;
prev = k;
l++;
}
if (l != result.length)
{
System.out.println( " Not valid Array " );
}
else
{
for ( int i : result)
{
System.out.print(i + " " );
}
}
}
public static void main(String args[])
{
int arr[] = new int [] { 1 , 1 , 1 , 1 , 2 , 2 , 3 , 3 };
rearrangeArray(arr);
}
}
|
Python3
def rearrangeArray(arr, N) :
mp = {}
visited = {}
for i in range (N) :
if (arr[i] in mp) :
mp[arr[i]] + = 1
else :
mp[arr[i]] = 1
pq = []
for i in range (N) :
val = arr[i]
if ((val in mp) and ((val not in visited) or (visited[val] ! = 1 ))) :
pq.append([mp[val], val])
visited[val] = 1
pq.sort()
pq.reverse()
result = [ 0 ] * N
prev = [ - 1 , - 1 ]
l = 0
while ( len (pq) ! = 0 ) :
k = pq[ 0 ]
pq.pop( 0 )
result[l] = k[ 1 ]
if (prev[ 0 ] > 0 ) :
pq.append(prev)
pq.sort()
pq.reverse()
prev = [k[ 0 ] - 1 , k[ 1 ]]
l + = 1
for it in result :
if (it = = 0 ) :
print ( "Not valid Array" )
return
for it in result :
print (it , end = " " )
A = [ 1 , 1 , 1 , 1 , 2 , 2 , 3 , 3 ]
N = len (A)
rearrangeArray(A, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void rearrangeArray( int [] arr, int N)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
Dictionary< int , int > visited = new Dictionary< int , int >();
for ( int i = 0; i < N; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]] += 1;
}
else {
mp[arr[i]] = 1;
}
}
List<Tuple< int , int >> pq = new List<Tuple< int , int >>();
for ( int i = 0; i < N ; i++)
{
int val = arr[i];
if (mp.ContainsKey(val) && (!visited.ContainsKey(val) || visited[val] != 1))
{
pq.Add( new Tuple< int , int >(mp[val], val));
}
visited[val] = 1;
}
pq.Sort();
pq.Reverse();
int [] result = new int [N];
Tuple< int , int > prev = new Tuple< int , int >( -1, -1 );
int l = 0;
while (pq.Count != 0)
{
Tuple< int , int > k = pq[0];
pq.RemoveAt(0);
result[l] = k.Item2;
if (prev.Item1 > 0)
{
pq.Add(prev);
pq.Sort();
pq.Reverse();
}
prev = new Tuple< int , int >(k.Item1 - 1, k.Item2);
l++;
}
foreach ( int it in result)
{
if (it == 0)
{
Console.WriteLine( "Not valid Array" );
return ;
}
}
foreach ( int it in result)
{
Console.Write(it + " " );
}
}
static void Main()
{
int [] A = { 1, 1, 1, 1, 2, 2, 3, 3 };
int N = A.Length;
rearrangeArray(A, N);
}
}
|
Javascript
<script>
function rearrangeArray(arr, N)
{
var mp = new Map();
var visited = new Map();
for ( var i = 0; i < N; i++)
{
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1)
}
var pq = [];
for ( var i = 0; i < N ; i++)
{
var val = arr[i];
if (mp.get(val) > 0 && visited[val] != 1)
{
pq.push([mp.get(val), val]);
}
visited[val] = 1;
}
pq.sort();
var result = Array(N).fill(0);
var prev = [-1, -1];
var l = 0;
while (pq.length != 0)
{
var k = pq[pq.length-1];
pq.pop();
result[l] = k[1];
if (prev[0] > 0)
{
pq.push(prev);
}
pq.sort();
k[0]--;
prev = k;
l++;
}
for ( var it of result)
{
if (it == 0)
{
document.write( "Not valid Array" + "<br>" );
return ;
}
}
for ( var it of result)
{
document.write(it + " " );
}
}
var A = [1, 1, 1, 1, 2, 2, 3, 3];
var N = A.length;
rearrangeArray(A, N);
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Last Updated :
22 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...