Challenge: Rearrange Positive & Negative Values

Given an array, can you re-arrange its elements in such a way that the negative elements appear at one side and positive elements appear in the other? Solve this problem in JavaScript and see if your code runs correctly!

Problem Statement #

Implement a function, reArrange(arr), which sorts the elements so that all the negative elements appear on the left, and all positive elements appear at the right.

While zero is NOT positive or negative, we consider zero to be a positive​ integer for this challenge!

Input #

An array containing positive and negative elements

Output #

A sorted array with negative elements at the left and positive elements at the right

Sample Input #

[10,-1,20,4,5,-9,-6]

Sample Output #

[-1,-9,-6,10,20,4,5]

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.