Draw The Diamond

Hard
0/120
Average time to solve is 25m
profile
Contributed by
38 upvotes
Asked in company
MobiKwik

Problem statement

You have to print a regular grid pattern of 'R' rows and 'C' columns with a diamond-like shape of size 'S' in each cell of the grid. The diamond shape will contain ‘/’ and ‘\’ characters to represent the borders of the diamond and ‘o’ (small ‘o’ alphabet) to represent the space inside the diamond and ‘e’(small ‘e’ alphabet) to represent the rest of the space of diamond.

For example:

e/\e
/oo\
\oo/
e\/e

The above diagram represents a grid of 1 Row and 1 Column and a diamond of size 2 in its single cell(i.e. each side of the diamond has length 2).
Detailed explanation ( Input/output format, Notes, Images )
Input Format-
First-line contains ‘T’, denoting the number of Test cases.
For each Test case:
The first line contains three space-separated integers, ‘r’,’c’ and ’s’, where ‘r’ represents the number of rows, ‘c’ represents the number of columns and ‘s’ represents the size of each diamond. 
Output Format-
For each test case, you have to print the desired pattern(i.e.- a grid that has ‘r’ rows and ‘c’ columns of size ‘s’ diamond).
Constraints -
1 <= ‘T’ <= 10
1 <= ‘r’ & ‘c’ <= 100.
1 <= ‘s’ <= 10
Time Limit: 1 sec
Sample Input-1
2
3 1 2 
4 4 1
Sample Output-1
e/\e
/oo\
\oo/
e\/e
e/\e
/oo\
\oo/
e\/e
e/\e
/oo\
\oo/
e\/e
/\/\/\/\
\/\/\/\/
/\/\/\/\
\/\/\/\/
/\/\/\/\
\/\/\/\/
/\/\/\/\
\/\/\/\/
Explanation for Sample Input 1:
For test case 1: 
    We have to print 3 rows and 1 column of size 2 diamond.
Sample Input -2
2
2 5 2
1 1 3
Sample Output -2
e/\ee/\ee/\ee/\ee/\e
/oo\/oo\/oo\/oo\/oo\
\oo/\oo/\oo/\oo/\oo/
e\/ee\/ee\/ee\/ee\/e
e/\ee/\ee/\ee/\ee/\e
/oo\/oo\/oo\/oo\/oo\
\oo/\oo/\oo/\oo/\oo/
e\/ee\/ee\/ee\/ee\/e
e/\e
/oo\
\oo/
e\/e
Hint

Try to print only one diamond.

Approaches (1)
Brute Force

Approach: Since each diamond’s size is (2 * s) * (2 * s). So the total size of the grid will be (2 * s * r) * (2 * s * c). So we will iterate in all the points on the grid and use some basic algebra(modular arithmetics) to find the diamond to which this point belongs. Then we can identify the type of location(i.e. either this point is a boundary or not) as we can divide the diamond into four equal parts(top-right, top-left, bottom-right, bottom left). If (j % (2 * s)) >= s then the current point belongs to the right part else left part and, if (i % (2 * s)) >= s then the current point belongs to the lower part else the upper part. Where (i, j) is the current position. Now, each part is a square of length s, and the part’s diagonal is a diamond’s boundary.   

 

Algorithm:

  • Func diamond( i, j, s).
    • Create a boolean backslash and initialize it to true.
    • If ‘i’ >= s / 2.
      • Update ‘i’ to ‘s’ - ‘i’ - 1.
      • Update ‘backslash’ to !backslash.
    • If ‘j’ < s / 2.
      • Update j to s - j - 1.
      • Update ‘backslash’ to !backslash.
    • Decrement ‘j’ by s / 2.
    • If i == j.
      • If ‘backslash’ is true.
        • Return '\\'.
      • Else.
        • Return '/'.
    • Else if (i > j).
      • Return 'o'.
    • Else.
      • Return 'e'.
         
  • Func main().
    • Update s to 2 * s.
    • Update r to r * s.
    • Update c to c * s.
    • Iterate using a for loop from i = ‘0’ to ‘r - 1’.
      • Iterate using a for loop from j = ‘0’ to ‘c - 1’.
        • Print  diamond(i % s, j % s, s).
      • Print '\n'.
Time Complexity

O(s * s * r * c), where ‘s’ is the size of the diamond, r is the number of rows and c is the number of columns.

We are replacing r and c by multiplying with s to r * s and c * s. We are running a ‘for’ loop inside a ‘for’ loop. Hence the overall complexity is O(s * s * r * c).

Space Complexity

O(1).

We are using constant space. Hence the overall complexity is O(1).

Code Solution
(100% EXP penalty)
Draw The Diamond
Full screen
Console