Javatpoint Logo
Javatpoint Logo

Unequal Adjacent Elements in Java

An array inArr containing n numbers (positive or negative) is given. The task is to return the array after the rearrangement of elements of the integers in such a way that no two adjacent elements are equal. If there are multiple valid arrangements, then any one valid arrangement can be returned. If the input array does not contain any valid arrangement, then an appropriate message can be displayed on the console.

Example 1:

Input

int inArr[] = {7, 7, 6, 8}

Output: {7, 6, 7, 8}

Explanation: There are many valid arrangements possible. Those valid arrangements are mentioned below. {7, 6, 7, 8}, {7, 6, 8, 7}, {7, 8, 7, 6}, {7, 8, 6, 7}, {8, 7, 6, 7}, {6, 7, 8, 7}. Out of which {7, 6, 7, 8} is mentioned in the output.

Example 2:

Input

int inArr[] = {2, 4, 8, 9, 5, 7, 3, 6, 1}

Output: {2, 4, 8, 9, 5, 7, 3, 6, 1}

Explanation: The input array is given in such a way that there are no two elements that are of the same value. Hence, the input arrays are themselves the answer.

Example 3:

Input

int arr[] = {3, 3, 3, 3, 3}

Output: no valid arrangement is possible where adjacent elements are of different values.

Explanation: All the elements are of the same value. Hence, no valid arrangement is possible.

Approach: Brute Force

In this approach, we compute all the arrangements of the input array. If any valid arrangement comes, we return that arrangement. If no valid arrangement is possible, display the appropriate message.

For computing the valid arrangement, first, we will sort the array in ascending order. Then we will find the next permutation of the sorted array and check whether it is a valid arrangement or not. If it is a valid arrangement, then we can return that arrangement. If not, then we will find the next permutation and continue to do so until we find a valid arrangement or all the arrangements are exhausted. If the arrangements are exhausted, then we can say that no valid arrangement is there for the given input array, and the same is illustrated in the following program.

FileName: UniqueAdjEle.java

Output:

For the input array: 
7 7 6 8 
The valid arrangement is: 
6 7 8 7 

For the input array: 
2 4 8 9 5 7 3 6 1 
The valid arrangement is: 
2 4 8 9 5 7 3 6 1 

For the input array: 
3 3 3 3 3 
The valid arrangement is not possible.

Complexity Analysis: Since the program is computing the permutation, the time complexity of the program is O(N!), where N is the total number of elements present in the linked list. The program uses constant space; therefore, the space complexity of the program is O(1).

The above program is not suitable for the larger inputs as the time complexity of the program is too high. Therefore, we must do some optimization to reduce the time complexity.

Approach: Using Set

It is a greedy approach where we put first that element which is occurring the most number of times in the input array. Using the set, we will put all the elements in the order of their occurrence count. The following are the steps involved in computing the answer.

Step 1: Declare an empty array or list solAl.

Step 2: Sort the array or list solAl.

Step 3: Declare a set st that keeps the elements and their occurrence count.

Step 4: Iterate over the list or array solAl:

  • Iterate until the same element is encountered every time. Increment the element count by 1. Put the element and the occurrence count of the element in the set st.

Step 5: Create a temporary variable for keeping the previous element and its occurrence count.

Step 6: While the set st is not empty, do the following:

  • Fetch the last element and get it added to the result. Delete that element from the set st.
  • Reduce the occurrence count of the element popped by 1.
  • If the occurrence count was non-zero of the previous elements, put it in the set.
  • The current element should be treated as the previous element for the upcoming iteration.

Step 7: Eventually, return the list or array solAl.

Implementation

The implementation of the above steps is mentioned below.

FileName: UniqueAdjEle.java

Output:

For the input array: 
7 7 6 8 
The valid arrangement is: 
7 8 7 6 

For the input array: 
2 4 8 9 5 7 3 6 1 
The valid arrangement is: 
2 4 8 9 5 7 3 6 1 

For the input array: 
3 3 3 3 3 
The valid arrangement is not possible.

Complexity Analysis: Since the program is using sorting, the time complexity of the program is O(N * log(N)). The program uses a set to store the elements and their occurrence count. The program is also using an auxiliary array (arr). Thus, making the space complexity of the program as (N), where N is the total number of elements present in the input array.

Note: Instead of Set, a priority queue can also be used. In that case, also, the time and space complexity remain the same.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA