
Asked in Myntra
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
12mo
0
Help your peers!
Add answer anonymously...
Top Software Developer Interview Questions Asked at Myntra
Q. We are given an array asteroids of integers representing asteroids in a row. For...read more
Q. Given a string of parentheses, find the length of the longest valid (well-formed...read more
Q. Given a string, find the longest palindromic substring.
Interview Questions Asked to Software Developer at Other Companies
Top Skill-Based Questions for Myntra Software Developer
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 Questions
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

