# [K (oK)], 21 bytes

    {1+*&~x>+\1%1+!*/x#3}

[Try it online!][TIO-k6g9b0iu]

[K (oK)]: https://github.com/JohnEarnest/ok
[TIO-k6g9b0iu]: https://tio.run/##y9bNz/7/P82q2lBbS62uwk47xlDVUFtRS79C2bj2f5q6oZ6pgpGCsYKxnqGJoamCiYKpgjlQyNDgPwA "K (oK) – Try It Online"

Inspired by [Jo King's APL solution](https://codegolf.stackexchange.com/a/199193/75681)