# Rearrange array element by sign

[Question]: An given array contains equal number of positive and negative elements. Without altering the relative order of positive and negative elements, you must return an array of alternately positive and negative values.
Example: —
Input: {1,2,-4,-5}
Output: 1 -4 2 -5

###### Approach #1

As positive index is on even numbers so create variable named positiveIndex with 0 & negativeIndex with 1 and increment the index by +2 after fill.

``````// TC: O(n)
// SC: O(1)
func arrangeArray(arr: [Int]) -> [Int] {
var positiveIndex = 0
var negativeIndex = 1
var arrangedArray = Array.init(repeating: 0, count: arr.count)
for i in 0..<arr.count {
if arr[i] < 0 {
arrangedArray[negativeIndex] = arr[i];
negativeIndex += 2;
} else {
arrangedArray[positiveIndex] = arr[i];
positiveIndex += 2;
}
}
return arrangedArray
}

var mixArray = [1,2,-3,-1,-2,3]
let opArrangedArr = arrangeArray(arr: mixArray)
print("The op Arranged Arr  is:", opArrangedArr)// [1, -3, 2, -1, 3, -2]``````