Given a number N having only one ‘1’ and all other ’0’s in its binary representation, find position of the only set bit. If there are 0 or more than 1 set bit the answer should be -1. Position of  set bit '1' should be counted starting with 1 from LSB side in binary representation of the number.
Example 1:
Input:
N = 2
Output:
2
Explanation:
2 is represented as "10" in Binary.
As we see there's only one set bit
and it's in Position 2 and thus the
Output 2.
Example 2:
Input:
N = 5
Output:
-1
Explanation:
5 is represented as "101" in Binary.
As we see there's two set bits
and thus the Output -1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function findPosition() which takes an integer N as input and returns the answer.
Expected Time Complexity: O(log(N))
Expected Auxiliary Space: O(1)
Constraints:
0 <= N <= 10^{8}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3

class Solution:
    def findPosition(self, N):
        # code here 

Solutions:
class Solution:

	def findPosition(self, N):
		if N == 0 or N & N - 1 != 0:
			return -1
		for i in range(32):
			if 1 << i & N != 0:
				return i + 1


class Solution:

	def findPosition(self, N):
		res = 0
		num = bin(N)
		num = num[2:]
		num = num[::-1]
		if N == 0:
			return -1
		if num.count('1') > 1:
			return -1
		else:
			idx = num.index('1')
			return int(idx) + 1


class Solution:

	def findPosition(self, N):
		if N == 0:
			return -1
		b = bin(N)[2:]
		k = b.count('1')
		if k > 1:
			return -1
		p = b[::-1]
		q = p.index('1')
		return q + 1


class Solution:

	def findPosition(self, N):
		n = bin(N)
		count = 0
		l = len(n)
		for i in range(l):
			if n[i] == '1':
				count = count + 1
				temp = l - i
		if count == 1:
			return temp
		else:
			return '-1'


import math

class Solution:

	def findPosition(self, N):
		if N == 0:
			return -1
		b = bin(N)[2:]
		k = str(b)
		if k.count('1') == 1:
			k = k[::-1]
			return k.index('1') + 1
		return -1


class Solution:

	def findPosition(self, n):
		if n == 0:
			return -1
		if n & n - 1 == 0:
			c = 0
			while n:
				c += 1
				n = n >> 1
			return c
		else:
			return -1


class Solution:

	def findPosition(self, N):
		b = 0
		c = bin(N).replace('0b', '')
		b = c.count('1')
		if b == 1:
			return len(c)
		else:
			return -1


class Solution:

	def findPosition(self, N):
		pos = 1
		count = 0
		while N != 0:
			if N & 1 == 1:
				ans = pos
				count += 1
			N >>= 1
			pos += 1
		if count == 0 or count > 1:
			return -1
		return ans


class Solution:

	def findPosition(self, N):
		count = 0
		N = bin(N)
		r = N[2:][::-1]
		for i in r:
			if i == '1':
				count = count + 1
		if count == 1:
			for j in r:
				if j == '1':
					g = r.index(j)
					g = g + 1
					return g
		else:
			return -1


class Solution:

	def findPosition(self, N):
		count = 0
		n = 0
		while N:
			if N & 1:
				count += 1
			N = N >> 1
			n += 1
		if count > 1:
			return -1
		else:
			return count + n - 1


class Solution:

	def findPosition(self, N):
		if N == 0:
			return -1
		pos = 0
		while N > 0:
			if N & 1 == 1 and N != 1:
				return -1
			N = N >> 1
			pos = pos + 1
		return pos


import math

class Solution:

	def findPosition(self, N):
		if N == 0:
			return -1
		if N & N - 1 == 0:
			return int(math.log(N, 2)) + 1
		else:
			return -1


class Solution:

	def findPosition(self, n):
		if n == 0:
			return -1
		if n & n - 1:
			return -1
		pos = 1
		while n ^ 1:
			n = n >> 1
			pos += 1
		return pos


class Solution:

	def findPosition(self, N):
		binario = bin(N)[2:]
		if binario.count('1') == 1:
			return len(binario) - binario.find('1')
		return -1


import math

class Solution:

	def findPosition(self, N):
		if N == 0:
			return -1
		if N == 1:
			return 1
		if bin(N).count('1') == 1:
			return int(math.log2(N) + 1)
		return -1


class Solution:

	def findPosition(self, N):
		for i in range(32):
			if 2 ** i == N:
				return i + 1
		return -1


class Solution:

	def findPosition(self, N):
		t = bin(N)
		b = t[::-1]
		if b.count('1') == 1:
			return b.index('1') + 1
		else:
			return -1


class Solution:

	def findPosition(self, N):
		N = bin(N)[2:]
		if N.count('1') == 0 or N.count('1') > 1:
			return -1
		else:
			return N[::-1].index('1') + 1


class Solution:

	def findPosition(self, N):
		k = bin(N)
		k = k[2:]
		if k.count('1') == 1:
			return len(k) - k.index('1')
		return -1


class Solution:

	def findPosition(self, N):
		found = False
		ans = -1
		i = 0
		while i < 32:
			if N & 1 << i:
				if found == False:
					found = True
					ans = i + 1
				else:
					ans = -1
			i = i + 1
		return ans


class Solution:

	def findPosition(self, N):
		bin = ''
		while N > 0:
			rem = N % 2
			bin = str(rem) + bin
			N = N // 2
		if bin.count('1') == 0 or bin.count('1') > 1:
			return -1
		bin = bin[::-1]
		return bin.index('1') + 1


class Solution:

	def findPosition(self, N):
		if bin(N).count('1') > 1 or '1' not in bin(N):
			return -1
		m = list(bin(N))
		m.remove('b')
		p = ''.join(m)
		return len(m) - p.rindex('1')


class Solution:

	def findPosition(self, N):
		if N & N - 1 != 0 or N <= 0:
			return -1
		count = 0
		while N:
			N = N // 2
			count += 1
		return count


from math import log

class Solution:

	def findPosition(self, n):
		if n == 0 or n & n - 1 != 0:
			return -1
		else:
			return int(log(n, 2)) + 1


class Solution:

	def findPosition(self, n):
		if n == 0 or n & n - 1 != 0:
			return -1
		i = 1
		pos = 1
		while i <= n:
			if i == n:
				return pos
			else:
				pos += 1
				i *= 2


class Solution:

	def findPosition(self, N):
		a = bin(N).replace('0b', '')
		k = a[::-1].find('1')
		if a.count('1') == 1:
			return k + 1
		return -1


class Solution:

	def findPosition(self, N):
		(bin, d, idx) = (format(N, 'b'), {'0': 0, '1': 0}, 0)
		for i in bin:
			d[i] += 1
			if d['1'] < 1:
				idx += 1
		return len(bin) - idx if d['1'] == 1 else -1


class Solution:

	def findPosition(self, N):
		cnt = 0
		w = N
		while N:
			N = N & N - 1
			cnt += 1
		if cnt == 1:
			ans = 1
			while w:
				if w & 1 == 1:
					return ans
				else:
					w = w >> 1
					ans += 1
			return ans
		return -1


class Solution:

	def findPosition(self, n):
		result = 1
		count = 0
		for pos in range(1, 33):
			if n & 1 << pos - 1 == 0:
				pos += 1
			else:
				result = pos
				count += 1
		return result if count == 1 else -1


class Solution:

	def findPosition(self, N):
		ans = 0
		a = 0
		for i in range(1, 31):
			if N & 1 != 0:
				ans += 1
				a = i
			N = N >> 1
		if ans == 1:
			return a
		else:
			return -1


class Solution:

	def findPosition(self, N):
		t = bin(N)
		t = list(t)
		if t.count('1') > 1 or t.count('1') == 0:
			return -1
		t = t[::-1]
		for i in range(len(t)):
			if t[i] == '1':
				return i + 1


class Solution:

	def findPosition(self, N):
		b = str(bin(N))
		if '1' not in b or b.count('1') > 1:
			return -1
		b_rev = b[::-1]
		index = b_rev.index('1')
		return index + 1


class Solution:

	def findPosition(self, N):
		i = 0
		idx = 0
		count = 0
		if N == 0:
			return -1
		while N > 0:
			if N & 1 == 1:
				count += 1
				idx = i
			N = N >> 1
			i += 1
			if count > 1:
				return -1
		return idx + 1


class Solution:

	def findPosition(self, N):
		position = 0
		count = 0
		i = 1
		while N:
			if N & 1:
				count += 1
				position = i
			N >>= 1
			i += 1
		if count > 1 or count == 0:
			return -1
		return position


class Solution:

	def findPosition(self, n):
		bn = bin(n)[2:]
		oneCount = bn.count('1')
		if oneCount > 1 or oneCount == 0:
			return -1
		else:
			pos = bn.index('1')
			return len(bn) - pos


class Solution:

	def findPosition(self, n):
		bn = bin(n)[2:]
		oneCount = bn.count('1')
		if oneCount > 1 or oneCount == 0:
			return -1
		else:
			pos = 1
			for i in range(len(bn) - 1, -1, -1):
				if bn[i] == '0':
					pos += 1
				else:
					break
			return pos


class Solution:

	def findPosition(self, n):
		num = bin(n)[2:]
		if num.count('1') != 1:
			return -1
		return len(num) - num.index('1')


class Solution:

	def findPosition(self, N):
		res = -1
		count = 0
		t = 1
		while N:
			if N & 1:
				res = t
				count += 1
			N = N // 2
			t += 1
		if count > 1:
			return -1
		return res


class Solution:

	def findPosition(self, N):
		c = 0
		p = 0
		while N > 0:
			p += 1
			if N % 2:
				c += 1
				if c == 2:
					break
			N = N >> 1
		if c == 1:
			return p
		return -1


import math

class Solution:

	def findPosition(self, N):
		n = N
		c = 0
		while N > 0:
			N = N & N - 1
			c += 1
		if c == 0 or c > 1:
			return -1
		else:
			return int(math.log(n & -n) / math.log(2)) + 1


class Solution:

	def findPosition(self, N):
		count = 0
		position = 0
		i = 0
		while N != 0:
			bit = N & 1
			i += 1
			N = N >> 1
			if bit == 1:
				count += 1
				position = i
		if count == 1:
			return position
		else:
			return -1


class Solution:

	def findPosition(self, N):
		binN = '{0:032b}'.format(N)
		ans = 0
		if binN.count('1') > 1 or binN.count('1') == 0:
			return -1
		else:
			for i in range(len(binN[::-1])):
				if binN[i] == '1':
					ans = 32 - i
			return ans


class Solution:

	def findPosition(self, N):
		if not N or N & N - 1:
			return -1
		ans = 0
		while N:
			N //= 2
			ans += 1
		return ans


class Solution:

	def findPosition(self, n):
		if n and n & n - 1 == 0:
			i = 31
			while i >= 0:
				if 1 << i == n:
					return i + 1
				i -= 1
		return -1


class Solution:

	def findPosition(self, n):
		if n == 0:
			return -1
		elif n & n - 1 == 0:
			s = 0
			while n > 0:
				n = n >> 1
				s = s + 1
			return s
		else:
			return -1


class Solution:

	def findPosition(self, N):
		count = 0
		pos = 0
		if N == 0:
			return -1
		while N >= 1:
			if N % 2 == 1:
				if pos != 0:
					count = -1
					break
				else:
					pos = count
			count += 1
			N = N >> 1
		return count


class Solution:

	def findPosition(self, N):
		M = N
		count = 0
		p = 0
		while M:
			M = M & M - 1
			count += 1
		if count == 1:
			while N:
				N = N >> 1
				p += 1
			return p
		else:
			return -1


class Solution:

	def findPosition(self, N):
		c = 0
		if N == 0:
			return -1
		if N & N - 1 == 0:
			if N == 1:
				return 1
			else:
				while N > 1:
					N = N / 2
					c = c + 1
				return c + 1
		else:
			return -1


class Solution:

	def findPosition(self, N):
		indx = 0
		cnt = 0
		if N == 0:
			return -1
		while N:
			if N & 1 == 1:
				cnt += 1
			N = N >> 1
			indx += 1
		if cnt != 1:
			return -1
		return indx


from collections import Counter

class Solution:

	def findPosition(self, N):
		a = str(bin(N)[2:])
		b = Counter(a)
		if b['1'] != 1:
			return -1
		for i in range(len(a)):
			if a[i] == '1':
				return len(a) - i
			else:
				continue
		return -1


class Solution:

	def findPosition(self, N):
		if N == 0:
			return -1
		if N & N - 1 == 0:
			i = 0
			while True:
				if N & 1 << i:
					return i + 1
				i += 1
		else:
			return -1


class Solution:

	def findPosition(self, N):
		a = bin(N)
		x = a[::-1]
		if x.count('1') == 1:
			return x.index('1') + 1
		else:
			return -1


class Solution:

	def findPosition(self, N):
		s = bin(N)[2:]
		if s.count('1') > 1 or s.count('1') == 0:
			return -1
		if s.count('1') == 1:
			j = 0
			for i in reversed(s):
				j += 1
				if i == '1':
					return j


class Solution:

	def findPosition(self, N):
		import math
		if N != 0 and N & N - 1 ^ N == N:
			return int(math.log2(N) + 1)
		else:
			return -1


class Solution:

	def findPosition(self, N):
		if bin(N)[2:].count('1') == 0 or bin(N)[2:].count('1') > 1:
			return -1
		x = bin(N)[2:]
		return len(x) - x.index('1')


class Solution:

	def findPosition(self, N):
		track_list = list()
		ones_count = 0
		if N == 1:
			return 1
		elif N == 0:
			return -1
		else:
			while True:
				if N % 2 == 1 and N != 1:
					return -1
				elif N % 2 == 0:
					track_list.append(0)
					N = N / 2
				elif N == 1:
					track_list.append(1)
					return track_list.index(1) + 1
				else:
					track_list.append(1)
					ones_count += 1
					if ones_count > 1:
						return -1


class Solution:

	def findPosition(self, n):
		count = 0
		val = -1
		if n == 0:
			return -1
		while n:
			if n & 1 == 1:
				count += 1
				if count > 1:
					return -1
			n = n >> 1
			val += 1
		return val + 1


class Solution:

	def findPosition(self, N):
		c1 = 0
		c2 = 0
		while N > 0:
			if N % 2 == 1:
				c2 += 1
			c1 += 1
			N = N // 2
		if c2 > 1 or c1 == 0:
			return -1
		return c1


import math

class Solution:

	def findPosition(self, n):
		if n > 0 and n & n - 1 > 0:
			return -1
		else:
			if n == 0:
				return -1
			pos = int(math.log(n, 2)) + 1
		return pos


class Solution:

	def findPosition(self, N):
		bin_N = str(bin(N))
		if '1' not in bin_N or bin_N.count('1') > 1:
			return -1
		bin_N_rev = bin_N[::-1]
		index = bin_N_rev.index('1')
		return index + 1


class Solution:

	def findPosition(self, N):
		binary = bin(N)[2:]
		res = -1
		set_bit = 0
		for ele in range(len(binary)):
			if binary[ele] == '1':
				res = len(binary) - ele
				set_bit += 1
		if set_bit == 1:
			return res
		return -1


class Solution:

	def findPosition(self, N):
		x = str(bin(N))[2:]
		if x.count('1') != 1:
			return -1
		else:
			x = x[::-1]
			return x.index('1') + 1


class Solution:

	def findPosition(self, N):
		(position, ans) = (1, -1)
		while N:
			if N & 1:
				if ans == -1:
					ans = position
				else:
					return -1
			position += 1
			N >>= 1
		return ans


class Solution:

	def findPosition(self, N):
		if N == 0:
			return -1
		a = []
		while N != 0:
			s = N % 2
			a.append(s)
			N //= 2
		a = a[::-1]
		for i in a:
			if i == 1:
				if a.count(i) > 1:
					return -1
		else:
			return len(a)


class Solution:

	def findPosition(self, N):
		if N == 0 or N & N - 1 != 0:
			return -1
		else:
			pos = 1
			while N:
				if N & 1:
					return pos
				N = N >> 1
				pos += 1


import math

class Solution:

	def findPosition(self, N):
		if bin(N).count('1') == 1:
			return int(math.log(N, 2)) + 1
		else:
			return -1


import math

class Solution:

	def findPosition(self, N):
		if N == 0:
			return -1
		temp = N
		while temp != 1:
			if temp % 2 != 0:
				return -1
			temp = temp // 2
		return int(math.log(N, 2)) + 1


class Solution:

	def findPosition(self, N):
		c = 0
		m = bin(N)[2:]
		for i in range(len(m)):
			if m[i] == '1':
				c += 1
				p = i
		if c == 1:
			return len(m) - p
		else:
			return -1


class Solution:

	def findPosition(self, N):
		p = bin(N)
		q = p[2:]
		r = q[::-1]
		if '1' in r and r.count('1') == 1:
			return r.index('1') + 1
		else:
			return -1


class Solution:

	def findPosition(self, N):

		def powerOf2(n):
			if n > 0 and n & n - 1 == 0:
				return True
			else:
				return False
		if powerOf2(N):
			(i, pos) = (1, 1)
			while i & N == 0:
				i <<= 1
				pos += 1
			return pos
		else:
			return -1


class Solution:

	def findPosition(self, N):
		if N == 0:
			return -1
		if N & N - 1 == 0:
			temp = bin(N)[2:]
			return len(temp)
		else:
			return -1