Rearrange The Array

Easy
0/40
5 upvotes
Asked in companies
AmazonAdobeChegg Inc.

Problem statement

You are given an array/list NUM of integers. You are supposed to rearrange the elements of NUM such that no two adjacent elements will be the same or find out if it not possible.

For example:
Input: arr[] = {1,1,1,2,2,2} 
Output: {1,2,1,2,1,2}

Note: {2,1,2,1,2,1} is also valid because there are no two adjacent elements which are the same.
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains an Integer 'N' denoting the size of the array/list.

The second line of each test case contains 'N' space-separated Integers denoting the elements of the array/list.
Output format :
For each test case/query, If it is possible to rearrange then print “YES” else print “NO” in separate lines. 
Note :
You do not need to print anything; it has already been taken care of.
Constraints :
1 <= T <= 100
1 <= N <= 5000
-10^9 <= NUM[i] <= 10^9

Where 'N' is the size of the given array/list and, NUM[i] denotes the i-th element in the array/list.
Sample Input 1 :
2
5
10 10 10 32 32
6
89 47 89 47 42 21
Sample Output 1 :
YES
YES
Explanation to Sample Input 1 :
For the first test case, We can put 32 in between 10 and arrangement looks like. {10,32,10,32,10}.

For the second test case, We have to arrange only 47 and 89 because the rest of the element is unique in itself. So one arrangement looks like { 89, 47, 89, 47, 42, 21}.
Sample Input 2 :
3
5
10 7 21 5 1
6
21 21 21 12 12 21
6
10 10 10 20 20 20
Sample Output 2 :
YES
NO
YES
Explanation to Sample Input 2 :
For the first test case, all the elements have the same frequency, so you can return any arrangement of those elements, i.e. {1, 7, 21, 5, 10}.

For the second test case, we can not rearrange the given array/list because after rearranging {21,12,21,12}, we will be stuck with two 21. There is no way to arrange them.

For the third test case, we can put all the 10 in between 20. So there will be no such adjacent existence which will be the same.
Hint

Can you think some greedy approach will work?

Approaches (1)
Greedy Approach

The key idea is to put the highest frequency element first (a greedy approach). We will be using the priority queue to pick the elements with the highest frequency. 

 

So first, we will store the frequency of each element. And then we will store all the numbers with their frequencies into the priority queue in the highest frequency priority manner. 

 

After that, we will pop elements one by one from the priority queue and add them to result only when the current element is not the same as the most recent element saved in the result. Also, we will decrease the frequency of each element and push it back into the priority queue if the element has a frequency greater than '0'. 

 

Here is the complete algorithm:

  1. Store the frequency of each element in a HashMap.
  2. Build the priority queue that stores the element and their frequencies, where comparison of priority queue elements will be based on the highest frequencies of elements.
  3. Take a temporary pair (elements, frequencies) that will be used as the most recent element which has been added into our result.
  4. Create a variable say “prev” that will be used as the previously visited element (the previous element in the resultant array. Initialize it to {num = -1, freq = -1}.
  5. Iterate until priority queue does not become empty:
    1. Pop an element and add it to the result.
    2. Decrease the frequency of the current element.
    3. Push the previous element and their frequency back into the priority queue if it’s frequency is greater than 0.
    4. Make the current element as the previous element for the next iteration.
  6. If the size of the result is the same as the given array/list, then return the result.
  7. Else return array/list containing only a single element as negative infinity, i.e. -2147483648.
Time Complexity

O(N* log (N) ), where ‘N’ is the number of elements in the given array/list.
 

We are storing the frequency of each element into “HashMap” which will take O(N) time. 

 

Also, we are building a priority queue of element-frequency pairs which will also take O(N) time.

 

Also, we are doing priority queue operations for rearrangement of ‘N’ elements. Pop and push operation take O(logN) time and top operation take O(1). So overall time complexity will be O(N* log(N)).

Space Complexity

O(N), where ‘N’ is the number of elements in the given array/list.
 

We are storing the ‘N’ elements into a priority queue. So in the worst case, when all the elements in the given array/list will be distinct, then the priority queue will take O(N) space. So, overall space complexity is O(N).

Code Solution
(100% EXP penalty)
Rearrange The Array
Full screen
Console