Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Pattern
2.
Approach
2.1.
Code using Brute Method
2.2.
Complexity Analysis
2.3.
Code using Single Nested Loop
2.4.
Complexity Analysis
3.
Frequently Asked Questions
3.1.
How do you print a half diamond pattern?
3.2.
What is a half-diamond number pattern?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Half Diamond Number Pattern

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.

Pattern

For Number of Rows = 5, the pattern is as follows

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
5 4 3 2 1 0
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5

Approach

From the above pattern, it is visible that we print n+1 rows in increasing reverse order and then n rows in decreasing reverse order.
Increasing Reverse Order:

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
5 4 3 2 1 0

Decreasing Reverse Order

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5

Brute Method: It can be achieved using 2 nested loops. One nested loop is used to print n+1 Increasing Reverse Pattern and another nested loop to print n Decreasing Reverse Pattern.

Optimized Method: Only one Nested loop is used with Improved Mathematical Logic.

Code using Brute Method

#include<stdio.h>
 void main(){
int i,j; 
int n=3;
for(i=0;i<=n;i++){
     for(j=n;j>=n-i;j--){
         printf("%d ",j);
     }
     printf("\n");
}  
for(i=1;i<=n;i++){
     for(j=n;j>=i;j--){
         printf("%d ",j);
     }
     printf("\n");
}
}

Output

3
3 2
3 2 1
3 2 1 0
3 2 1
3 2
3

Complexity Analysis

Time Complexity: O(N2)

Space Complexity: O(1)

Code using Single Nested Loop

#include<stdio.h>
void main(){
int n=4,i,j,x;
for(i=n;i>=-n;i--){
     if(i>=0)
     x=i;
     else
     x=-1*i;
     for(j=n;j>=x;j--){
         printf("%d ",j);
     }
     printf("\n");
}
}

Output

4
4 3
4 3 2
4 3 2 1
4 3 2 1 0
4 3 2 1
4 3 2
4 3
4

Complexity Analysis

Time Complexity: O(N)

Space Complexity: O(1)

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

Frequently Asked Questions

How do you print a half diamond pattern?

First, we print n+1 rows in increasing reverse order and then n rows in decreasing reverse order. It can be achieved using 2 nested loops. One nested loop is used to print n+1 Increasing Reverse Pattern and another nested loop to print n Decreasing Reverse Pattern.

What is a half-diamond number pattern?

A half-diamond number pattern is printing numbers up to n in n+1 rows in increasing reverse order in the shape of a half diamond. For example, a half diamond number pattern for input 3 will be: 
3
3 2
3 2 1
3 2 1 0
3 2 1
3 2
3

You can also read about - Strong number in c

Conclusion

In this blog, we learnt how to code a Half Diamond Number pattern using C language. 

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 Interview Puzzles, 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
V-Shape Number Pattern
Next article
Inverted Half Diamond Pattern
Live masterclass