Java: Move every positive number to the right and every negative number to the left of a given array of integers
Move Positives Right
Write a Java program to move every positive number to the right and every negative number to the left of a given array of integers.
Visual Presentation:
Sample Solution:
Java Code:
Sample Output:
Original array: [-2, 3, 4, -1, -3, 1, 2, -4, 0] Result: [-4, -3, -2, -1, 0, 1, 2, 3, 4]
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to rearrange an array so that all even numbers precede odd numbers.
- Write a Java program to stably partition an array into negative and positive numbers while preserving their order.
- Write a Java program to rearrange an array so that negatives appear first, zeros in the middle, and positives at the end.
- Write a Java program to reverse only the positive numbers in an array while leaving negative numbers in their original positions.
Go to:
PREV : Divide Using Subtraction.
NEXT : Integer to String Format.
Java Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.