Need Hint - Pattern Problem

No constraints are given, need a hint! Please help -> Pattern Inverted Hour Glass

hello @yashjain0112

  • First build an intuition that u need to work for first row and at the end of the loop need to do work for the next row.
  • For each row first you need to print your pattern in decreasing order then spaces and then the same pattern but in increasing order.
  • To build the whole pattern just repeat this work according to total number of rows. With this approach, things seems to work out easily.

We take an input N and run 2N-1 iterations in total , that is , there are 2N+1 rows in total.
Till row = N , we print an increasing pattern and then the decreasing one.
For each row , we run 2N+1 iterations as well.
For each row , we first print a decreasing pattern ( the left side of the pattern ) starting from N and print till j <= i and then spaces.
For the right side of each pattern , we print an increasing pattern only if the value of 2N-j <= i .
Try this out.
This is only for the upper part of the pattern. If you are able to figure this out and then printing the lower half wouldn’t be much difficult.

Refer this code. Dry run it and try to understand

Solved it!
Thanks for the explanation! Here’s the link to my code, please tell if I can improve upon it.

@yashjain0112
no u cant improve furthur from O(n^2)

1 Like