So I’ve been practicing few coding question on InterviewBit and I came across ‘Bead Challenge’. Here’s the questions:
Problem
And here is an example:
Example
I did understand the question, but couldn’t come up with a solution. So I looked into the solution the platform had to offer:
from collections import defaultdict
def secreatFunction(x, divs):
i = 2
while i * i <= x:
while x % i == 0:
divs[i] += 1
x //= i
i += 1
if x > 1:
divs[x] += 1
class Solution:
def solve(self, A):
a=defaultdict(int)
n=len(A)
for i in A:
secreatFunction(i, a)
for count in a.values():
if count % n != 0:
return 0
return 1
I tried comprehending the code logic using pen and paper, and yes I do understand how the solution call for a case where a given prime factor can be equally distributed among all the beads (or all the elements in the array). But I can’t map the code logic to the question logic. Can somebody help me out?
I’ve tried putting in various valid input into the given code, validated using pen and paper and it seems to work just fine. So its seems not to be an issue of the code logic. I just can’t map the logic to the question and I am pondering if there is an another approach that makes sense.
Derick Davies is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.