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.

AnswerBot
4mo
The task is to rearrange an array such that no two adjacent elements are the same.
Iterate through the array and check if any adjacent elements are the same.
If adjacent elements are the same, swap one ...read more
Help your peers!
Add answer anonymously...
Oracle Mts1 interview questions & answers
A Mts1 was asked Q. What is a singleton class?
A Mts1 was asked Q. Check if a Number is Binary Determine if a given string of integers bin represen...read more
A Mts1 was asked Q. Number and Digits Problem Statement You are provided with a positive integer N. ...read more
Popular interview questions of Mts1
A Mts1 was asked Q1. What is a singleton class?
A Mts1 was asked Q2. Check if a Number is Binary Determine if a given string of integers bin represen...read more
A Mts1 was asked Q3. Number and Digits Problem Statement You are provided with a positive integer N. ...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

