Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Puzzle Statement
3.
Program Code in Java
3.1.
Complexity Analysis
4.
Frequently Asked Questions
4.1.
Is the input row size necessary to be an odd number?
4.2.
Why do we always require two “ for “ loops to implement a pattern?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Hollow Diamond Number Pattern

Author Ayushi Poddar
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Interview Puzzles

Introduction

Puzzles are good exercise for the brain. They help in enhancing the cognitive abilities of the brain helping with Problem Solving and related skills. There are numerous types of puzzles; each one having a logic inherent to itself which helps in cracking it. A good puzzle well is actually like a good mystery that we may have read about or watched on TV. It has the finest of hints which help in reaching its solution.

The following article discusses one such puzzle so let's get right to it.

Puzzle Statement

A hollow diamond number pattern prints a pattern of numbers that resembles the structure of a diamond that is hollow from within. Consider the below-given picture. The picture below is a hollow star diamond pattern because stars form the hollow diamond structure.

Illustration Image

If we replace the asterisks(the star symbols) printed in the above pattern with numbers, we can call it a hollow diamond number pattern. For example, if we consider the image given below.

Illustration Image

 

Explanation

The correct approach to printing the desired pattern would be to divide the entire pattern into two halves- upper and lower. The lower half is the exact replica of the upper half, just turned upside down. So firstly to divide into two halves, we first calculate a point till which the first half of the pattern will be printed, by having a control variable which is the median point.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Program Code in Java

import java.util.*;
class Diamond
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int row_size=sc.nextInt();
        int control=row_size/2+1;  //Varible to control where to stop and where to start
        int x=1;
        for(int i=1;i<=row_size;i++) //Outer loop for the rows
        {
            for(int j=1;j<=row_size;j++) //Inner loop for the columns
            {
                if((j==control)||(j==row_size-control+1))
                {
                    System.out.print(x);
                }
                else
                {
                     System.out.print(" ");
                }
            }
            if(i<=row_size/2)
            {
                x++;
                control--;
            }
            else
            {
                x--;
                control++;
            }
            System.out.println();
        }
    }
} 

Output

If input row size = 5, then the output would be in the following manner 

1     
   22
  3  3
 4   4
5     5
 4   4
  3  3
   22
    1

Complexity Analysis

Time Complexity: O(n^2) as nested for loops.

Space Complexity: O(1) as no additional array is being taken.
 

Frequently Asked Questions

Is the input row size necessary to be an odd number?

Yes, the input row size must be an odd number because a diamond-like structure can only be formed using the odd number of rows.

Why do we always require two “ for “ loops to implement a pattern?

For implementing a pattern, we always have an outer loop that controls the row operations in the pattern and an inner loop that controls the column operations.

Conclusion

This article extensively discussed the problem of printing the hollow diamond number pattern, as the name suggests, is formed by forming a hollow diamond structure using numbers. 

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Previous article
Inverted Half Diamond Pattern
Next article
Fibonacci Half Pyramid
Live masterclass