Given an integer n. You need to recreate the pattern given below for any value of N. Let's say for N = 5, the pattern should look like as below:
5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 3 3 3 3 4 5 5 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
Print the pattern in the function given to you.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
//Function to print pattern22
void pattern22(int n) {
// Outer loop for the rows
for(int i = 0; i < 2*n-1; i++){
// Inner loop for the columns.
for(int j = 0; j < 2*n-1; j++){
/* Initialising the top, down, left
and right indices of a cell.*/
int top = i;
int left = j;
int right = (2*n - 2) - j;
int bottom = (2*n - 2) - i;
/* Minimum of 4 directions and then we
subtract from n because previously we
would get a pattern whose border has 0's,
but we want with border n's and then
decreasing inside.*/
cout<<(n- min(min(top,bottom), min(left,right)))<<" ";
}
//Move to the next row
cout<<endl;
}
}
};
int main() {
int N = 5;
//Create an instance of Solution class
Solution sol;
sol.pattern22(N);
return 0;
}
import java.util.*;
class Solution {
// Function to print pattern22
public void pattern22(int n) {
// Outer loop for the rows
for (int i = 0; i < 2 * n - 1; i++) {
// Inner loop for the columns
for (int j = 0; j < 2 * n - 1; j++) {
/* Initialising the top, down, left
and right indices of a cell.*/
int top = i;
int left = j;
int right = (2 * n - 2) - j;
int bottom = (2 * n - 2) - i;
/* Minimum of 4 directions and then we
subtract from n because previously we
would get a pattern whose border has 0's,
but we want with border n's and then
decreasing inside.*/
System.out.print((n - Math.min(Math.min(top, bottom), Math.min(left, right))) + " ");
}
// Move to the next row
System.out.println();
}
}
public static void main(String[] args) {
int N = 5;
// Create an instance of Solution class
Solution sol = new Solution();
sol.pattern22(N);
}
}
class Solution:
# Function to print pattern22
def pattern22(self, n):
# Outer loop for the rows
for i in range(2 * n - 1):
# Inner loop for the columns
for j in range(2 * n - 1):
""" Initialising the top, down, left
and right indices of a cell"""
top = i
left = j
right = (2 * n - 2) - j
bottom = (2 * n - 2) - i
""" Minimum of 4 directions and then we
subtract from n because previously we
would get a pattern whose border has 0's,
but we want with border n's and then
decreasing inside."""
print((n - min(min(top, bottom), min(left, right))), end=" ")
# Move to the next row
print()
# Main function
if __name__ == "__main__":
N = 5
# Create an instance of Solution class
sol = Solution()
sol.pattern22(N)
class Solution {
// Function to print pattern22
pattern22(n) {
// Outer loop for the rows
for (let i = 0; i < 2 * n - 1; i++) {
// Inner loop for the columns
for (let j = 0; j < 2 * n - 1; j++) {
/* Initialising the top, down, left
and right indices of a cell*/
let top = i;
let left = j;
let right = (2 * n - 2) - j;
let bottom = (2 * n - 2) - i;
/* Minimum of 4 directions and then we
subtract from n because previously we
would get a pattern whose border has 0's,
but we want with border n's and then
decreasing inside.*/
process.stdout.write((n - Math.min(Math.min(top, bottom), Math.min(left, right))) + " ");
}
// Move to the next row
console.log();
}
}
}
let N = 5;
//Create an instance of Solution class
let sol = new Solution();
sol.pattern22(N);