FOR FREE CONTENT

Half Right Diamond Number Pattern 3

Back to Programming

Description

Half Right Diamond Number Pattern:

The number of lines is taken as input. The number of lines of this pattern should be odd. The pattern is divided into two halves, in the first half the number of elements is increased and in the second half, the number of elements is decreased

Algorithm

INPUT: The number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: Read n [number of lines]
Step 2: [printing the pattern]
	If n mod 2≠0 then
		Set d<-n/2+1
		For i=d to 1 repeat
			For j=i to d repeat
				Print j
			[End of ‘for’ loop]
			For k=j-2 to i repeat
				Print k
			[End of ‘for’ loop]
			Move to the next line
		[End of ‘for’ loop]
		For i=i+2 to d repeat
			For j=i to d repeat
				Print j
			[End of ‘for’ loop]
			For k=j-2 to i repeat
				Print k
			[End of ‘for’ loop]
			Move to the next line
		[End of ‘for’ loop]
	else
		print "the number of lines should be odd"
	[End of ‘for’ loop]
Step 3: Stop.

Code

Time Complexity:

for(i=d;i>=1;i--)--------------------------------------------------------------------- n/2+1

                                {              for(j=i;j<=d;j++)------------------------------- n/2-i+2

                                                                printf("%d ",j);

                                                for(k=j-2;k>=i;k--)----------------------------- j-1-i

                                                                printf("%d ",k);

                                                printf("\n");

                                }

                                for(i=i+2;i<=d;i++)------------------------------------------- n/2

                                {              for(j=i;j<=d;j++)----------------------------------- n/2-i+2

                                                                printf("%d ",j);

                                                for(k=j-2;k>=i;k--)--------------------------------- j-i-1

                                                                printf("%d ",k);

                                                printf("\n");

                                }

The complexity is: O(((n/2+1)*(n/2-2i+1+j))+((n/2)*(n/2-2i+1+j)))=O(n2)