Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Print Pattern

Easy Acceptance 56.39% Points 20

The task is to print a sequence of numbers starting from n, without using a loop. In the sequence, subtract 5 from n repeatedly (i.e., n, n-5, n-10, etc.) until n becomes less than or equal to 0. Once n reaches this condition, start adding 5 back to n (i.e., n+5, n+10, etc.) until n returns to its initial value. Implement a function pattern(n) that takes n as input and returns a list of the generated sequence.

Examples
Example 1

Example 1

Input: n = 13

Output: 13 8 3 -2 3 8 13

Explanation: The value decreases until it goes below 0. After that, it increases back to 13.

Example 2

Example 2

Input: n = 5

Output: 5 0 5

Explanation: The value decreases until it reaches 0. After that, it increases back to 5.

Constraints
  • -105 <= n <= 105
Hints
  • Expected Time Complexity: O(n)
  • Expected Auxiliary Space: O(n)
Topics
Recursion
Companies
Microsoft
Added 11 Apr 2025 56.39% acceptance
Ln 1, Col 1Sign in to save your code
5
5 0 5