Given an array arr[] of size N, find the first digit from the left of the product of these N integers.
Example 1:
Input: N = 4, arr[] = {5, 8, 3, 7}
Output: 8
Explanation: Produt is 840
Example 2:
Input: N = 3, arr[] = {6, 7, 9} 
Output: 3
Explanation: Produt is 378
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function firstDigit() which takes N and array arr[] as input parameters and returns the left digit of product.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, arr[i] ≤ 10^{5}
Test cases have been designed such that there is no precision ambiguity.

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def firstDigit(self, arr, n):

        # code here 

Solutions:
class Solution:

	def firstDigit(self, arr, n):
		p = 1
		for i in range(n):
			p = p * arr[i]
			d = str(p)
			p = int(d[:6])
		return str(p)[0]


class Solution:
	mod = 10

	def firstDigit(self, arr, n):
		val = 1
		for i in arr:
			val *= i
			st = str(val)
			val = int(st[:6])
		return str(val)[0]


import math

class Solution:

	def firstDigit(self, arr, n):
		s = 0.0
		for a in arr:
			s += math.log10(a)
		s = s - math.floor(s)
		return int(math.pow(10, s) + 1e-06)


import math

class Solution:

	def firstDigit(self, arr, n):
		res = 1
		for i in arr:
			res = res * i
		dig = int(math.log10(res))
		return res // 10 ** dig


class Solution:

	def firstDigit(self, arr, n):
		s = 1
		for i in range(n):
			s = s * arr[i]
			m = str(s)
			s = int(m[:6])
		return str(s)[0]


import math

class Solution:

	def firstDigit(self, arr, n):
		S = 0
		if n == 1:
			s = str(arr[0])
			return s[0]
		else:
			for i in arr:
				S = S + math.log10(i * 1.0)
			fract_S = S - math.floor(S)
			ans = math.pow(10, fract_S)
			return int(ans)


import functools as f

class Solution:

	def firstDigit(self, arr, n):
		p = 1
		for i in range(n):
			p = p * arr[i]
			d = str(p)
			p = int(d[:8])
		return str(p)[0]


import math

class Solution:

	def firstDigit(self, arr, n):
		product = 1
		for i in range(0, n):
			product = product * arr[i]
		digit = product // 10 ** int(math.log10(product))
		return digit


import math

class Solution:

	def firstDigit(self, arr, n):
		S = 0
		for i in range(n):
			S += math.log(arr[i], 10)
		S += 1e-06
		S -= math.floor(S)
		ans = int(pow(10, S))
		return ans


import math

class Solution:

	def firstDigit(self, arr, n):
		if n == 1:
			return str(arr[0])[0]
		s = 0
		for val in arr:
			s = s + math.log10(val * 1.0)
		frac = s - math.floor(s)
		return int(math.pow(10, frac))