Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Rearrange Alternatively
EASY
15 mins
25 upvotes
Sorting
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Sorting
-
-
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Rearrange Alternatively

Contributed by
Ashwani
Easy
yellow-spark
0/40
Avg time to solve 15 mins
Success Rate 85 %
Share
25 upvotes

Problem Statement

Given an array arr of size N containing positive and negative integers. Arrange the array alternatively such that every non-negative integer is followed by a negative integer and vice-versa.

Note:
The number of positive integers and negative integers may not be equal. In such cases, add the extra integers at the end.
For Example:
For array {4, -9, -2, 6, -8}, the output will be {-9, 4, -2, 6, -8}

For array {1, 2, 3, -5}, the output will be {-5, 1, 2, 3}   
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample input 1:
1
4
-1 2 2 -5 
Sample output 1:
-1 2 -5 2 
Sample input 2:
2
5
-2 -3 -3 -6 -2
3
1 -1 -1
Sample output 2:
-2 -3 -3 -6 -2
-1 1 -1
Reset Code
Full screen
Auto
copy-code
Console