Given a sorted array consisting 0s and 1s. The task is to find the index of first 1 in the given array. 
 
Example 1:
Input : 
arr[] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1}
Output : 
6
Explanation:
The index of first 1 in the array is 6.
Example 2:
Input : 
arr[] = {0, 0, 0, 0}
Output : 
-1
Explanation:
1's are not present in the array.
 
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function firstIndex() which takes the array A[] and its size N as inputs and returns the index of first 1. If 1 is not present in the array then return -1.
Expected Time Complexity: O(Log (N))
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 10^{6}
0 ≤ A_{i} ≤ 1

Difficulty: EASY
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(Log (N))
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3



class Solution:

    def firstIndex(self, arr, n):

    # Your code goes here





Solutions:
class Solution:

	def firstIndex(self, arr, n):
		lower = 0
		higher = n - 1
		while lower <= higher:
			mid = (lower + higher) // 2
			if arr[mid] == 1 and (mid == 0 or arr[mid - 1] == 0):
				return mid
			elif arr[mid] == 0:
				lower = mid + 1
			else:
				higher = mid - 1
		return -1


class Solution:

	def firstIndex(self, arr, n):
		for i in arr:
			if i == 1:
				x = arr.index(i)
				break
		if 1 not in arr:
			return -1
		else:
			return x


class Solution:

	def firstIndex(self, arr, n):
		if 1 in arr:
			return arr.index(1)
		return -1


class Solution:

	def firstIndex(self, arr, n):
		if 1 not in arr:
			return -1
		for i in range(n):
			if arr[i] != 0:
				return i


class Solution:

	def firstIndex(self, arr, n):
		count = []
		value = []
		for i in range(n):
			if arr[i] == 1:
				count.append(i)
			else:
				value.append(i)
		if len(count) == 0:
			return -1
		else:
			return count[0]


class Solution:

	def firstIndex(self, arr, n):
		for i in range(len(arr)):
			if arr[i] == 1:
				return i
				break
		if 1 not in arr:
			return -1


class Solution:

	def firstIndex(self, arr, n):
		if len(arr) == 1 and arr[0] == 1:
			return 0
		if len(arr) == 1 and arr[0] == 0:
			return -1
		for i in range(n):
			if arr[i] == 1:
				return i
		return -1


class Solution:

	def firstIndex(self, arr, n):
		low = 0
		high = n - 1
		while low <= high:
			mid = (low + high) // 2
			if arr[mid] == 1 and (mid == 0 or arr[mid - 1] == 0):
				return mid
			elif arr[mid] == 0:
				low = mid + 1
			else:
				high = mid - 1
		return -1


class Solution:

	def firstIndex(self, arr, n):
		for i in arr:
			if i == 1:
				return arr.index(i)
		else:
			return -1


class Solution:

	def firstIndex(self, arr, n):
		if arr.count(0) == n:
			return -1
		else:
			return arr.count(0)


class Solution:

	def firstIndex(self, arr, n):
		ans = -1
		lo = 0
		hi = n - 1
		while lo <= hi:
			mid = (hi + lo) // 2
			if arr[mid] == 1:
				hi = mid - 1
				ans = mid
			else:
				lo = mid + 1
		return ans


class Solution:

	def firstIndex(self, arr, n):
		ans = -1
		left = 0
		right = n - 1
		while left <= right:
			mid = (left + right) // 2
			if arr[mid] == 0:
				left = mid + 1
			else:
				ans = mid
				right = mid - 1
		return ans


class Solution:

	def firstIndex(self, arr, n):
		try:
			arr.index(1)
		except ValueError:
			return -1
		return arr.index(1)


class Solution:

	def firstIndex(self, arr, n):
		return ''.join(map(str, arr)).find('1')


class Solution:

	def firstIndex(self, arr, n):
		start = 0
		end = n - 1
		while start <= end:
			mid = start + (end - start) // 2
			if (mid == 0 or arr[mid] > arr[mid - 1]) and arr[mid] == 1:
				return mid
			elif arr[mid] < 1:
				start = mid + 1
			else:
				end = mid - 1
		return -1


class Solution:

	def firstIndex(self, arr, n):
		s = 0
		e = len(arr) - 1
		n = len(arr)
		ind = -1
		while s <= e:
			mid = s + (e - s) // 2
			if arr[mid] == 1:
				ind = mid
				e = mid - 1
			elif arr[mid] >= 1:
				e = mid - 1
			elif arr[mid] < 1:
				s = mid + 1
		return ind


class Solution:

	def firstIndex(self, arr, n):
		b = False
		for i in range(n):
			if arr[i] == 1:
				b = True
				return i
		if not b:
			return -1


class Solution:

	def firstIndex(self, arr, n):
		count = 0
		t = 0
		for i in range(n):
			if arr[i] == 1:
				count = count + 1
				t = i
				break
			else:
				continue
		if count == 1:
			return t
		else:
			return '-1'


class Solution:

	def firstIndex(self, arr, n):
		if arr[0] == 1:
			return 0
		l = 0
		h = len(arr) - 1
		while l <= h:
			mid = l + (h - l) // 2
			if arr[mid] == 1 and arr[mid - 1] == 0:
				return mid
			elif arr[mid] == 1:
				h = mid - 1
			elif arr[mid] == 0:
				l = mid + 1
		return -1


class Solution:

	def firstIndex(self, arr, n):
		l = 0
		r = n - 1
		while l <= r:
			mid = (l + r) // 2
			if arr[mid] == 1 and (mid == 0 or arr[mid - 1] == 0):
				return mid
			elif arr[mid] == 0:
				l = mid + 1
			else:
				r = mid - 1
		return -1


class Solution:

	def firstIndex(self, arr, n):
		s = list(set(arr))
		if len(s) == 1:
			if s[0] == 0:
				return -1
			else:
				return 0
		else:
			for i in range(n):
				if arr[i] == 1:
					return i


class Solution:

	def firstIndex(self, arr, n):

		def FindArray(array):
			if len(array) == 1:
				if array[0] == 1:
					return 0
				else:
					return -1
			left = 0
			right = 1
			if right < len(array):
				return BinarySearch(array, left, right)
			if array[right] != 1:
				right *= 2
			else:
				left = right / 2
				return BinarySearch(array, left, right)

		def BinarySearch(nums, left, right):
			res = -1
			start = 0
			end = len(nums) - 1
			while end >= start:
				mid = (start + end) // 2
				if nums[mid] == 1:
					res = mid
					end = mid - 1
				else:
					start = mid + 1
			return res
		return FindArray(arr)


class Solution:

	def firstIndex(self, arr, n):
		for i in range(n):
			if arr[i] == 1:
				x = i
				break
		for j in range(n):
			if arr[i] != 1:
				y = -1
		if 1 in arr:
			return x
		else:
			return y


class Solution:

	def firstIndex(self, arr, n):
		index = 0
		if 1 in arr:
			for i in range(len(arr)):
				if arr[i] == 1:
					index = i
					break
			return index
		else:
			return -1


class Solution:

	def firstIndex(self, arr, n):
		l = 0
		r = n - 1
		res = -1
		while l <= r:
			mid = l + (r - l) // 2
			if arr[mid] == 1:
				res = mid
				r = mid - 1
			elif arr[mid] < 1:
				l = mid + 1
			elif arr[mid] > 1:
				r = mid - 1
		return res


class Solution:

	def firstIndex(self, arr, n):
		arr.sort()
		for i in range(len(arr)):
			if arr[i] != 1:
				continue
			elif arr[i] == 1:
				return i
		else:
			return -1


class Solution:

	def firstIndex(self, arr, n):
		if 1 not in arr:
			return -1
		a = arr.count(0)
		return a


class Solution:

	def firstIndex(self, arr, n):
		ans = -1
		for i in range(n):
			if arr[i] == 1:
				ans = i
				break
		return ans


class Solution:

	def firstIndex(self, arr, n):
		flag = 0
		for i in range(0, n):
			if arr[i] == 1:
				p = i
				flag = 1
				break
		if flag == 1:
			return p
		else:
			return -1


def binarysearch(arr, l, r):
	if l <= r:
		mid = (l + r) // 2
		if arr[mid] == 1:
			return binarysearch(arr, l, mid - 1)
		return binarysearch(arr, mid + 1, r)
	return l

class Solution:

	def firstIndex(self, arr, n):
		l = binarysearch(arr, 0, n - 1)
		return -1 if l == n else l


class Solution:

	def firstIndex(self, arr, n):
		for x in range(n):
			if arr[x] == 1:
				return x
		return -1


from collections import Counter

class Solution:

	def firstIndex(self, arr, n):
		c = Counter(arr)
		d = c[0]
		d1 = c[1]
		if d1 == 0:
			return -1
		else:
			return d


class Solution:

	def firstIndex(self, arr, n):
		res = -1
		low = 0
		high = n - 1
		while low <= high:
			mid = (low + high) // 2
			if arr[mid] == 1:
				res = mid
				high = mid - 1
			else:
				low = mid + 1
		return res


class Solution:

	def firstIndex(self, arr, n):
		if arr[n - 1] == 0:
			return -1
		return arr.index(1)


class Solution:

	def firstIndex(self, arr, n):
		for i in range(n):
			if arr[i] % 2 != 0:
				return i
		else:
			return -1


class Solution:

	def firstIndex(self, arr, n):
		left = 0
		right = n - 1
		index = -1
		while right >= left:
			mid = int((left + right) / 2)
			if arr[mid] < 1:
				left = mid + 1
			elif arr[mid] == 1:
				index = mid
				right = mid - 1
		return index