In this blog post we will Provide Coding Ninjas Basics of Java with Data Structures and Algorithms Solution. This will have solutions to all the problems that are included in Coding Ninjas Java Course.  Rectangular Numbers Coding Ninjas question with solution.

Rectangular Numbers Coding Ninjas question


Rectangular numbers
Send Feedback
Print the following pattern for the given number of rows.


Pattern for N = 4
4444444
4333334
4322234
4321234
4322234
4333334
4444444


Input format : N (Total no. of rows)
Output format : Pattern in N lines


Sample Input :
3


Sample Output :
33333
32223
32123
32223
33333

<img fetchpriority=
Rectangular Numbers Coding Ninjas question with solution

Coding ninjas solutions

Java Language [Solutions 1]

public class RectangularNumbers { 
    // coding ninjas solution
    public static void print(int n) {

        // Write your code here
        int i, j, a;
        for (i = -(n - 1); i < n; i++) {
            a = n;
            for (j = -(n - 1); j < n; j++) {
                if (Math.abs(i) < Math.abs(j)) {
                    if (j < 0) {
                        System.out.print(a);
                        a--;
                    } else {
                        a++;
                        System.out.print(a);
                    }
                } else {
                    System.out.print(a);
                }
            }
            System.out.println();
        }
    }
    //main driver code
    public static void main(String[] args) {
        print(4);
    }
}

Output

java -cp /tmp/kY2Dxx35A3 RectangularNumbers
4444444
4333334
4322234
4321234
4322234
4333334
4444444

C++ [Solutions 2]

// C++ Program to print rectangular
// inner reducing pattern
#include <bits/stdc++.h>
using namespace std;

#define max 100

// function to Print pattern
void print(int a[][max], int size) 
{
for (int i = 0; i < size; i++) {
	for (int j = 0; j < size; j++) {
	cout << a[i][j];
	}
	cout << endl;
}
}

// function to compute pattern
void innerPattern(int n) {
	
// Pattern Size
int size = 2 * n - 1;
int front = 0;
int back = size - 1;
int a[max][max];
while (n != 0) 
{
	for (int i = front; i <= back; i++) {
	for (int j = front; j <= back; j++) {
		if (i == front || i == back ||
			j == front || j == back)
		a[i][j] = n;
	}
	}
	++front;
	--back;
	--n;
}
print(a, size);
}

// Driver code
int main()
{
	// Input
	int n = 4; 
	
	// function calling
	innerPattern(n);
	
return 0;
}

// This code is contributed by Anant Agarwal.

Python3 [Solutions 3]

# Python3 Program to print rectangular 
# inner reducing pattern 
MAX = 100

# function to Print pattern 
def prints(a, size): 
	for i in range(size): 
		for j in range(size):
			print(a[i][j], end = '')
		print()

# function to compute pattern 
def innerPattern(n): 
	
	# Pattern Size 
	size = 2 * n - 1
	front = 0
	back = size - 1
	a = [[0 for i in range(MAX)] 
			for i in range(MAX)]
	while (n != 0): 
		for i in range(front, back + 1):
			for j in range(front, back + 1):
				if (i == front or i == back or
					j == front or j == back):
					a[i][j] = n
		front += 1
		back -= 1
		n -= 1
	prints(a, size); 

# Driver code 

# Input 
n = 4

# function calling 
innerPattern(n)


C# [Solutions 4]

using System;

public class Pattern {

// function to compute pattern
public static void InnerPattern(int n)
{
	
	// Pattern Size
	int size = 2 * n - 1;
	int front = 0;
	int back = size - 1;
	int[, ] a = new int[size, size];

	while (n != 0) {
	for (int i = front; i <= back; i++) {
		for (int j = front; j <= back; j++) {
		if (i == front || i == back
			|| j == front || j == back)
			a[i, j] = n;
		}
	}
	++front;
	--back;
	--n;
	}
	Print(a, size);
}

// function to Print pattern
public static void Print(int[, ] a, int size)
{
	for (int i = 0; i < size; i++) {
	for (int j = 0; j < size; j++) {
		Console.Write(a[i, j]);
	}
	Console.WriteLine();
	}
}

// Main Method
public static void Main(string[] args)
{
	int n = 4; // Input
	InnerPattern(n);
}
}

JavaScript [Solutions 5]

const MAX = 100;

// function to Print pattern 
function prints(a, size) {
for (let i = 0; i < size; i++) {
	let row = '';
	for (let j = 0; j < size; j++) {
	row += a[i][j];
	}
	console.log(row);
}
}

// function to compute pattern 
function innerPattern(n) {
// Pattern Size 
const size = 2 * n - 1;
let front = 0;
let back = size - 1;
let a = new Array(MAX).fill(0).map(() => new Array(MAX).fill(0));
while (n !== 0) {
	for (let i = front; i <= back; i++) {
	for (let j = front; j <= back; j++) {
		if (i === front || i === back || j === front || j === back) {
		a[i][j] = n;
		}
	}
	}
	front += 1;
	back -= 1;
	n -= 1;
}
prints(a, size);
}

// Driver code 

// Input 
const n = 4;

// function calling 
innerPattern(n);

Conclusion

Coding Ninja primarily targets students and professionals looking to enhance their coding and programming skills, prepare for technical interviews, or pursue careers in software development and related fields. It has gained a reputation for its quality courses and experienced instructors, making it a popular choice among those looking to improve their coding abilities.

Related Articles:

READ MORE


Deepika

Hey, I'm Deepika, Experienced in Mobile app Development (Flutter, Android and iOS) and professional blogger. Technically sound Post graduated M.Tech in Computer Science and Engineering. I Love to gain every type of knowledge that's why i have done many courses in different fields like engineering and technology. Skilled in Flutter,( Dart ), Java, HTML, CSS, PHP, Python, SQL, C, C++,Firebase,MySQL,SQLite,JavaScript, Networking, Ethical Hacking.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *