Negative To The End

Easy
0/40
Average time to solve is 15m
profile
Contributed by
4 upvotes
Asked in company
Soft Suave

Problem statement

You are given an array ‘A’ of size ‘N’ consisting of both negative and positive integers. You need to return an array in which all the negative numbers are at the end of the array, but the relative order of positive and negative elements is the same.

Example:
Input: ‘N’ = 6
‘A’ = [-1, 2, -3, 1, 13, -10]

Output: [2, 1, 13, -1, -3, 10]

Explanation: In the output array, all the negative elements come after positive elements, and we can also see that the order of positive elements and negative elements is the same, i.e., 2 comes before 1 and 13 in the final array because in the array ‘A’, 2 comes before 1 and 13, and for all other elements, this condition follows.
Detailed explanation ( Input/output format, Notes, Images )
Input Format
First-line contains 'T', denoting the number of Test cases.

For each Test case:

The first line contains two integers, ‘N’, denoting the size of the array ‘A’.

The following line contains ‘N’ integers, denoting the array ‘A’.
Output format:
Return an array in which all the negative numbers are at the end of the array, but the relative order of positive and negative elements is the same.
Note:-
You don't need to print anything. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^3
-1e9 <= A[i] <= 10^9, A[i] != 0

Time Limit: 1-sec
Sample Input 1:
2
6
-1 2 -3 1 13 -10

4
-1 -1 -2 -3
Sample Output 1:
2 1 13 -1 -3 -10
-1 -1 -2 -3
Explanation Of Sample Input 1:
For test case 1:

Input: ‘N’ = 6
‘A’ = [-1, 2, -3, 1, 13, -10]

Output: [2, 1, 13, -1, -3, 10]

Explanation: In the output array, all the negative elements come after positive elements, and we can also see that the order of positive elements and negative elements is the same, i.e., 2 comes before 1 and 13 in the final array because in the array ‘A’, 2 comes before 1 and 13, and for all other elements, this condition follows.


For test case 2:

Input: ‘N’ = 4
‘A’ = [-1, -1, -2, -3]

Output: [-1, -1, -2, -3]

Explanation: Since there are no positive elements, all negative elements are already at the end of the array, so there’s no need to change array ‘A’.
Sample Input 2:
2
4
1 2 1 3

4
-1 2 3 -4
Sample Output 2:
1 2 1 3
2 3 -1 -4
Hint

Use two arrays, one for positive elements and one for negative elements.

Approaches (1)
Implementation

Consider two arrays, posArray and negArray, containing positive and negative elements, respectively. 

Start from the left of the array ‘A’ and go to the right end of the array ‘A’, and at each index, check whether the element is positive or negative and insert the element in the respective array. This way, after the whole iteration, posArray will have all the positive elements, and negArray will have all the negative elements. Still, in both arrays, the relative order will be the same.

 

The steps are as follows:-
 

// Function to move all the negative elements to the end of the array

function negativeToTheEnd(int[] a, int n):
 

  1. Int ‘n’ be the size of array ‘a’.
  2. Initialize two empty arrays, ‘posArray’ and ‘negArray’, consisting of positive and negative elements, respectively, in the order they are present in the array from left to right.
  3. Iterate over array ‘a’ and at each iteration:
    • Initialize a variable ‘curr’, and assign it the current value of the array.
    • If curr < 0:
      • Insert curr into ‘negArray’.
    • Else:
      • Insert curr into ‘posArray’.
  4. Initialize an empty array ‘ans’.
  5. Insert all elements of ‘posArray’ into the array ‘ans’.
  6. Insert all the elements of ‘negArray’ into the array ‘ans’.
  7. Return ‘ans’.
Time Complexity

O( N ), Where 'N' is the size of array ‘A’.
 

We are iterating over all the elements of the array ‘A’ twice(once while iterating using for loop and once while inserting elements into the array ‘ans’),
 

Hence the time complexity is O( N ).

Space Complexity

O( N ), Where 'N' is the size of array ‘A’.
 

We are using two arrays, ‘posArray’ and ‘negArray’ and ‘ans’ arrays to store elements which in total will keep 2*N elements,


Hence the space complexity is O( N ).

Code Solution
(100% EXP penalty)
Negative To The End
Full screen
Console