You are given an array arr of n integers. You must return the minimum number of operations required to make this array 0. For this you can do an operation :
Choose a sub-array of non-zero elements & replace all with 0(0 must be present in arr, if not you can not replace it).
Example 1:
Input:
n = 4
arr = {3,0,4,5}
Output:
2
Explanation:
First, we can choose 3 replace with 0(which is on 1st Index) and in the second operation, we can choose 4 & 5 -> replace with 0(1st Index).
Example 2:
Input:
n = 8 
arr = {10,4,9,6,10,10,4,4} 
Output: 
-1 
Explanation: 
If we do operations here, we can not make the entire 0 
because no 0 is present in the array, hence -1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function arrayOperations() which takes an integer n and an array arr as inputs and find the minimum number of operations you must perform to make all the elements of the array 0. If not possible return -1;
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <= 10^{5}
0 <= arr[i] <= 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: from typing import List





class Solution:

    def arrayOperations(self, n : int, arr : List[int]) -> int:

        # code here

        



Solutions:
from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		num = 0
		subset = 0
		i = 1
		while i < len(arr):
			if arr[i] == 0 and arr[i - 1] == 0:
				arr = arr[:i - 1] + arr[i:]
			else:
				i += 1
		if len(arr) == 1:
			if arr[0] == 0:
				return 0
			else:
				return -1
		for i in arr:
			if i == 0:
				num += 1
		if num == 0:
			return -1
		if arr[0] == 0:
			num -= 1
		if arr[-1] == 0:
			num -= 1
		return num + 1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		flg = 0
		i = 0
		c = 0
		cnt = 0
		while i < n:
			if arr[i] == 0:
				flg = 1
				if c > 0:
					cnt += 1
				c = 0
				i += 1
			elif arr[i] > 0:
				c = 1
				i += 1
		if c > 0:
			cnt += 1
		if flg == 1:
			return cnt
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		k = arr.count(0)
		flag = 0
		count = 0
		if k == n:
			return 0
		elif k == 0:
			return -1
		else:
			for i in arr:
				if i != 0:
					flag = 1
				elif flag == 1:
					count += 1
					flag = 0
			if flag == 1:
				count += 1
			return count


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if arr.count(0) == 0:
			return -1
		if set(arr) == {0}:
			return 0
		else:
			f = 0
			c = 0
			for i in range(n):
				if arr[i] != 0:
					if not f:
						c += 1
					f = 1
				else:
					f = 0
			return c


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if arr.count(0) == 0:
			return -1
		if arr[-1] != 0:
			arr.append(0)
		if arr[0] != 0:
			arr.insert(0, 0)
		sub_list = []
		last_zero_at = 0
		for (ind, i) in enumerate(arr):
			if i == 0:
				s = arr[last_zero_at + 1:ind]
				if len(s) > 0:
					sub_list.append(s)
				last_zero_at = ind
		return len(sub_list)


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		count = 0
		zero = 0
		nonZero = False
		zerofound = False
		for i in range(n):
			if arr[i] == 0:
				zerofound = True
				zero += 1
				if nonZero:
					count += 1
					nonZero = False
			else:
				nonZero = True
		if nonZero and zerofound:
			count += 1
		if zero == n:
			return 0
		if count > 0:
			return count
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		has0 = False
		non0 = False
		c = 0
		for i in arr:
			if i != 0:
				non0 = True
			else:
				has0 = True
				if non0:
					c += 1
					non0 = False
		if arr[-1] != 0:
			c += 1
		if has0:
			return c
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		total = 0
		i = 0
		(count, zero) = (0, 0)
		isNonZero = False
		isZeroFound = False
		for i in range(n):
			if arr[i] == 0:
				isZeroFound = True
				zero += 1
				if isNonZero:
					count += 1
					isNonZero = False
			else:
				isNonZero = True
		if isNonZero and isZeroFound:
			count += 1
		if zero == n:
			return 0
		return count if count > 0 else -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		last = 0
		c = 0
		ez = 0
		for i in range(n):
			if arr[i] == 0:
				ez = 1
				if last != 0:
					c += 1
			last = arr[i]
		if last != 0:
			c += 1
		if ez:
			return c
		return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		count = 0
		t = 1
		if 0 not in arr:
			return -1
		for x in range(n):
			if arr[x] == 0:
				t = 1
			elif arr[x] != 0 and t != 0:
				t = 0
				count += 1
		return count


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		(l, r, c) = (0, 0, 0)
		z_count = arr.count(0)
		if z_count < 1:
			return -1
		if z_count == n:
			return 0
		else:
			while l <= r and r <= n - 1:
				if r == n - 1 and arr[r] != 0:
					c += 1
					break
				elif arr[r] != 0 and arr[r + 1] == 0:
					l = r + 1
					r = r + 2
					c += 1
				else:
					r += 1
			return c


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		ans = 0
		len = 0
		for i in arr:
			if i == 0:
				if len:
					ans += 1
				len = 0
			else:
				len += 1
		if len == n:
			return -1
		if len:
			return ans + 1
		else:
			return ans


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		flag = False
		sub_ray = False
		res = 0
		for i in range(n):
			if arr[i] == 0:
				flag = True
			if arr[i] != 0 and (not sub_ray):
				sub_ray = True
			elif arr[i] == 0 and sub_ray:
				res += 1
				sub_ray = False
		if sub_ray == True and flag == True:
			res += 1
			sub_ray = False
		if sub_ray == True and res == 0:
			return -1
		return res


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		d = 0
		for i in range(n):
			if arr[i] == 0:
				d += 1
		if d == n:
			return 0
		arr = [0] + arr + [0]
		c = 0
		p = 0
		for i in range(n + 2):
			if arr[i] == 0:
				if i - p > 1 and i - p != n + 1:
					c += 1
				p = i
		if c == 0:
			return -1
		return c


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		non_zero = 0
		count = 0
		if sum(arr) == 0:
			return count
		for i in arr:
			if i != 0:
				non_zero += 1
			elif i == 0 and non_zero > 0:
				count += 1
				non_zero = 0
		if count == 0:
			return -1
		elif non_zero > 0:
			return count + 1
		else:
			return count


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		run = False
		partition = 0
		zeroSeen = False
		for x in arr:
			if x != 0:
				if not run:
					run = True
					partition += 1
			else:
				if not zeroSeen:
					zeroSeen = True
				if run:
					run = False
		return partition if partition > 1 or (zeroSeen and partition == 1) or partition == 0 else -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		i = 0
		c = 0
		fl = False
		while i < n:
			if arr[i] != 0:
				c += 1
				while i < n and arr[i] != 0:
					i += 1
			else:
				i += 1
				fl = True
		if not fl:
			return -1
		return c


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		operations = 0
		i = 0
		while i < n:
			if arr[i] == 0:
				i += 1
				continue
			j = i
			while j < n and arr[j] != 0:
				j += 1
			operations += 1
			i = j
		return operations


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		c = 0
		for i in range(1, len(arr)):
			if arr[i - 1] != 0 and arr[i] == 0:
				c += 1
		if arr[-1] > 0:
			c += 1
		return c


from typing import List

class Solution:

	def arrayOperations(self, n: int, l: List[int]) -> int:
		if l.count(0) == 0:
			return -1
		l = [0] + l + [0]
		n = n + 2
		c = 0
		m = []
		for i in range(n):
			if l[i] == 0:
				m.append(i)
		for i in range(1, len(m)):
			if m[i] - m[i - 1] > 1:
				c += 1
		return c


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		c = 0
		start = 0
		end = 0
		for i in range(0, n):
			if arr[i] == 0:
				end = i
				if end - start > 0:
					c += 1
					start = i + 1
					end = i + 1
				else:
					start = i + 1
					end = i + 1
			else:
				end += 1
		if end - start == n:
			return -1
		if end - start > 0:
			c += 1
		return c


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		start = 0
		end = 0
		cnt = 0
		zero = 0
		for e in arr:
			if e == 0:
				start += 1
				zero = 1
			else:
				break
		for e in arr[::-1]:
			if e == 0:
				end += 1
				zero = 1
			else:
				break
		i = start
		while i < len(arr) - end:
			flag = 1
			while arr[i] == 0:
				if flag:
					cnt += 1
					flag = 0
				i += 1
			else:
				i += 1
		if cnt:
			return cnt + 1
		elif len(arr) - end <= start:
			return 0
		elif zero:
			return 1
		return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		a = 0
		c = 0
		if 0 not in arr:
			return -1
		for i in arr:
			if i == 0:
				a += c
				c = 0
			else:
				c = 1
		a += c
		return a


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		count = 0
		prev_not_zero = False
		has_zero = False
		for (i, a) in enumerate(arr):
			if prev_not_zero is False and a != 0:
				prev_not_zero = True
				count += 1
			if a == 0:
				prev_not_zero = False
				has_zero = True
		if has_zero:
			return count
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 in arr:
			count = 0
			while 0 in arr:
				temp = arr.index(0)
				if len(arr[:temp]) > 0:
					count = count + 1
				arr = arr[temp + 1:]
			if len(arr) > 0:
				count = count + 1
			return count
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		count = 0
		i = 0
		while i < n:
			if arr[i] == 0:
				i += 1
				continue
			count += 1
			while i < n and arr[i] != 0:
				arr[i] = 0
				i += 1
		if sum(arr) == 0:
			return count
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		if not any(arr):
			return 0
		ans = 0
		flag = 0
		start = None
		for i in arr:
			if i != 0:
				flag = 0
				continue
			elif i == 0 and flag == 0:
				flag = 1
				ans += 1
		if arr[n - 1] != 0:
			ans += 1
		return ans


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		c = 0
		k = 0
		j = 0
		if 0 not in arr:
			return -1
		else:
			for i in range(n):
				if arr[i] != 0:
					j = 1
					k += 1
				elif k > 0:
					k = 0
					c += 1
			if arr[n - 1] != 0:
				c += 1
			if j > 0 and c > 0:
				return c
			else:
				return j


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		count = 0
		i = n - 1
		previous_count = arr.count(0)
		while arr.count(0) < n:
			while arr[i] != 0:
				arr[i] = 0
				i -= 1
			if previous_count < arr.count(0):
				previous_count = arr.count(0)
				count += 1
			if arr.count(0) == n:
				return count
			i -= 1
		return count


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		count = 0
		if arr[0] != 0:
			count += 1
		for (ind, element) in enumerate(arr):
			if element == 0:
				if ind + 1 < len(arr) and arr[ind + 1] != 0:
					count += 1
		return count


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		l = arr
		cop = l[:]
		c = 0
		for i in range(len(l) - 1):
			if l[i] == 0:
				if l[i + 1] == 0:
					del cop[i + 1 - c]
					c += 1
		x = cop[1:len(cop) - 1].count(0)
		if x == 0:
			if l.count(0) != 0:
				if l.count(0) == len(l):
					return 0
				else:
					return 1
			else:
				return -1
		else:
			return x + 1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		c = 0
		i = 0
		if arr.count(0) == 0:
			return -1
		else:
			while i < n:
				if arr[i] != 0:
					c += 1
					while i < n and arr[i] != 0:
						i += 1
				else:
					i += 1
		return c


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		prev = 0
		c = 0
		sum1 = 0
		for i in range(n):
			if arr[i] != 0:
				prev = c
				c += 1
			elif prev == 0 and c == 0:
				c = 0
				pass
			elif c != 0:
				sum1 += 1
				prev = c
				c = 0
		if c != 0:
			sum1 += 1
		return sum1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		c = 0
		s = 0
		zero = False
		for i in arr:
			if i == 0:
				zero = True
			if i == 0:
				if s != 0:
					c += 1
					s = 0
			else:
				s += i
		if s != 0:
			c += 1
		if c == 0:
			return 0
		elif c > 1:
			return c
		elif zero:
			return 1
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		res = i = 0
		while i < n:
			if arr[i] != 0:
				while i < n and arr[i] != 0:
					i += 1
				res += 1
			else:
				i += 1
		return res


from typing import List

class Solution:

	def arrayOperations(self, n: int, a: List[int]) -> int:
		ans = 0
		f = 0
		e = 0
		pre = -1
		while f < n:
			if f != 0 and a[f - 1] != 0 and (a[f] == 0):
				ans = ans + 1
			if a[f] == 0:
				pre = f
			f += 1
		if a[-1] == 0:
			return ans
		elif pre != -1:
			return ans + 1
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		ans = 0
		for i in range(0, n):
			if arr[0] == 0 and i == 0:
				continue
			elif arr[i] == 0 and arr[i - 1] != 0:
				ans += 1
		if arr[-1] != 0:
			ans += 1
		return ans


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		cnt = 0
		prefix = 0
		for i in range(n):
			if prefix > 0 and arr[i] == 0:
				prefix = 0
				cnt += 1
			else:
				prefix += arr[i]
		if prefix > 0:
			cnt += 1
		if cnt > 1 or arr[0] == 0 or arr[n - 1] == 0:
			return cnt
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		(temp, count) = (0, 0)
		while temp < n:
			if temp < n and arr[temp] != 0:
				while temp < n and arr[temp] != 0:
					temp += 1
				count += 1
			elif temp <= n and arr[temp] == 0:
				while temp < n and arr[temp] == 0:
					temp += 1
		return count if all(arr) == 0 else -1


from typing import List
import numpy as np

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		zero1 = np.all(arr)
		if zero1 == False:
			arr0 = np.zeros(n, dtype='int')
			if (arr0 == arr).all():
				return 0
			else:
				a = 0
				c = 0
				for i in range(0, n):
					if arr[i] == 0:
						a = a + (c > 0)
						c = 0
						continue
					c = c + 1
				a = a + (c > 0)
				return a
		else:
			return -1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		cnt = 0
		sm = 0
		if 0 not in arr:
			return -1
		for i in arr:
			if i == 0:
				if sm != 0:
					cnt += 1
				sm = 0
			else:
				sm += i
		if sm:
			cnt += 1
		return cnt


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		x = 0
		for i in range(n):
			if arr[i] != 0 and (i == 0 or arr[i - 1] == 0):
				x += 1
		return x


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		nn = arr.count(0)
		if nn == 0:
			return -1
		if nn == n:
			return 0
		count = 0
		ind = 0
		while ind < n:
			if arr[ind] != 0:
				break
			ind += 1
		while ind < n:
			while ind < n:
				if arr[ind] == 0:
					break
				ind += 1
			count += 1
			while ind < n:
				if arr[ind] != 0:
					break
				ind += 1
		return count


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		l = [set()]
		for i in arr:
			if i != 0:
				l[-1].add(i)
			else:
				l.append(set())
		l = list(filter(lambda x: x, l))
		return len(l)


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		ans = 0
		non_zero = 0
		for i in range(len(arr)):
			if arr[i] != 0:
				non_zero += 1
			elif non_zero > 0:
				ans += 1
				non_zero = 0
		if non_zero > 0:
			if ans == 0:
				return -1
			ans += 1
		return ans


from typing import List

class Solution:

	def arrayOperations(self, n: int, v: List[int]) -> int:
		(ans, length) = (0, 0)
		for i in range(n):
			if arr[i] == 0:
				if length:
					ans += 1
				length = 0
			else:
				length += 1
		if length == n:
			return -1
		if length:
			ans += 1
		return ans


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		count = 0
		other = 0
		flag = 0
		for i in range(n):
			if arr[i] == 0:
				flag = 1
		for i in range(n):
			if other == 0 and arr[i] == 0:
				continue
			elif other != 0 and arr[i] == 0:
				other = 0
				count += 1
			if arr[i] != 0:
				other += 1
			if i == n - 1 and other != 0:
				count += 1
		if flag == 0:
			return -1
		return count


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		i = 0
		cnt = 0
		for j in range(n):
			modified = False
			if arr[j] != 0:
				continue
			else:
				if j - i != 0:
					cnt += 1
					modified = True
				i = j + 1
		if not modified:
			if j - i + 1 != 0:
				cnt += 1
		return cnt


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		ans = 0
		le = 0
		for i in range(0, n):
			if arr[i] == 0:
				if le > 0:
					ans += 1
				le = 0
				continue
			le += 1
		if le > 0:
			if le == n:
				return -1
			else:
				return ans + 1
		else:
			return ans


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		ans = 0
		nonZero = 0
		flag = False
		for i in range(len(arr)):
			if arr[i] != 0:
				nonZero += 1
			else:
				flag = True
				if nonZero > 0:
					ans += 1
				nonZero = 0
		if flag == True:
			if nonZero > 0:
				ans += 1
		else:
			return -1
		return ans


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		c = 0
		d = 0
		c0 = 0
		for i in range(len(arr)):
			if arr[i] != 0:
				if d == 0:
					c += 1
					d = 1
			elif arr[i] == 0:
				d = 0
				c0 += 1
		if c0 == 0:
			return -1
		return c


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		if 0 not in arr:
			return -1
		ct = 0
		zeroList = []
		for i in range(len(arr)):
			if arr[i] == 0:
				zeroList.append(i)
		for i in range(1, len(zeroList)):
			if zeroList[i] - zeroList[i - 1] > 1:
				ct += 1
		if arr[0] != 0:
			ct += 1
		if arr[-1] != 0:
			ct += 1
		return ct


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		cnt = 0
		flag = False
		allNonZero = True
		for i in range(n):
			if arr[i] != 0:
				if not flag:
					cnt += 1
				flag = True
			else:
				allNonZero = False
				flag = False
		if allNonZero:
			return -1
		return cnt


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		count = 0
		res = 0
		for i in range(n):
			if i != 0 and arr[i - 1] == 0:
				continue
			if arr[i] == 0:
				count += 1
		if count == 0:
			return -1
		res = count + 1
		if arr[0] == 0 and arr[n - 1] == 0:
			res -= 2
		elif arr[0] == 0 or arr[n - 1] == 0:
			res -= 1
		return res


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		c = 0
		i = 0
		while i < n:
			if arr[i] == 0:
				c += 1
				while arr[i] == 0:
					i += 1
					if i == n:
						break
			else:
				i += 1
		if sum(arr) == 0:
			return 0
		if c == 0:
			return -1
		if arr[-1] == 0:
			return c
		else:
			return c + 1


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		ans = 0
		conti = False
		p = 0
		for i in range(n):
			if arr[i] == 0:
				p += 1
				if conti:
					ans += 1
					conti = False
			elif not conti:
				conti = True
		if conti:
			ans += 1
		if p == 0:
			return -1
		return ans


from typing import List

class Solution:

	def arrayOperations(self, n: int, arr: List[int]) -> int:
		ans = 0
		cnt = 0
		for p in range(n):
			if arr[p] == 0:
				if cnt != 0:
					ans = ans + 1
					cnt = 0
			else:
				cnt = cnt + 1
		if cnt == n:
			return -1
		if cnt != 0:
			ans = ans + 1
		return ans