Rearrange Array: Move Negative Numbers to the Beginning
Given an array ARR
consisting of N
integers, rearrange the elements such that all negative numbers are located before all positive numbers. The order of elements in each category (negative or positive) does not matter.
Can you achieve this with O(1) auxiliary space?
Input:
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains an integer ‘N’, the number of elements in the array.
The second line of every test case contains ‘N’ space-separated integers which are the elements of the array.
Output:
For each test case, output “Yes” if the rearranged array is correct as per the problem statement, otherwise print “No”. Each test case result is printed on a new line.
Example:
Input:
array = [1, 2, -3, 4, -4, -5]
Output:
[-3, -5, -4, 2, 4, 1]
- or any other arrangement where all negative numbers come before positives.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 5 * 104
-105 ≤ ARR[i] ≤ 105
- Time Limit: 1 sec
Note:
No need to print the results yourself, just return the resulting array as per the problem's requirements.

AnswerBot
4mo
Yes, this can be achieved by using a two-pointer approach to rearrange the array in-place with O(1) auxiliary space.
Use two pointers, one starting from the beginning and one from the end of the array....read more
Anonymous
11mo
0
Help your peers!
Add answer anonymously...
Myntra Software Developer interview questions & answers
A Software Developer was asked 5mo agoQ. We are given an array asteroids of integers representing asteroids in a row. For...read more
A Software Developer was asked 9mo agoQ. Given a string of parentheses, find the length of the longest valid (well-formed...read more
A Software Developer was asked 9mo agoQ. Given a string, find the longest palindromic substring.
Popular interview questions of Software Developer
A Software Developer was asked 9mo agoQ1. Given a string of parentheses, find the length of the longest valid (well-formed...read more
A Software Developer was asked 9mo agoQ2. Given a string, find the longest palindromic substring.
A Software Developer was asked 9mo agoQ3. Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge ...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

