Challenge: Re-arrange 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 at the other? Solve this problem in Java and see if your code runs correctly!

Problem Statement #

In this problem, you have to implement the void reArrange(int[] arr) method, which will sort the elements, such that all the negative elements appear at the left and positive elements appear at the right.

Note: Consider 0 as a positive number.

Method Prototype #

void reArrange(int[] arr)

Output #

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

Sample Input #

arr = {10, -1, 20, 4, 5, -9, -6}

Sample Output #

arr = {-1, -9, -6, 10, 20, 4, 5}

Note: Order of the numbers doesn’t matter.

{-1, -9, -6, 10, 20, 4, 5} = {-9, -1, -6, 10, 4, 20, 5}

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