C++20 sections · 1024 units
Open in Course

Nearly Lucky - Implementation

Working solution

I extract digits to count 4s and 7s, then verify if that count is lucky:

function isLucky(n)
if n = 0 then
 return false
 while n > 0 digit := n mod 10
 if digit  4 and digit  7 then
 return false n := n / 10
 return true function checkNearlyLucky(n)
 count := 0
 while n > 0
 digit := n mod 10
 if digit = 4 or digit = 7 then
 count := count + 1
 n := n / 10
 if isLucky(count) then
 return "YES"
 else
 return "NO"
``` I use it to verify the count of lucky digits. Digit iteration extracts each digit and checks against target values (4 and 7). The count accumulates lucky digits. Then you check if the count itself is lucky.