FOR FREE YEAR SOLVED

K Shape Number Pattern 1

Back to Programming

Description

K Shape Number Pattern:

Here the number of lines is taken as input. The aforesaid pattern is divided into two halves while implemented. In The first half, the number of elements are decreased and in the second half, the number of elements increased. For each half two for loops are used to print the elements. In the first half, The number of elements are decreased by 1 and in the second half, the number of elements are increased by 1. The pattern is only possible if the number of lines is even.

Algorithm

INPUT: Number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [taking the inputs]
	Read n [number of lines]
Step 2: [printing the pattern]
	Set st<-n/2
	If n modulus 2=0 then
For i=1 to n/2 repeat
			For j=st to 1 repeat
				Print j
			[End of ‘for’ loop]
			Move to the next line
			Set st<-st-1
		[End of ‘for’ loop]
		Set st<-st+1
		For i=n/2+1 to n repeat
			For j=st to 1 repeat
				Print j
			[End of ‘for’ loop]
			Move to the next line
			Set st<-st+1			
		[End of ‘for’ loop]
	else
		print "Please give correct input(no. of lines should be even)
	[End of ‘if’]
Step 3: Stop 

Code

Time Complexity:

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

                                {              for(j=st;j>=1;j--)-------------------------- st

                                                                printf("%d",j);

                                                printf("\n");

                                                st-=1;

                                }

                                //printing the second half of the pattern where the number of stars

                                //are increasing

                                st=st+1;

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

                                {              for(j=st;j>=1;j--)------------------------------ st

                                                                printf("%d",j);

                                                printf("\n");

                                                st+=1;                                   

                                }

The complexity is: O((n/2*st)+(n/2*st))=O(2*(n/2*st))=O(n2).