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
- Program to print hollow Triangle pattern
- Python 3 | Program to print double sided stair-case pattern
- Printing Triangle Pattern in Java
- Printing the Triangle Pattern using last term N
- Program for incrementing/decrementing triangle pattern
- 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 the given pattern recursively
- Print a pattern without using any loop
- Pattern to print X in a rectangular box
- Program to print the given Z Pattern