Datadog Interview Question

Implement a recursive algorithm to satisfy the required outcome.