Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

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.

```
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

```
1 <= T <= 10
1 <= N <= 10^3
-1e9 <= A[i] <= 10^9, A[i] != 0
Time Limit: 1-sec
```

```
2
6
-1 2 -3 1 13 -10
4
-1 -1 -2 -3
```

```
2 1 13 -1 -3 -10
-1 -1 -2 -3
```

```
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’.
```

```
2
4
1 2 1 3
4
-1 2 3 -4
```

```
1 2 1 3
2 3 -1 -4
```