Hebrews 10:1 the OT law with its sacrifices could not make one perfect. If it could then there would be no need for those sacrifices to cease, no need for Christ to shed His blood.
Hebrews 10:4 " For it is not possible that the blood of bulls and of goats should take away sins. " Those OT animal sacrifices could not take away sins leaving one totally justified before God.
Romans 4:3 Abraham believed God and it was reckoned, accounted, credited to him.
Abraham's righteousness/justification was reckoned, credited to him. Not until Christ died and His blood flowed back to wash away all the sins of those OT characters were they then fully justified.
Romans 4:6-8 explains how God can 'reckon' a man righteous even though that man did not keep the law perfectly. David obviously sinned, did not keep the OT law perfectly therefore how could he be 'reckoned' as righteous apart from works (apart from sinless, flawless law keeping)?
David, though not sinlessly perfect, had a faithful obedience therefore his iniquities were forgiven and sins were covered. God either reckoned sin or righteousness to a man and God dis not do this reckoning unconditionally or randomly. Those that obey God, which includes repenting of sins, are the ones GOd will not reckon sin to.
God forgave the obedient in view of the coming of Christ and shedding of His blood apart from flawless law keeping.
Hebrews 10:4 clearly says those OT sacrifices could not take away sin and there was a remembrance of their sins. That is why they continued to have to offer those sacrifices year by year.
It would take one sacrifice of the blood of Christ to fully wash away all sins and God remember those sins no more under the NT...total justification.