You are required to form a matrix of size where is the number of rows and is the number of columns. You are required to form the waves of numbers around the provided center, (0-based indexing).
Example:
- Size of the matrix: and
- Center coordinates: and (0 based indexing)
- Pattern:
4 4 4 4 4 4 4 4 4
4 3 3 3 3 3 3 3 4
4 3 2 2 2 2 2 3 4
4 3 2 1 1 1 2 3 4
4 3 2 1 0 1 2 3 4
4 3 2 1 1 1 2 3 4
4 3 2 2 2 2 2 3 4
4 3 3 3 3 3 3 3 4
4 4 4 4 4 4 4 4 4
You are given the values of (the values of and is 0-based indexed). Your task is to print the provided pattern.
Example:
Input: r=10, c=10, x=5, y=5
Output:
5 5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 4 4
5 4 3 3 3 3 3 3 3 4
5 4 3 2 2 2 2 2 3 4
5 4 3 2 1 1 1 2 3 4
5 4 3 2 1 0 1 2 3 4
5 4 3 2 1 1 1 2 3 4
5 4 3 2 2 2 2 2 3 4
5 4 3 3 3 3 3 3 3 4
5 4 4 4 4 4 4 4 4 4
Approach
Java
public class PrintingPatterns {public static void main(String args[]) {int r = 10;int c = 10;int x = 5;int y = 5;StringBuilder str = new StringBuilder();for (int i = 0; i < r; i++) {for (int j = 0; j < c; j++) {str.append(Math.max(Math.abs(x - i), Math.abs(y - j))).append(" ");}str.append("\n");}System.out.println(str);}}
No comments:
Post a Comment