Given an unsorted array arr[] of size N having both negative and positive integers. The task is place all negative element at the end of array without changing the order of positive element and negative element.
 
Example 1:
Input : 
N = 8
arr[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output : 
1  3  2  11  6  -1  -7  -5
Example 2:
Input : 
N=8
arr[] = {-5, 7, -3, -4, 9, 10, -1, 11}
Output :
7  9  10  11  -5  -3  -4  -1
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function segregateElements() which takes the array arr[] and its size N as inputs and store the answer in the array arr[] itself.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
1 ≤ N ≤ 10^{5}
-10^{5 } ≤ arr[] ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

class Solution:
    def segregateElements(self, arr, n):
        # Your code goes here

Solutions:
class Solution:

	def segregateElements(self, arr, n):
		d = []
		c = []
		for i in range(n):
			if arr[i] < 0:
				d.append(arr[i])
			else:
				c.append(arr[i])
		arr.clear
		f = c + d
		for i in range(len(f)):
			arr[i] = f[i]
		return arr


class Solution:

	def segregateElements(self, arr, n):
		list1 = []
		for i in range(0, n):
			if arr[i] > 0:
				list1.append(arr[i])
		for i in range(0, n):
			if arr[i] < 0:
				list1.append(arr[i])
		for i in range(0, n):
			arr[i] = list1[i]


import copy

class Solution:

	def segregateElements(self, arr, n):
		pos = []
		ne = []
		for i in arr:
			if i < 0:
				ne.append(i)
			else:
				pos.append(i)
		j = -1
		for i in range(0, len(arr)):
			if i < len(pos):
				arr[i] = pos[i]
			else:
				j += 1
				arr[i] = ne[j]


class Solution:

	def segregateElements(self, arr, n):
		pos = []
		ne = []
		for i in range(n):
			if arr[i] >= 0:
				pos.append(arr[i])
			else:
				ne.append(arr[i])
		arr[:] = pos + ne


class Solution:

	def segregateElements(self, arr, n):
		pos = []
		neg = []
		for a in arr:
			if a >= 0:
				pos.append(a)
			else:
				neg.append(a)
		for i in range(len(pos)):
			arr[i] = pos[i]
		for i in range(len(neg)):
			arr[len(pos) + i] = neg[i]


class Solution:

	def segregateElements(self, arr, n):
		pos = []
		neg = []
		for i in arr:
			if i >= 0:
				pos.append(i)
			else:
				neg.append(i)
		tot = pos + neg
		arr[0:n] = tot
		return tot


class Solution:

	def fixPos(self, arr, idx, n):
		temp = arr[idx]
		for i in range(idx, n - 1):
			arr[i] = arr[i + 1]
		arr[n - 1] = temp

	def segregateElements(self, arr, n):
		ls = [0] * n
		pos = 0
		neg = 0
		for i in range(n):
			if arr[i] < 0:
				neg += 1
		neg = n - neg
		for i in range(n):
			if arr[i] < 0:
				ls[neg] = arr[i]
				neg += 1
			else:
				ls[pos] = arr[i]
				pos += 1
		for i in range(n):
			arr[i] = ls[i]


class Solution:

	def segregateElements(self, arr, n):
		l1 = []
		l2 = []
		for i in arr:
			if i >= 0:
				l1.append(i)
			else:
				l2.append(i)
		arr[:] = l1[:] + l2[:]
		return arr


class Solution:

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


class Solution:

	def segregateElements(self, arr, n):
		negative = []
		ori = []
		for i in range(n):
			if arr[i] < 0:
				negative.append(arr[i])
			else:
				ori.append(arr[i])
		ori.extend(negative)
		arr.clear()
		arr.extend(ori)


class Solution:

	def segregateElements(self, arr, n):
		e = []
		o = []
		for i in range(len(arr)):
			if arr[i] < 0:
				o.append(arr[i])
			else:
				e.append(arr[i])
		a = e + o
		for i in range(len(arr)):
			arr[i] = a[i]


class Solution:

	def segregateElements(self, arr, n):
		neg = []
		k = 0
		for i in range(0, n):
			if arr[i] >= 0:
				arr[k] = arr[i]
				k = k + 1
			else:
				neg.append(arr[i])
		for i in range(0, len(neg)):
			arr[k] = neg[i]
			k += 1


class Solution:

	def segregateElements(self, arr, n):
		pos = []
		for i in range(0, n):
			if arr[i] >= 0:
				pos.append(arr[i])
		for i in range(0, n):
			if arr[i] < 0:
				pos.append(arr[i])
		for i in range(0, n):
			arr[i] = pos[i]
		return pos


class Solution:

	def segregateElements(self, arr, n):
		ne = []
		p = []
		for i in range(n):
			if arr[i] < 0:
				ne.append(arr[i])
			elif arr[i] > 0:
				p.append(arr[i])
		for i in range(len(p)):
			arr[i] = p[i]
		j = 0
		for i in range(len(p), n, 1):
			arr[i] = ne[j]
			j = j + 1
		return arr


class Solution:

	def segregateElements(self, arr, n):
		l1 = []
		for i in arr:
			if i > 0:
				l1.append(i)
		for i in arr:
			if i < 0:
				l1.append(i)
		arr[:] = l1[:]


class Solution:

	def segregateElements(self, arr, n):
		(n, p) = ([], [])
		for i in arr:
			if i < 0:
				n.append(i)
			else:
				p.append(i)
		p.extend(n)
		arr.clear()
		arr.extend(p)


class Solution:

	def segregateElements(self, arr, n):
		temp = [0 for i in range(n)]
		j = 0
		for i in range(n):
			if arr[i] >= 0:
				temp[j] = arr[i]
				j += 1
		if j == n:
			return
		for i in range(n):
			if arr[i] < 0:
				temp[j] = arr[i]
				j += 1
		for i in range(n):
			arr[i] = temp[i]


class Solution:

	def segregateElements(self, arr, n):
		res = []
		res1 = []
		for i in arr:
			if i >= 0:
				res.append(i)
			else:
				res1.append(i)
		arr.clear()
		res.extend(res1)
		arr.extend(res)


class Solution:

	def segregateElements(self, arr, n):
		pos = []
		neg = []
		for i in arr:
			if i >= 0:
				pos.append(i)
			else:
				neg.append(i)
		for i in neg:
			pos.append(i)
		arr.clear()
		for i in pos:
			arr.append(i)


class Solution:

	def segregateElements(self, arr, n):
		(p, N) = ([], [])
		for i in range(n):
			if arr[i] > 0:
				p.append(arr[i])
			if arr[i] < 0:
				N.append(arr[i])
		temp = p + N
		for i in range(n):
			arr[i] = temp[i]


class Solution:

	def segregateElements(self, arr, n):
		new = []
		for num in arr:
			if num > 0:
				new.append(num)
		for num in arr:
			if num < 0:
				new.append(num)
		for i in range(n):
			arr[i] = new[i]


class Solution:

	def segregateElements(self, arr, n):
		N = []
		P = []
		for i in arr:
			if i < 0:
				N.append(i)
			else:
				P.append(i)
		arr.clear()
		for i in range(0, len(P)):
			arr.append(P[i])
		for j in range(0, len(N)):
			arr.append(N[j])
		return arr


class Solution:

	def segregateElements(self, arr, n):
		neg = []
		ans = []
		for i in arr:
			if i < 0:
				neg.append(i)
			else:
				ans.append(i)
		ans.extend(neg)
		arr[:] = ans[:]
		return arr


class Solution:

	def segregateElements(self, arr, n):
		negArr = []
		posArr = []
		for i in arr:
			if i < 0:
				negArr.append(i)
			else:
				posArr.append(i)
		posArr.extend(negArr)
		arr.clear()
		arr.extend(posArr)
		return posArr


class Solution:

	def segregateElements(self, arr, n):
		newArr = []
		for ii in range(len(arr)):
			if arr[ii] > 0:
				newArr.append(arr[ii])
		for ii in range(len(arr)):
			if arr[ii] < 0:
				newArr.append(arr[ii])
		for ii in range(len(arr)):
			arr[ii] = newArr[ii]


class Solution:

	def segregateElements(self, arr, n):
		output = []
		for elem in arr:
			if elem > 0:
				output.append(elem)
		for elem in arr:
			if elem < 0:
				output.append(elem)
		for index in range(len(arr)):
			if arr[index] != output[index]:
				arr[index] = output[index]
		return arr


class Solution:

	def segregateElements(self, arr, n):
		from collections import deque
		positiveDeque = deque()
		negativeDeque = deque()
		for num in arr:
			if num >= 0:
				positiveDeque.append(num)
			else:
				negativeDeque.append(num)
		positiveDeque.extend(negativeDeque)
		for i in range(n):
			arr[i] = positiveDeque.popleft()


class Solution:

	def segregateElements(self, arr, n):
		m = []
		b = []
		for i in range(n):
			if arr[i] > 0:
				m.append(arr[i])
			elif arr[i] < 0:
				b.append(arr[i])
		m.extend(b)
		arr.clear()
		arr.extend(m)


class Solution:

	def segregateElements(self, arr, n):
		posList = []
		negList = []
		for x in arr:
			if x < 0:
				negList.append(x)
			else:
				posList.append(x)
		arr.clear()
		arr.extend(posList + negList)


class Solution:

	def segregateElements(self, arr, n):
		arr1 = []
		arr2 = []
		for i in arr:
			if i >= 0:
				arr1.append(i)
			else:
				arr2.append(i)
		arr[:] = arr1 + arr2


class Solution:

	def segregateElements(self, arr, n):
		posarray = []
		negarray = []
		for val in arr:
			if val > 0:
				posarray.append(val)
			else:
				negarray.append(val)
		(i, k) = (0, 0)
		for i in range(len(posarray)):
			arr[k] = posarray[i]
			k = k + 1
		for i in range(len(negarray)):
			arr[k] = negarray[i]
			k = k + 1


class Solution:

	def segregateElements(self, arr, n):
		ans = [0] * n
		i = n - 1
		for j in range(n - 1, -1, -1):
			if arr[j] < 0:
				ans[i] = arr[j]
				i -= 1
		for j in range(n - 1, -1, -1):
			if arr[j] >= 0:
				ans[i] = arr[j]
				i -= 1
		for i in range(n):
			arr[i] = ans[i]


class Solution:

	def segregateElements(self, arr, n):
		neg = []
		pos = []
		for i in arr:
			if i > 0:
				pos.append(i)
			if i < 0:
				neg.append(i)
		arr.clear()
		arr.extend(pos)
		arr.extend(neg)
		return arr


class Solution:

	def segregateElements(self, arr, n):
		list1 = []
		list2 = []
		for i in arr:
			if i >= 0:
				list1.append(i)
			else:
				list2.append(i)
		ans = list1 + list2
		for i in range(len(arr)):
			arr[i] = ans[i]


class Solution:

	def segregateElements(self, arr, n):
		(pos, neg) = ([], [])
		for i in arr:
			if i < 0:
				neg.append(i)
			else:
				pos.append(i)
		index = 0
		for i in pos + neg:
			arr[index] = i
			index = index + 1


class Solution:

	def segregateElements(self, arr, n):
		nl = []
		pl = []
		for i in range(n):
			if arr[i] < 0:
				nl.append(arr[i])
			if arr[i] > 0:
				pl.append(arr[i])
		ans = pl + nl
		for i in range(len(arr)):
			arr[i] = ans[i]


class Solution:

	def segregateElements(self, arr, n):
		a = []
		for i in range(len(arr)):
			if arr[i] >= 0:
				a.append(arr[i])
		for j in range(len(arr)):
			if arr[j] < 0:
				a.append(arr[j])
		arr.clear()
		for ele in a:
			arr.append(ele)


class Solution:

	def segregateElements(self, arr, n):
		p = []
		n = []
		for i in range(len(arr)):
			if arr[i] >= 0:
				p.append(arr[i])
			else:
				n.append(arr[i])
		for i in range(len(p)):
			arr[i] = p[i]
		for j in range(len(p), len(arr)):
			arr[j] = n[j - len(p)]
		return arr


class Solution:

	def segregateElements(self, arr, n):
		arr[:] = [i for i in arr if i > 0] + [i for i in arr if i < 0]
		return arr


class Solution:

	def segregateElements(self, arr, n):
		negs = 0
		for i in arr:
			negs += i < 0
		(start, end) = (0, n - negs)
		for i in arr.copy():
			if i < 0:
				arr[end] = i
				end += 1
			else:
				arr[start] = i
				start += 1
		return arr


class Solution:

	def segregateElements(self, arr, n):
		p_lists = []
		n_lists = []
		i = 0
		j = 0
		for num in arr:
			if num < 0:
				n_lists.append(num)
			else:
				p_lists.append(num)
		while i < len(arr):
			if i < len(p_lists):
				arr[i] = p_lists[i]
				i = i + 1
			else:
				arr[i] = n_lists[j]
				i = i + 1
				j = j + 1
		return arr


class Solution:

	def segregateElements(self, arr, n):
		neg_list = []
		pos_list = []
		for num in arr:
			if num < 0:
				neg_list.append(num)
			elif num > 0:
				pos_list.append(num)
		(i, j) = (0, 0)
		pos_len = len(pos_list)
		while i < len(arr):
			if i < pos_len:
				arr[i] = pos_list[i]
				i += 1
			else:
				arr[i] = neg_list[j]
				i += 1
				j += 1
		return arr


class Solution:

	def segregateElements(self, arr, n):
		a = []
		b = []
		for i in range(n):
			if arr[i] > 0:
				a.append(arr[i])
			else:
				b.append(arr[i])
		ans = a + b
		for i in range(n):
			arr[i] = ans[i]


class Solution:

	def segregateElements(self, arr, n):
		temp = []
		for num in arr:
			if num > 0:
				temp.append(num)
		for num in arr:
			if num < 0:
				temp.append(num)
		for i in range(len(temp)):
			arr[i] = temp[i]
		return arr


class Solution:

	def segregateElements(self, arr, n):
		pos_numbers = []
		neg_numbers = []
		for i in arr:
			if i < 0:
				neg_numbers.append(i)
			else:
				pos_numbers.append(i)
		i = 0
		j = 0
		N = len(pos_numbers)
		while i < len(arr):
			if i < N:
				arr[i] = pos_numbers[i]
				i += 1
			else:
				arr[i] = neg_numbers[j]
				i += 1
				j += 1


class Solution:

	def segregateElements(self, arr, n):
		(p, n) = ([], [])
		for i in arr:
			if i < 0:
				n.append(i)
			else:
				p.append(i)
		arr[:] = p + n
		return arr


class Solution:

	def segregateElements(self, arr, n):
		a = []
		b = []
		for i in range(n):
			if arr[i] < 0:
				a.append(arr[i])
		for i in arr:
			if i > 0:
				b.append(i)
		arr.clear()
		for i in b:
			arr.append(i)
		for i in a:
			arr.append(i)
		return arr


class Solution:

	def segregateElements(self, arr, n):
		a = []
		for x in arr:
			if x >= 0:
				a.append(x)
		for x in arr:
			if x < 0:
				a.append(x)
		for i in range(n):
			arr[i] = a[i]


class Solution:

	def segregateElements(self, arr, n):
		temp1 = []
		temp2 = []
		for i in range(n):
			if arr[i] > 0:
				temp1.append(arr[i])
			else:
				temp2.append(arr[i])
		c = 0
		for i in temp1 + temp2:
			arr[c] = i
			c += 1
		return arr


class Solution:

	def segregateElements(self, arr, n):
		a = []
		b = []
		for i in arr:
			if i < 0:
				a.append(i)
			else:
				b.append(i)
		arr[:] = b + a
		return arr


class Solution:

	def segregateElements(self, arr, n):
		tmp1 = []
		tmp2 = []
		for i in arr:
			if i >= 0:
				tmp1.append(i)
			else:
				tmp2.append(i)
		c = 0
		for i in tmp1 + tmp2:
			arr[c] = i
			c += 1


class Solution:

	def segregateElements(self, arr, n):
		result = []
		for i in arr:
			if i >= 0:
				result.append(i)
		for i in arr:
			if i < 0:
				result.append(i)
		for i in range(0, len(result)):
			arr[i] = result[i]