InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Alternate positive and negative elements

Given an array of integers A, arrange them in an alternate fashion such that every non-negative number
is followed by negative and vice-versa, starting from a negative number, maintaining the order of appearance.
The number of non-negative and negative numbers need not be equal.

If there are more non-negative numbers they appear at the end of the array.
If there are more negative numbers, they too appear at the end of the array.

Note: Try solving with O(1) extra space.

Input Format

The first argument given is the integer array A.

Output Format

Return the modified array.


1 <= length of the array <= 7000
-10^9 <= A[i] <= 10^9 

For Example

Input 1:
    A = [-1, -2, -3, 4, 5]
Output 1:
    [-1, 4, -2, 5, -3]

Input 2:
    A = [5, -17, -100, -11]
Output 2:
    [-17, 5, -100, -11]
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Alternate positive and negative elements on Interview Code Editor
Sign Up
to access hints and editorial solutions for Alternate positive and negative elements


Click here to start solving coding interview questions