Move All Negative Numbers To Beginning

Rearrange a given array 'ARR' with 'N' integers so that all negative numbers appear before all positive numbers.

Follow Up:
Can you solve this in O(1) auxiliary space? 
Note:
The order of elements in the resulting array is not important.

Example:

Input:
ARR = [1, 2, -3, 4, -4, -5]
Output:
[-3, -5, -4, 2, 4, 1]

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 5 * 104
  • -105 <= ARR[i] <= 105

Where ‘T’ represents the number of test cases and ‘N’ represents the number of elements present in the array.

Note:
You do not need to print anything, it has already been taken care of. Just return the resulting array.
AnswerBot
4mo

Rearrange array with negative numbers at the beginning, order not important.

  • Iterate through the array and swap negative numbers to the beginning using two pointers approach.

  • Maintain one pointer for ne...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at Samsung

Q. Suppose an array of length n sorted in ascending order is rotated between 1 and ...read more
Q. What are the ACID properties in DBMS?
Q. Sum of Digits Problem Statement Given an integer 'N', continue summing its digit...read more
Software Developer Intern 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