Given a value n, we need to print the following pattern accordingly, using only constant extra space.
Input : n = 1 Output : x Input : n = 2 Output : x x x x Input: n = 5 Output: x x o x o x x o x o x x o x o x x Input: n = 6 Output: x x o x o x x o x x o x x o x x o x x o x o x x Input : n = 7; Output : x x o x o x x o x x o x x o x o x o x x o x x o x x o x o x x Input : n = 8; Output : x x o x o x x o x x o x o x o x x o x o o x o x x o x o x o x x o x x o x o x x
We can divide this problem into 3 parts:
1) Print upper half with n-1 lines for odd n or n-2 lines for even n.
2) Print middle lines, 1 line for odd n or 3 lines for even n.
3) Print lower half, with n-1 lines for odd n or n-2 lines for even n.
For such complex patterns it may be easier if we can use 1-based indexing
and separate functions to print characters beginning with x or o.
x x o x o x x o x x o x x o x o x o x x o x x o x x o x o x x x x o x o x x o x x o x o x o x x o x o o x o x x o x o x o x x o x x o x o x x
The time complexity of the above program is .
This article is contributed by Satish Srinivas. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Program to print double headed arrow pattern
- Python 3 | Program to print double sided stair-case pattern
- Program to print a Hollow Triangle inside a Triangle
- Program to print hollow Triangle pattern
- Print Triangle separated pattern
- Program to print modified Binary triangle pattern
- Sum of upper triangle and lower triangle
- Printing Triangle Pattern in Java
- Program for incrementing/decrementing triangle pattern
- Printing the Triangle Pattern using last term N
- Printing triangle star pattern using a single loop
- C Program to print Floyd's triangle
- Program to print Sum Triangle for a given array
- Program to print binary right angle triangle
- Program to print Reverse Floyd's triangle
- Programs to print Triangle and Diamond patterns using recursion
- Print a pattern without using any loop
- Print matrix in diagonal pattern
- Print pattern using only one loop | Set 1 (Using setw)
- Program to print a pattern of numbers