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 ≤ 10^{6}
1 ≤ arr[i] ≤ 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class Solution:
    def arranged(self,a,n):
        #Code here

Solutions:
class Solution:

	def arranged(self, a, n):
		neg = []
		pos = []
		for i in a:
			if i < 0:
				neg.append(i)
			else:
				pos.append(i)
		l = []
		for i in range(n // 2):
			l.append(pos[0])
			pos.pop(0)
			l.append(neg[0])
			neg.pop(0)
		l = l + neg + pos
		return l


class Solution:

	def arranged(self, a, n):
		ne = []
		p = []
		for i in a:
			if i < 0:
				ne.append(i)
			else:
				p.append(i)
		j = 0
		for i in range(0, n, 2):
			a[i] = p[j]
			j += 1
		j = 0
		for i in range(1, n, 2):
			a[i] = ne[j]
			j += 1
		return a


class Solution:

	def arranged(self, a, n):
		p = []
		m = []
		k = []
		for i in a:
			if i > 0:
				p.append(i)
			else:
				m.append(i)
		min_len = min(len(p), len(m))
		for i in range(min_len):
			k.append(p[i])
			k.append(m[i])
		return k


class Solution:

	def arranged(self, a, n):
		b = []
		c = []
		for i in a:
			if i < 0:
				b.append(i)
			else:
				c.append(i)
		j = 0
		k = 0
		for i in range(0, len(a)):
			if i % 2 == 0:
				a[i] = c[j]
				j += 1
			else:
				a[i] = b[k]
				k += 1
		return a


class Solution:

	def arranged(self, a, n):
		b = []
		c = []
		for i in a:
			if i > 0:
				b.append(i)
			else:
				c.append(i)
		a[::2] = b[:]
		a[1::2] = c[:]
		return a


class Solution:

	def arranged(self, a, n):
		(p, n) = ([], [])
		for i in a:
			if i > 0:
				p.append(i)
			else:
				n.append(i)
		ans = []
		for (i, j) in zip(p, n):
			ans.append(i)
			ans.append(j)
		return ans


class Solution:

	def arranged(self, a, n):
		pl = []
		nl = []
		for i in range(n):
			if a[i] >= 1:
				pl.append(a[i])
			else:
				nl.append(a[i])
		a[::2] = pl[:]
		a[1::2] = nl[:]
		return a


class Solution:

	def arranged(self, a, n):
		list1 = [0 for i in range(1, n + 1)]
		list2 = [i for i in a if i > 0]
		list3 = [i for i in a if i < 0]
		(m, j) = (0, 0)
		for i in range(len(a)):
			if i % 2 == 0:
				list1[i] = list2[m]
				m += 1
			else:
				list1[i] = list3[j]
				j += 1
		return list1


class Solution:

	def arranged(self, a, n):
		pos = []
		neg = []
		for i in a:
			if i < 0:
				neg.append(i)
			else:
				pos.append(i)
		an = []
		for i in range(len(pos)):
			an.append(pos[i])
			an.append(neg[i])
		return an


class Solution:

	def arranged(self, a, n):
		N = []
		P = []
		res = []
		for i in a:
			if i >= 0:
				P.append(i)
			else:
				N.append(i)
		for i in range(n // 2):
			res.append(P[i])
			res.append(N[i])
		return res


class Solution:

	def arranged(self, a, n):
		k = []
		b = []
		res = []
		for i in a:
			if i > 0:
				k.append(i)
			else:
				b.append(i)
		for i in range(n // 2):
			res.append(k[i])
			res.append(b[i])
		return res


class Solution:

	def arranged(self, a, n):
		positive_arr = []
		negative_arr = []
		for i in a:
			if i < 0:
				negative_arr.append(i)
			else:
				positive_arr.append(i)
		result = []
		for i in range(0, n, 2):
			result.append(positive_arr[i // 2])
			result.append(negative_arr[i // 2])
		return result


class Solution:

	def arranged(self, a, n):
		a1 = []
		b = []
		r = []
		for i in a:
			if i > 0:
				a1.append(i)
			else:
				b.append(i)
		for i in range(n // 2):
			r.append(a1[i])
			r.append(b[i])
		return r


class Solution:

	def arranged(self, a, n):
		p = [i for i in a if i > 0]
		n = [i for i in a if i < 0]
		a[::2] = p[:]
		a[1::2] = n[:]
		return a


class Solution:

	def arranged(self, a, n):
		l1 = []
		for i in a:
			if i > 0:
				l1.append(i)
		l2 = []
		for i in a:
			if i < 0:
				l2.append(i)
		l = []
		for i in range(len(l1)):
			l.append(l1[i])
			l.append(l2[i])
		return l


class Solution:

	def arranged(self, a, n):
		pos = []
		neg = []
		p = [0] * n
		for i in range(n):
			if a[i] >= 0:
				pos.append(a[i])
			if a[i] < 0:
				neg.append(a[i])
		for j in range(n):
			if j % 2 == 0:
				p[j] = pos[j // 2]
			else:
				p[j] = neg[j // 2]
		return p


class Solution:

	def arranged(self, a, n):
		p = []
		ne = []
		nu = []
		for i in range(len(a)):
			if a[i] < 0:
				ne.append(a[i])
			else:
				p.append(a[i])
		for j in range(len(p)):
			nu.append(p[j])
			nu.append(ne[j])
		return nu


class Solution:

	def arranged(self, a, n):
		pos = [i for i in a if i > 0]
		neg = [i for i in a if i < 0]
		a[::2] = pos[:]
		a[1::2] = neg[:]
		return a


class Solution:

	def arranged(self, a, n):
		e = []
		o = []
		for i in range(len(a)):
			if a[i] > 0:
				e.append(a[i])
			else:
				o.append(a[i])
		l = []
		for i in range(len(e)):
			l.append(e[i])
			l.append(o[i])
		return l


class Solution:

	def arranged(self, a, n):
		pint = []
		nint = []
		for i in a:
			if i < 0:
				nint.append(i)
			else:
				pint.append(i)
		f = 1
		ans = []
		k = 0
		for i in range(0, n):
			if f:
				ans.append(pint[k])
			else:
				ans.append(nint[k])
				k += 1
			f ^= 1
		return ans


class Solution:

	def arranged(self, a, n):
		n = len(a)
		positive = []
		negative = []
		for i in range(n):
			if a[i] > 0:
				positive.append(a[i])
			else:
				negative.append(a[i])
		i = 0
		j = 0
		k = 0
		while i < len(positive) and j < len(negative):
			a[k] = positive[i]
			k += 1
			i += 1
			a[k] = negative[j]
			k += 1
			j += 1
		while i < len(positive):
			a[k] = positive[i]
			k += 1
			i += 1
		while j < len(negative):
			a[k] = negative[j]
			k += 1
			j += 1
		return a


class Solution:

	def arranged(self, a, n):
		pos = []
		neg = []
		res = []
		for i in range(n):
			if a[i] > 0:
				pos.append(a[i])
			elif a[i] < 0:
				neg.append(a[i])
		m = int(n / 2)
		for i in range(m):
			res.append(pos[i])
			res.append(neg[i])
		return res


class Solution:

	def arranged(self, a, n):
		c = []
		d = []
		e = []
		for i in a:
			if i < 0:
				c.append(i)
			else:
				d.append(i)
		while c or d:
			if len(d):
				e.append(d.pop(0))
			if len(c):
				e.append(c.pop(0))
		return e


class Solution:

	def arranged(self, a, n):
		pos = []
		neg = []
		for x in a:
			neg.append(x) if x < 0 else pos.append(x)
		a.clear()
		for i in range(n):
			if pos:
				a.append(pos.pop(0))
			if neg:
				a.append(neg.pop(0))
		return a


class Solution:

	def arranged(self, a, n):
		p = []
		ng = []
		ans = []
		for i in range(n):
			if a[i] > 0:
				p.append(a[i])
			elif a[i] < 0:
				ng.append(a[i])
		for i in range(len(p)):
			ans.append(p[i])
			ans.append(ng[i])
		return ans


class Solution:

	def arranged(self, a, n):
		ans = [0] * n
		(p, k) = (0, 1)
		for i in range(0, n):
			if a[i] >= 0:
				ans[p] = a[i]
				p += 2
			else:
				ans[k] = a[i]
				k += 2
		return ans


class Solution:

	def arranged(self, a, n):
		pve = []
		nve = []
		res = []
		k = 0
		m = 0
		for i in range(n):
			if a[i] > 0:
				pve.append(a[i])
			elif a[i] < 0:
				nve.append(a[i])
		for i in range(n):
			if i % 2 == 0:
				res.append(pve[k])
				k = k + 1
			else:
				res.append(nve[m])
				m = m + 1
		return res


class Solution:

	def arranged(self, a, n):
		pos_num = [i for i in a if i > 0]
		neg_num = [i for i in a if i < 0]
		a[::2] = pos_num[:]
		a[1::2] = neg_num[:]
		return a


class Solution:

	def arranged(self, a, n):
		(x, y) = ([], [])
		for i in a:
			if i > 0:
				x.append(i)
			else:
				y.append(i)
		l = []
		for i in range(len(x)):
			l.append(x[i])
			l.append(y[i])
		return l


class Solution:

	def arranged(self, a, n):
		p = []
		ne = []
		for i in a:
			if i < 0:
				ne.append(i)
			else:
				p.append(i)
		flg = 0
		j = 0
		k = 0
		for i in range(n):
			if flg == 0:
				a[i] = p[j]
				flg = 1
				j += 1
			elif flg == 1:
				a[i] = ne[k]
				flg = 0
				k += 1
		return a


class Solution:

	def arranged(self, a, n):
		pos = [i for i in a if i >= 0]
		neg = [i for i in a if i < 0]
		res = []
		for (i, j) in zip(pos, neg):
			res.append(i)
			res.append(j)
		return res


class Solution:

	def arranged(self, a, n):
		p = [i for i in a if i > 0]
		n = [i for i in a if i < 0]
		res = []
		for i in range(len(p)):
			res.append(p[i])
			res.append(n[i])
		return res


class Solution:

	def arranged(self, a, n):
		positive = []
		negative = []
		for number in a:
			if number < 0:
				negative.append(number)
			if number > 0:
				positive.append(number)
		output = []
		for i in range(n // 2):
			output.append(positive[i])
			output.append(negative[i])
		return output


class Solution:

	def arranged(self, a, n):
		l = []
		s = []
		z = []
		for i in a:
			if i < 0:
				l.append(i)
			elif i > 0:
				s.append(i)
		for (i, j) in zip(s, l):
			z.append(i)
			z.append(j)
		return z


class Solution:

	def arranged(self, a, n):
		a1 = []
		a2 = []
		a3 = []
		for i in a:
			if i > 0:
				a1.append(i)
			else:
				a2.append(i)
		for i in range(int(n / 2)):
			a3.append(a1[i])
			a3.append(a2[i])
		return a3


class Solution:

	def arranged(self, a, N):
		j = 0
		p = []
		n = []
		ans = []
		for i in range(N):
			if a[i] < 0:
				n.append(a[i])
			elif a[i] > 0:
				p.append(a[i])
		while j < len(p):
			ans.append(p[j])
			ans.append(n[j])
			j += 1
		return ans


class Solution:

	def arranged(self, a, n):
		p = []
		q = []
		for i in a:
			if i >= 0:
				p.append(i)
			else:
				q.append(i)
		a[0] = p[0]
		p1 = 1
		q1 = 0
		for i in range(1, n):
			if i % 2 != 0:
				a[i] = q[q1]
				q1 += 1
			else:
				a[i] = p[p1]
				p1 += 1
		return a


class Solution:

	def arranged(self, a, n):
		l = []
		l1 = [0] * n
		l2 = [0] * n
		(j1, j2) = (1, 0)
		for i in a:
			if i < 0:
				l1[j1] = i
				j1 += 2
			else:
				l2[j2] = i
				j2 += 2
		for i in range(n):
			l.append(l1[i] + l2[i])
		return l


class Solution:

	def arranged(self, a, n):
		pos = []
		neg = []
		p = []
		for i in a:
			if i > 0:
				pos.append(i)
			else:
				neg.append(i)
		while pos or neg:
			if len(pos):
				p.append(pos.pop(0))
			if len(neg):
				p.append(neg.pop(0))
		return p


class Solution:

	def arranged(self, a, n):
		x = []
		y = []
		for i in a:
			if i > 0:
				x.append(i)
			else:
				y.append(i)
		z = []
		for i in range(n // 2):
			z.append(x[i])
			z.append(y[i])
		return z


class Solution:

	def arranged(self, a, n):
		array = [0] * n
		negative = 0
		positive = n // 2
		for i in a:
			if i < 0:
				array[negative] = i
				negative += 1
			else:
				array[positive] = i
				positive += 1
		new_array = []
		neg = 0
		pos = n // 2
		for j in range(n // 2):
			new_array.append(array[pos])
			new_array.append(array[neg])
			pos += 1
			neg += 1
		return new_array


class Solution:

	def arranged(self, a, n):
		arr = []
		j = 0
		g = 1
		for i in range(n):
			arr.append(0)
		for i in range(n):
			if a[i] > 0:
				arr[j] = a[i]
				j = j + 2
		for i in range(n):
			if a[i] < 0:
				arr[g] = a[i]
				g = g + 2
		return arr


class Solution:

	def arranged(self, a, n):
		temp = []
		temp1 = []
		for i in a:
			if i < 0:
				temp.append(i)
			else:
				temp1.append(i)
		a[::2] = temp1
		a[1::2] = temp
		return a


class Solution:

	def arranged(self, a, n):
		neg = [i for i in a if i < 0]
		pos = [i for i in a if i > 0]
		return [sub[items] for items in range(len(neg)) for sub in [pos, neg]]


class Solution:

	def arranged(self, a, n):
		p = []
		m = []
		ans = []
		for v in a:
			if v > 0:
				p.append(v)
			else:
				m.append(v)
		for i in range(0, len(p)):
			ans.append(p[i])
			ans.append(m[i])
		return ans


class Solution:

	def arranged(self, a, n):
		ne = list()
		p = list()
		for i in a:
			if i > 0:
				p.append(i)
			else:
				ne.append(i)
		l = list()
		for j in range(0, n):
			if j % 2 == 0:
				a[j] = p[int(j / 2)]
			else:
				a[j] = ne[int(j / 2)]
		a = tuple(a)
		return a


class Solution:

	def arranged(self, a, n):
		p = []
		o = []
		for i in a:
			if i >= 0:
				p.append(i)
			else:
				o.append(i)
		a[::2] = p[:]
		a[1::2] = o[:]
		return a


class Solution:

	def arranged(self, arr, n):
		P = []
		N = []
		for elem in arr:
			if elem < 0:
				N.append(elem)
			else:
				P.append(elem)
		i = 0
		j = 0
		while i < n / 2:
			arr[j] = P[i]
			arr[j + 1] = N[i]
			j += 2
			i += 1
		return arr


class Solution:

	def arranged(self, a, n):
		l1 = []
		l2 = []
		for i in a:
			if i < 0:
				l1.append(i)
			else:
				l2.append(i)
		a[::2] = l2[:]
		a[1::2] = l1[:]
		return a


class Solution:

	def arranged(self, a, n):
		z = []
		x = []
		for i in range(0, n):
			if a[i] < 0:
				z.append(a[i])
			elif a[i] >= 0:
				x.append(a[i])
		return [sub[item] for item in range(len(z)) for sub in [x, z]]


class Solution:

	def arranged(self, a, n):
		l1 = []
		l2 = []
		for i in a:
			if i > 0:
				l1.append(i)
			else:
				l2.append(i)
		a = list(zip(l1, l2))
		ans = []
		for i in a:
			for j in i:
				ans.append(j)
		return ans


class Solution:

	def arranged(self, a, n):
		l = []
		m = []
		for i in a:
			if i > 0:
				l.append(i)
			else:
				m.append(i)
		res = []
		for i in range(len(l)):
			res.append(l[i])
			res.append(m[i])
		return res


class Solution:

	def arranged(self, a, n):
		l = list(a)
		(x, x1, x2) = ([], [], [])
		for i in l:
			if i > 0:
				x.append(i)
			else:
				x1.append(i)
		for i in range(len(l) // 2):
			x2.append(x[i])
			x2.append(x1[i])
		return x2


class Solution:

	def arranged(self, a, n):
		b = []
		c = []
		for i in a:
			if i > 0:
				b.append(i)
			else:
				c.append(i)
		y = []
		for i in range(len(b)):
			y.append(b[i])
			y.append(c[i])
		return y


class Solution:

	def arranged(self, a, n):
		ne = []
		p = []
		for j in a:
			if j < 0:
				ne.append(j)
			else:
				p.append(j)
		f = 0
		r = []
		N = 0
		P = 0
		for j in range(n):
			if f == 0:
				r.append(p[P])
				P += 1
				f = 1
			else:
				r.append(ne[N])
				N += 1
				f = 0
		return r


class Solution:

	def arranged(self, a, n):
		flag = True
		pptr = 0
		nptr = 0
		res = []
		while len(res) < len(a):
			if flag:
				if a[pptr] > 0:
					res.append(a[pptr])
					flag = False
				pptr += 1
			else:
				if a[nptr] < 0:
					res.append(a[nptr])
					flag = True
				nptr += 1
		return res


class Solution:

	def arranged(self, a, n):
		i = 0
		j = 0
		ng = []
		ps = []
		res = []
		while i < n:
			if a[i] < 0:
				ng.append(a[i])
			else:
				ps.append(a[i])
			i += 1
		while j < len(ps):
			res.append(ps[j])
			res.append(ng[j])
			j += 1
		return res


class Solution:

	def arranged(self, a, n):
		a1 = []
		a2 = []
		for i in range(n):
			if a[i] > 0:
				a1.append(a[i])
			else:
				a2.append(a[i])
		j = k = 0
		while j < len(a1):
			a[k] = a1[j]
			a[k + 1] = a2[j]
			k += 2
			j += 1
		return a


class Solution:

	def arranged(self, a, n):
		ar = []
		neg = [i for i in a if i < 0]
		pos = [i for i in a if i >= 0]
		k = 0
		while k < n // 2:
			ar.append(pos[k])
			ar.append(neg[k])
			k += 1
		return ar


class Solution:

	def arranged(self, a, n):
		i = 0
		j = 0
		ng = []
		pt = []
		result = []
		while i < n:
			if a[i] < 0:
				ng.append(a[i])
			else:
				pt.append(a[i])
			i += 1
		while j < len(pt):
			result.append(pt[j])
			result.append(ng[j])
			j += 1
		return result


class Solution:

	def arranged(self, a, n):
		li = []
		for i in a:
			if i < 0:
				li.append(i)
		for i in a:
			if i > 0:
				li.append(i)
		k = n // 2
		for j in range(0, n - 1, 2):
			a[j] = li[k]
			k = k + 1
		k = 0
		for j in range(1, n, 2):
			a[j] = li[k]
			k = k + 1
		return a


class Solution:

	def arranged(self, a, n):
		even = [i for i in a if i > 0]
		odd = [i for i in a if i < 0]
		a[::2] = even[:]
		a[1::2] = odd[:]
		return a


class Solution:

	def arranged(self, a, n):
		pos = []
		neg = []
		for i in range(n):
			if a[i] < 0:
				neg.append(a[i])
			else:
				pos.append(a[i])
		i = 0
		j = 1
		x = 0
		y = 0
		while i < n:
			a[i] = pos[x]
			i = i + 2
			x = x + 1
		while j < n:
			a[j] = neg[y]
			j = j + 2
			y = y + 1
		return a