Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6560
@blackshadows 6400
@mb1973 5777
@Quandray 5271
@akhayrutdinov 5111
@saiujwal13083 5074
@kirtidee18 4356
@sanjay05 3762
@mantu_singh 3638
@gfgaccount 3601
@marius_valentin_dragoi 3525
Complete Leaderboard
Positive and negative elements
Basic Accuracy: 68.01% Submissions: 716 Points: 1

Given an array arr[ ] containing equal number of positive and negative elements, arrange the array such that every positive element is followed by a negative element.
Note- The relative order of positive and negative numbers should be maintained.

Example 1:

Input:
N = 6
arr[] = {-1, 2, -3, 4, -5, 6}
Output:  
2 -1 4 -3 6 -5
Explanation: Positive numbers in order 
are 2, 4 and 6. Negative numbers in order 
are -1, -3 and -5. So the arrangement we 
get is 2, -1, 4, -3, 6 and -5.

Example 2:

Input:
N = 4
arr[] = {-3, 2, -4, 1}
Output:  
2 -3 1 -4 

Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function arranged() that takes array arr[ ] and its size integer N as parameters and returns the required array.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 106
1 ≤ arr[i] ≤ 109

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Positive and negative elements

Output Window