Print a sequence of numbers starting with N where A[0] = N, without using loop, in which  A[i+1] = A[i] - 5,  until A[i] > 0. After that A[i+1] = A[i] + 5  repeat it until A[i] = N.
Example 1:
Input: N = 16
Output: 16 11 6 1 -4 1 6 11 16
Explaination: The value decreases until it 
is greater than 0. After that it increases 
and stops when it becomes 16 again.
Example 2:
Input: N = 10
Output: 10 5 0 5 10
Explaination: It follows the same logic as 
per the above example.
Your Task:
You do not need to read input or print anything. Your task is to complete the function pattern() which takes N as input parameters and returns a list containing the pattern.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: Complete search

Starter code: #User function Template for python3



class Solution:

    def pattern(self, N):

        # code here

Solutions:
class Solution:

	def pattern(self, N):
		l = []
		k = 0
		N1 = N
		while True:
			if k == 0:
				l.append(N)
				N = N - 5
				if N <= 0:
					k = 1
			else:
				l.append(N)
				N = N + 5
				if N1 == N:
					l.append(N1)
					return l


import math
import sys
sys.setrecursionlimit(10 ** 6)

class Solution:
	arr = []

	def recshow(self, N, i=0):
		if N <= 0:
			self.arr[i] = N
			return
		self.arr[i] = N
		j = len(self.arr) - i - 1
		self.arr[j] = N
		self.recshow(N - 5, i + 1)
		return

	def pattern(self, N):
		self.arr = [0] * (math.ceil(N / 5) * 2 + 1)
		self.recshow(N)
		return self.arr


class Solution:

	def pattern(self, N):
		a = []
		a.append(N)
		k = N
		while N > 0:
			N -= 5
			a.append(N)
		a = a[:-1] + a[::-1]
		return a


class Solution:

	def pattern(self, N):
		v = []
		v.append(N)
		d = N - 5
		c = 0
		while d != N:
			if d > 0 and c == 0:
				v.append(d)
				d = d - 5
			else:
				v.append(d)
				d += 5
				c = 1
		v.append(N)
		return v


import sys
sys.setrecursionlimit(10 ** 6)

class Solution:

	def pattern(self, N):
		A = []
		m = N

		def print_pattern1(A, m):
			if m <= 0:
				A.append(m)
				return A
			else:
				A.append(m)
				return print_pattern1(A, m - 5)
			return A
		B = []

		def print_pattern2(B, n):
			if n > 0:
				print_pattern2(B, n - 5)
				B.append(n)
				return B
			else:
				return B
		return print_pattern1(A, N) + print_pattern2(B, N)


class Solution:

	def pattern(self, N):
		ans = [N]
		temp = N
		while N >= 0:
			N -= 5
			ans.append(N)
			if N == 0:
				break
		while N < temp:
			N += 5
			ans.append(N)
		return ans


import sys
sys.setrecursionlimit(10 ** 6)

class Solution:

	def checkit(self, list1, number):
		list1.append(number)
		if number <= 0:
			return list1
		list1 = self.checkit(list1, number - 5)
		list1.append(number)
		return list1

	def pattern(self, N):
		ans = []
		return self.checkit(ans, N)


import sys
sys.setrecursionlimit(10 ** 6)

def helper(ans, n):
	ans.append(n)
	if n <= 0:
		return ans
	helper(ans, n - 5)
	ans.append(n)
	return ans

class Solution:

	def pattern(self, N):
		ans = []
		return helper(ans, N)


class Solution:

	def pattern(self, N):
		import sys
		sys.setrecursionlimit(10000)
		lst = []

		def rec(N):
			lst.append(N)
			if N <= 0:
				return
			N -= 5
			rec(N)
		rec(N)
		return lst + lst[:len(lst) - 1][::-1]


import sys
sys.setrecursionlimit(10 ** 6)

class Solution:

	def __init__(self):
		self.li = []

	def pattern(self, N):
		self.li.append(N)
		if N <= 0:
			return
		self.pattern(N - 5)
		self.li.append(N)
		return self.li


import sys

class Solution:
	result = []

	def solve(self, N):
		self.result.append(N)
		if N <= 0:
			return
		self.solve(N - 5)
		self.result.append(N)

	def pattern(self, N):
		sys.setrecursionlimit(N if N > 100 else 100)
		self.result = []
		self.solve(N)
		return self.result


class Solution:

	def pattern(self, N):
		l = []
		while N > 0:
			l.append(N)
			N -= 5
		l += [N] + l[::-1]
		return l


class Solution:

	def pattern(self, N):
		he = []
		he.append(N)
		temp = 0
		temp = N
		while N > 0:
			N = N - 5
			he.append(N)
		while N != temp:
			N = N + 5
			he.append(N)
		return he


import sys
sys.setrecursionlimit(10 ** 6)

class Solution:

	def pattern(self, N):
		res = [N]
		target = N
		N -= 5

		def recurs(N, target, res, val):
			if N == target:
				res.append(N)
				return res
			elif N <= 0 or not val:
				val = False
				res.append(N)
				return recurs(N + 5, target, res, val)
			else:
				res.append(N)
				return recurs(N - 5, target, res, val)
		a = recurs(N, target, res, True)
		return a


class Solution:

	def pattern(self, n):
		a = []
		curr = n
		while curr > 0:
			a.append(curr)
			curr = curr - 5
		while curr <= n:
			a.append(curr)
			curr = curr + 5
		return a


class Solution:

	def pattern(self, N):
		i = 0
		lst = [0]
		i = 0
		lst[i] = N
		while True:
			if lst[i] > 0:
				lst.append(lst[i] - 5)
				i += 1
			if lst[i] <= 0:
				break
		while True:
			lst.append(lst[i] + 5)
			i += 1
			if lst[i] == N:
				break
		return lst


class Solution:
	lis = []

	def pattern(self, N):
		m = N
		arr = []
		while N > 0:
			arr.append(N)
			N -= 5
		while N <= m:
			arr.append(N)
			N += 5
		return arr


class Solution:

	def pattern(self, N):
		res = []
		i = N
		while i > 0:
			res.append(i)
			i -= 5
		while i <= N:
			res.append(i)
			i += 5
		return res


import sys
sys.setrecursionlimit(10000)

class Solution:

	def solve(self, n, li):
		li.append(n)
		if n <= 0:
			return
		self.solve(n - 5, li)

	def pattern(self, N):
		li = []
		self.solve(N, li)
		return li + li[::-1][1:]


class Solution:

	def pattern(self, N):
		arr = []
		arr.append(N)
		a = N - 5
		c = 0
		while a != N:
			if c == 0:
				if a > 0:
					arr.append(a)
					a = a - 5
				else:
					c = 1
					arr.append(a)
			else:
				c = 1
				a = a + 5
				arr.append(a)
		return arr


import sys
sys.setrecursionlimit(10000)

class Solution:

	def pattern(self, N):
		return self.rec(N, N, [N], False)

	def rec(self, N, curr, arr, inc):
		if inc and N == curr:
			return arr
		if not inc and curr <= 0:
			return self.rec(N, curr, arr, True)
		if inc:
			arr.append(curr + 5)
			return self.rec(N, curr + 5, arr, True)
		else:
			arr.append(curr - 5)
			return self.rec(N, curr - 5, arr, False)


import sys
sys.setrecursionlimit(10 ** 4)

class Solution:

	def pattern(self, N):
		arr = []
		flage = False
		self.pa(arr, N, flage, N)
		return arr

	def pa(self, arr, no, flage, N):
		if no <= 0:
			flage = True
		if flage and N == no:
			arr.append(no)
			return
		else:
			arr.append(no)
			if flage:
				self.pa(arr, no + 5, flage, N)
			else:
				self.pa(arr, no - 5, flage, N)


class Solution:

	def pattern(self, N):
		arr = []
		j = 0
		for i in range(N, -5, -5):
			arr.append(i)
		for j in range(i + 5, N + 1, +5):
			arr.append(j)
		return arr


class Solution:

	def pattern(self, N):
		A = [N]
		while A[-1] > 0:
			num = A[-1] - 5
			A.append(num)
		b = A[len(A) - 2::-1]
		A.extend(b)
		return A


class Solution:

	def pattern(self, N):
		res_arr = []
		last = 0
		for i in range(N, -5, -5):
			res_arr.append(i)
			last = i
		res_arr.pop()
		for i in range(last, N + 1, 5):
			res_arr.append(i)
		return res_arr


class Solution:

	def pattern(self, N):
		x = -5
		a = []
		a.append(N)
		while N > 0:
			a.append(N - 5)
			N = N - 5
		a += a[len(a) - 2::-1]
		return a


import sys
sys.setrecursionlimit(10 ** 4)

class Solution:

	def pattern(self, N):
		ans = []
		self.help(N, ans)
		ans.append(N)
		return ans

	def help(self, N, ans):
		ans.append(N)
		N = N - 5
		if N > 0:
			self.help(N, ans)
		ans.append(N)
		return ans


import sys
sys.setrecursionlimit(10 ** 6)

class Solution:

	def pattern(self, N):
		result = []

		def descending(n):
			result.append(n)
			if n <= 0:
				return
			descending(n - 5)

		def ascending(n):
			if n <= 0:
				return
			ascending(n - 5)
			result.append(n)
		descending(N)
		ascending(N)
		return result


class Solution:

	def pattern(self, N):
		arr = []
		p = N
		while p > 0:
			arr.append(p)
			p = p - 5
		while p != N:
			arr.append(p)
			p += 5
		arr.append(N)
		return arr


class Solution:

	def pattern(self, N):
		i = 0
		b = []
		b.append(N)
		while b[i] > 0:
			b.append(b[i] - 5)
			i = i + 1
		d = len(b)
		b.append(b[d - 1] + 5)
		i = d
		while b[i] < N:
			b.append(b[i] + 5)
			i += 1
		return b


class Solution:

	def pattern(self, N):
		ans = []
		n = N
		while n > 0:
			ans.append(n)
			n -= 5
		while n <= N:
			ans.append(n)
			n += 5
		return ans


class Solution:

	def pattern(self, N):
		ans = []
		while N > 0:
			ans.append(N)
			N = N - 5
		ans.append(ans[-1] - 5)
		ans.extend(ans[:-1][::-1])
		return ans


class Solution:

	def pattern(self, N):
		n = N
		res = [N]
		N -= 5
		ad = -5
		while N != n:
			res.append(N)
			if N <= 0:
				ad = -ad
			N += ad
		res.append(n)
		return res


import sys
sys.setrecursionlimit(10 ** 6)

class Solution:

	def pattern(self, N):

		def helper(N, data):
			if N <= 0:
				data.append(N)
				return data
			data.append(N)
			helper(N - 5, data)
			data.append(N)
			return data
		result = []
		return helper(N, result)


class Solution:

	def pattern(self, n):
		m = n
		l = []
		while n > 0:
			l.append(n)
			n = n - 5
		while n <= m:
			l.append(n)
			n = n + 5
		return l


import sys
sys.setrecursionlimit(10000)

class Solution:

	def pattern(self, N):
		res = []
		f = [False]
		res.append(N)

		def cal(N):
			if not f[0]:
				res.append(res[-1] - 5)
				if res[-1] <= 0:
					f[0] = True
				cal(N)
			else:
				res.append(res[-1] + 5)
				if res[-1] == N:
					return
				cal(N)
		cal(N)
		return res


import sys
sys.setrecursionlimit(15000)

class Solution:

	def pattern(self, N):
		ans = [N]
		n = N - 5
		flag = True
		if n <= 0:
			flag = False
		ans += self.pattern1(n, N, flag)
		return ans

	def pattern1(self, n, N, flag):
		if n == N:
			return [N]
		if n <= 0:
			flag = False
		ans = [n]
		if flag:
			return ans + self.pattern1(n - 5, N, flag)
		else:
			return ans + self.pattern1(n + 5, N, flag)


import sys
sys.setrecursionlimit(10 ** 6)

class Solution:

	def pattern(self, N):
		l = [N]

		def g(a, b):
			l.append(a)
			if a <= 0:
				return l
			if a > 0 and b == False:
				a = a - 5
			else:
				a = a + 5
				b = True
			return g(a, b)
		ans = g(N - 5, False)
		realans = ans.copy()
		for i in range(len(ans) - 2, -1, -1):
			realans.append(ans[i])
		return realans


class Solution:

	def pattern(self, N):
		lst = []
		lst.append(N)
		a = lst[0]
		while N > 0:
			N -= 5
			lst.append(N)
		while N < a:
			N += 5
			lst.append(N)
		return lst


class Solution:

	def pattern(self, N):
		x = N
		s = []
		s.append(N)
		while x > 0:
			s.append(x - 5)
			x = x - 5
		a = x + 5
		while a != N:
			s.append(a)
			a = a + 5
		s.append(N)
		return s


import sys
sys.setrecursionlimit(10 ** 6)

class Solution:

	def pattern(self, n):
		arr = []

		def solve(n):
			arr.append(n)
			if n <= 0:
				return
			solve(n - 5)
			arr.append(n)
		solve(n)
		return arr


class Solution:

	def pattern(self, n):
		temp = n
		arr = []
		while temp > 0:
			arr.append(temp)
			temp -= 5
		while temp <= n:
			arr.append(temp)
			temp += 5
		return arr


class Solution:

	def recur(self, i, n, ans, word):
		if i <= 0:
			i = i + 5
			ans.append(i)
			return self.recur(i, n, ans, 'sum')
		elif i >= n and len(ans) and (word == 'sum'):
			return ans
		elif word == 'sum':
			i = i + 5
			ans.append(i)
			return self.recur(i, n, ans, 'sum')
		else:
			i = i - 5
			ans.append(i)
			return self.recur(i, n, ans, 'sub')

	def pattern(self, N):
		queue = [N]
		i = N
		while i > 0:
			i = i - 5
			queue.append(i)
		while i < N:
			i = i + 5
			queue.append(i)
		return queue


import sys
sys.setrecursionlimit(20000)

class Solution:

	def printnum(self, N, arr):
		if N <= 0:
			arr.append(N)
			return
		arr.append(N)
		self.printnum(N - 5, arr)
		arr.append(N)
		return arr

	def pattern(self, N):
		if N <= 0:
			ans = str(N)
			print(ans, end=' ')
			return
		ans = str(N)
		print(ans, end=' ')
		self.pattern(N - 5)
		print(str(N), end=' ')
		return []


import sys
sys.setrecursionlimit(20000)

class Solution:

	def printnum(self, N, arr):
		if N <= 0:
			arr.append(N)
			return
		arr.append(N)
		self.printnum(N - 5, arr)
		arr.append(N)
		return arr

	def pattern(self, N):
		arr = []
		self.printnum(N, arr)
		return arr


import sys

class Solution:

	def pattern(self, N):
		sys.setrecursionlimit(10 ** 4)
		return self.helper([N], 0)

	def helper(self, arr, flag):
		item = arr[-1] - 5 + flag * 10
		if item <= 0:
			flag = 1
		arr.append(item)
		if arr[0] == arr[-1]:
			return arr
		return self.helper(arr, flag)


class Solution:

	def pattern(self, N):
		b = []
		c = N
		b.append(N)
		while N:
			N = N - 5
			b.append(N)
			if N == 0 or N < 0:
				break
		N = N + 5
		while N <= c:
			b.append(N)
			N += 5
		return b


class Solution:

	def pattern(self, N):
		k = [N]
		d = N
		while d > 0:
			d = d - 5
			k.append(d)
		while d < N:
			d = d + 5
			k.append(d)
		return k


class Solution:

	def pattern(self, N):
		a = []
		i = 0
		a.append(N)
		while 1:
			if a[i] > 0:
				a.append(a[i] - 5)
				i = i + 1
			if a[i] <= 0:
				break
		while 1:
			a.append(a[i] + 5)
			i = i + 1
			if a[i] == a[0]:
				break
		return a


class Solution:

	def pattern(self, N):
		num = N
		l = list()
		while num > 0:
			l.append(num)
			num -= 5
		while num <= N:
			l.append(num)
			num += 5
		return l


class Solution:

	def pattern(self, N):
		a = N
		b = []
		k = 0
		while 1:
			b.append(N)
			if N <= 0:
				N = N + 5
				k = 1
			elif k == 0:
				N = N - 5
			else:
				N = N + 5
			if N == a:
				b.append(N)
				break
		return b


class Solution:

	def pattern(self, N):
		l = []
		temp = N
		l.append(N)
		while 1:
			if N <= 0:
				break
			else:
				N = N - 5
				l.append(N)
		while temp != N:
			N = N + 5
			l.append(N)
		return l


import sys
sys.setrecursionlimit(10000)

class Solution:

	def pattern1(self, N):
		if N == self.temp and self.flag == 1:
			self.l1.append(N)
			return
		self.l1.append(N)
		if N > 0 and self.flag == 0:
			self.pattern1(N - 5)
		else:
			self.flag = 1
			self.pattern1(N + 5)

	def pattern(self, N):
		self.l1 = []
		self.flag = 0
		self.temp = N
		self.pattern1(N)
		return self.l1


class Solution:

	def pattern(self, N):
		import sys
		sys.setrecursionlimit(20000)
		flag = True
		l = [N]
		M = N
		N = N - 5
		l.append(N)
		self.printpattern(N, l, M, flag)
		return l

	def printpattern(self, N, l, M, flag):
		if N == M:
			return l
		if N <= 0:
			flag = False
		if flag == True and N > 0:
			N = N - 5
			l.append(N)
			self.printpattern(N, l, M, flag)
		else:
			N = N + 5
			l.append(N)
			self.printpattern(N, l, M, flag)
		return l


class Solution:

	def pattern(self, N):
		l = []
		x = N
		while x > 0:
			l.append(x)
			x -= 5
		a = l[len(l) - 1] - 5
		a = [a]
		y = l[::-1]
		l.extend(a)
		l.extend(y)
		return l


class Solution:

	def pattern(self, N):
		res = [N]
		i = 0
		while True:
			s = res[i] - 5
			res.append(s)
			i += 1
			if s <= 0:
				break
		while True:
			s = res[i] + 5
			res.append(s)
			i += 1
			if s >= N:
				break
		return res


class Solution:

	def pattern(self, N):
		n = N
		l = []
		for i in range(n, -5, -5):
			l.append(i)
		last = l[-1]
		for i in range(last, n + 1, 5):
			l.append(i)
		ind = l.index(last)
		del l[ind]
		return l


class Solution:

	def pattern(self, n):
		cnt = n
		li = []
		while cnt > 0:
			li.append(cnt)
			cnt -= 5
		while cnt <= n:
			li.append(cnt)
			cnt += 5
		return li


class Solution:

	def pattern(self, a):
		t = []
		r = a
		t.append(a)
		while a > 0:
			a -= 5
			t.append(a)
		while True:
			q = t[-1]
			q += 5
			t.append(q)
			if q == r:
				break
		return t


class Solution:

	def pattern(self, N):
		c = []
		p = N
		c.append(N)
		while N > 0:
			N -= 5
			c.append(N)
		j = N
		while j < p:
			j += 5
			c.append(j)
		return c


class Solution:

	def pattern(self, n):
		l = []
		n1 = n
		while True:
			l += [n]
			n = n - 5
			if n <= 0:
				l += [n]
				break
		while True:
			a = l[-1]
			a += 5
			l += [a]
			if l[-1] == n1:
				break
		return l


class Solution:

	def pattern(self, n):
		l = [n]
		k = n
		while k > 0:
			k -= 5
			l.append(k)
		l[:] = l + l[::-1][1:]
		return l


class Solution:

	def pattern(self, N):
		a = [N]
		F = 1
		while True:
			if F == 1 and a[-1] > 0:
				a.append(a[-1] - 5)
			else:
				a.append(a[-1] + 5)
				F = 0
			if a[-1] == N:
				return a


class Solution:

	def pattern(self, N):
		s = N
		l = []
		while N > 0:
			l.append(N)
			N = N - 5
		else:
			while N <= s:
				l.append(N)
				N = N + 5
		return l