Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Diamond Pattern

Basic Acceptance 94.13% Points 10.00

You are given an integer N. Your task is to print a diamond pattern of stars consisting of 2N − 1 rows. The pattern must contain an upward centered pyramid of N rows, followed by an inverted centered pyramid of N − 1 rows. The number of stars increases by two in each row until the middle row, then decreases by two symmetrically.

Examples
Example 1

Input: N = 4

Output:

   *
Example 2

Explanation: The diamond contains 7 rows. The number of stars increases from 1 to 7, then decreases symmetrically to 1, with appropriate leading spaces to keep the pattern centered.

Example 3

Input: N = 1

Output:

*

Explanation: A single row containing one star forms the smallest possible diamond.

Example 4

Input: N = 2

Output:

 *
Example 5

Explanation: The diamond contains three rows, with 1 star in the first and last rows and 3 stars in the middle row.

Constraints
  • 1 <= N <= 100
Companies
Apple ServiceNow Paytm
Topics
Data Structures Basics String
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