Help in BINFUN

Can someone explain there approach for BINFUN .

I did brute force and my solution got partially accepted. But I’m not able to figure out how to approach it for full AC.
my partially accepted code