Rearrange The Array Problem Statement
You are given an array/list 'NUM' of integers. Rearrange the elements of 'NUM' such that no two adjacent elements are the same in the rearranged array.
Example:
Input:
NUM[] = {1,1,1,2,2,2}
Output:
{1,2,1,2,1,2}
Note: {2,1,2,1,2,1} is also valid as no two adjacent elements are the same.
Input:
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:
For each test case/query, if it is possible to rearrange then print “YES” else print “NO” in separate lines. And if the output given by the user is wrong then print “Invalid Output”.
If it is possible to rearrange then return any right arrangement of the given array/list otherwise put a single integer INT_MIN in the array/list and return that.
Constraints:
- 1 <= T <= 10
- 1 <= N <= 104
- -109 <= NUM[i] <= 109
- Time Limit: 1 sec.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Be the first one to answer
Add answer anonymously...
Top Oracle Mts1 interview questions & answers
Popular interview questions of Mts1
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app