Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Print Pattern

Easy Acceptance 56.39% Points 20.00

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.

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Auxiliary Space: O(n)
Constraints
  • -105 <= n <= 105
Companies
Microsoft
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions