Given an array of integers of size, N. Assume ‘0’ as the invalid number and all others as a valid number. Write a program that modifies the array in such a way that if the next number is a valid number and is the same as the current number, double the current number value and replace the next number with 0. After the modification, rearrange the array such that all 0’s are shifted to the end and the sequence of the valid number or new doubled number is maintained as in the original array.
Example 1:
Ã¢â¬â¹Input : arr[ ] = {2, 2, 0, 4, 0, 8}
Output : 4 4 8 0 0 0
Explanation:
At index 0 and 1 both the elements are same.
So, we can change the element at index 0 to 
4 and element at index 1 is 0 then we shift 
all the values to the end of the array. 
So, array will become [4, 4, 8, 0, 0, 0].
Example 2:
Input : arr[ ] = {0, 2, 2, 2, 0, 6, 6, 0, 0, 8}
Output :  4 2 12 8 0 0 0 0 0 0
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function modifyAndRearrangeArr() that takes an array (arr) and its size (n), and modifies it in-place.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

class Solution:
    def modifyAndRearrangeArr (self, arr,  n) : 
        #Complete the function


Solutions:
class Solution:

	def modifyAndRearrangeArr(self, arr, n):
		ar = []
		z = 0
		i = 0
		while i < n - 1:
			if arr[i] == 0:
				z += 1
			elif arr[i] == arr[i + 1]:
				ar.append(arr[i] * 2)
				z += 1
				i += 1
			else:
				ar.append(arr[i])
			i += 1
		if i < n:
			ar.append(arr[i])
		for x in range(0, z):
			ar.append(0)
		j = 0
		while j < n:
			arr[j] = ar[j]
			j += 1


class Solution:

	def modifyAndRearrangeArr(self, arr, n):
		for i in range(n - 1):
			if arr[i] == arr[i + 1]:
				arr[i] *= 2
				arr[i + 1] = 0
		t = [0] * n
		j = 0
		for i in range(n):
			if arr[i] != 0:
				t[j] = arr[i]
				j += 1
		for i in range(n):
			arr[i] = t[i]


class Solution:

	def modifyAndRearrangeArr(self, arr, n):
		i = 0
		p = 0
		while i < n:
			if i + 1 < n and arr[i] == arr[i + 1]:
				arr[i + 1] = arr[i] * 2
				arr[i] = 0
				p += 1
				i += 1
			i += 1
		if 0 not in arr:
			return arr
		u = arr.index(0)
		b = False
		uu = u + 1
		while uu < n:
			if arr[uu] != 0:
				if u < n:
					(arr[uu], arr[u]) = (arr[u], arr[uu])
					u += 1
				else:
					break
			else:
				uu += 1
		return arr


class Solution:

	def modifyAndRearrangeArr(self, arr, n):
		for i in range(n - 1):
			if arr[i] != 0 and arr[i] == arr[i + 1]:
				arr[i] = 2 * arr[i]
				arr[i + 1] = 0
		count = 0
		for j in range(n):
			if arr[j] != 0:
				(arr[count], arr[j]) = (arr[j], arr[count])
				count += 1
		return arr


class Solution:

	def modifyAndRearrangeArr(self, arr, n):
		for i in range(len(arr) - 1):
			if arr[i] == arr[i + 1]:
				arr[i] = arr[i + 1] + arr[i]
				arr[i + 1] = 0
		l = []
		for i in arr:
			if i > 0:
				l.append(i)
		for j in arr:
			if j == 0:
				l.append(0)
		arr[:] = l[:]


class Solution:

	def modifyAndRearrangeArr(self, arr, n):
		zeroes = []
		number = []
		for i in range(n - 1):
			if arr[i] != 0:
				if arr[i] == arr[i + 1]:
					arr[i] = arr[i] * 2
					arr[i + 1] = 0
		for i in range(n):
			if arr[i] == 0:
				zeroes.append(arr[i])
			else:
				number.append(arr[i])
		c = number + zeroes
		for i in range(n):
			arr[i] = c[i]
		return c


class Solution:

	def modifyAndRearrangeArr(self, arr, n):
		j = 1
		while j < n:
			if arr[j] == arr[j - 1]:
				arr[j - 1] *= 2
				arr[j] = 0
			j += 1
		a = []
		z = []
		for i in arr:
			if i == 0:
				z.append(0)
			else:
				a.append(i)
		arr[:] = a + z
		return arr


class Solution:

	def modifyAndRearrangeArr(self, arr, n):
		ans = [0] * n
		(a, i) = (0, 0)
		while i < n:
			if i < n - 1 and arr[i] != 0 and (arr[i] == arr[i + 1]):
				ans[a] = arr[i] * 2
				a += 1
				i += 1
			elif arr[i] != 0:
				ans[a] = arr[i]
				a += 1
			i += 1
		arr[:] = ans