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 with negative numbers at the beginning.

  • Use two pointers, one starting from the beginning and one from the end of the ar...read more

Help your peers!
Select
Add answer anonymously...

SAP Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked Q. Longest common subsequence
An Associate Software Engineer was asked Q. All Prime Numbers Less Than or Equal to N Given a positive integer N, your task ...read more
An Associate Software Engineer was asked Q. Problem: Total Area of Overlapping Rectangles You are given two rectangles situa...read more

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked Q1. Longest common subsequence
An Associate Software Engineer was asked Q2. All Prime Numbers Less Than or Equal to N Given a positive integer N, your task ...read more
An Associate Software Engineer was asked Q3. Problem: Total Area of Overlapping Rectangles You are given two rectangles situa...read more
SAP Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits