FOR FREE YEAR SOLVED

Number Pattern 16

Back to Programming

Description

Number Pattern:

The number of lines are 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=1 to d repeat
			Set p<-i
			For j=1 to i repeat
				Print p
				Set p<-p+1
			[End of ‘for’ loop]
			Set p<-p-2
			For k=j-2 to 1 repeat
				Print p
				Set p<-p-1
			[End of ‘for’ loop]
			Move to the next line
		[End of ‘for’ loop]
		For i=i-2 to 1 repeat
			Set p<-i
			For j=1 to i repeat
				Print p
				Set p<-p+1
			[End of ‘for’ loop]
			Set p<-p-2
			For k=j-2 to 1 repeat
				Print p
				Set p<p-1
			[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=1;i<=d;i++)--------------------------------------------------------------- n/2+1

                                {              p=i;

                                                for(j=1;j<=i;j++)--------------------------- i

                                                {              printf("%d ",p);

                                                                p++;       

                                                }

                                                p-=2;

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

                                                {              printf("%d ",p);

                                                                p--;         

                                                }

                                                printf("\n");

                                }

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

                                {              p=i;

                                                for(j=1;j<=i;j++)------------------------------------ i

                                                {              printf("%d ",p);

                                                                p++;       

                                                }

                                                p-=2;

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

                                                {              printf("%d ",p);

                                                                p--;         

                                                }

                                                printf("\n");

                                }

 

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