Arrange Negative Array Elements Before Positive Elements in Java


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to arrange all elements of array such that negative elements are present before positive elements.

If a number is greater than 0 then it is called as positive number, if less than 0 then it is called as negative number.

Note − The array must be an integer array.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Suppose the original array is {1, 2, -3, -4, 5, -6, 7, -8, 9}.

After rearranging the array, the result will be −

-8 -6 -4 -3 1 2 5 7 9

Instance-2

Suppose the original array is {8, 2, -6, 4, -11, 15, 27, -8, -9}

After rearranging the array, the result will be −

-11 -9 -8 -6 2 4 8 15 27

Instance-3

Suppose the original array is {12, 21, -31, -14, 56, 16, 17, -18, 9}

After rearranging the array, the result will be −

-31 -18 -14 9 12 16 17 21 56

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Sort the array in ascending order

  • Step 3 − Initialize for loop to print array elements

  • Step 4 − Print the elements of the array.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length

where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array.

  • By Using User Defined Method.

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm arrange all elements of array such that negative elements are present before positive elements.

import java.util.*;
public class Main {
   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int[] arr = { 12, 21, -31, -14, 56, 16, 17, -18, 9 };
      System.out.println("Original array: \n"+Arrays.toString(arr));
      
      //sorting array in ascending order
      Arrays.sort(arr);
      System.out.println("Updated array: ");
      
      //for each loop to print array elements
      for (int e : arr)
         System.out.print(e + " ");
   }
}

Output

Original array: 
[12, 21, -31, -14, 56, 16, 17, -18, 9]
Updated array: 
-31 -18 -14 9 12 16 17 21 56

Approach-2: By Using User Defined Method

Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm arrange all elements of array such that negative elements are present before positive elements.

import java.util.*;
public class Main{
   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int[] arr = { 8, 2, -6, 4, -11, 15, 27, -8, -9 };
      System.out.println("Original array: \n"+Arrays.toString(arr));
      
      //call a user defined method 
      negative_first(arr);
   }
   //user defined method 
   public static void negative_first(int[] arr){
   
      //sorting array in ascending order
      Arrays.sort(arr);
      System.out.println("Updated array: ");
      
      //for loop to print array elements
      for (int e : arr)
         System.out.print(e + " ");
   }
}

Output

Original array: 
[8, 2, -6, 4, -11, 15, 27, -8, -9]
Updated array: 
-11 -9 -8 -6 2 4 8 15 27

In this article, we explored how to arrange all elements of array such that negative elements are present before positive elements by using Java programming language.

Updated on: 05-Jan-2023

628 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements