Cracking the coding interview: Print the pattern — 1

You a given a number N. You need to print the pattern for the given value of N.

For N = 2 the pattern will be
2 2 1 1
2 1

For N = 3 the pattern will be
3 3 3 2 2 2 1 1 1
3 3 2 2 1 1
3 2 1

Example 1:

22 2 1 1 $2 1 $

Example 2:

33 3 3 2 2 2 1 1 1 $3 3 2 2 1 1 $3 2 1 $

Note: The Input/Output format and Example is given are used for the system’s internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Your Task:
Since this is a function problem, you don’t need to worry about the test cases. Your task is to complete the function printPat which takes one argument ’N’ denoting the length of the pattern.
Note: Instead of printing a new line print a “$” without quotes.

Constraints:
1 <= N <= 40

Solution:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store