Count of matrices (of different orders) with given number of elements
Last Updated :
13 May, 2021
Given a number N denotes the total number of elements in a matrix, the task is to print all possible order of matrix. An order is a pair (m, n) of integers where m is number of rows and n is number of columns. For example, if the number of elements is 8 then all possible orders are:
(1, 8), (2, 4), (4, 2), (8, 1).
Examples:
Input: N = 8
Output: (1, 2) (2, 4) (4, 2) (8, 1)
Input: N = 100
Output:
(1, 100) (2, 50) (4, 25) (5, 20) (10, 10) (20, 5) (25, 4) (50, 2) (100, 1)
Approach:
A matrix is said to be of order m x n if it has m rows and n columns. The total number of elements in a matrix is equal to (m*n). So we start from 1 and check one by one if it divides N(the total number of elements). If it divides, it will be one possible order.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include <iostream>
using namespace std;
// Function to print all possible order
void printAllOrder(int n)
{
// total number of elements in a matrix
// of order m * n is equal (m*n)
// where m is number of rows and n is
// number of columns
for (int i = 1; i <= n; i++) {
// if n is divisible by i then i
// and n/i will be the one
// possible order of the matrix
if (n % i == 0) {
// print the given format
cout << i << " " << n / i << endl;
}
}
}
// Driver code
int main()
{
int n = 10;
printAllOrder(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to print all possible order
static void printAllOrder(int n)
{
// total number of elements in a matrix
// of order m * n is equal (m*n)
// where m is number of rows and n is
// number of columns
for (int i = 1; i <= n; i++) {
// if n is divisible by i then i
// and n/i will be the one
// possible order of the matrix
if (n % i == 0) {
// print the given format
System.out.println( i + " " + n / i );
}
}
}
// Driver code
public static void main(String []args)
{
int n = 10;
printAllOrder(n);
}
}
// This code is contributed by ihritik
Python
# Python implementation of the above approach
# Function to print all possible order
def printAllOrder(n):
# total number of elements in a matrix
# of order m * n is equal (m*n)
# where m is number of rows and n is
# number of columns
for i in range(1,n+1):
# if n is divisible by i then i
# and n/i will be the one
# possible order of the matrix
if (n % i == 0) :
# print the given format
print( i ,n // i )
# Driver code
n = 10
printAllOrder(n)
# This code is contributed by ihritik
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to print all possible order
static void printAllOrder(int n)
{
// total number of elements in a matrix
// of order m * n is equal (m*n)
// where m is number of rows and n is
// number of columns
for (int i = 1; i <= n; i++) {
// if n is divisible by i then i
// and n/i will be the one
// possible order of the matrix
if (n % i == 0) {
// print the given format
Console.WriteLine( i + " " + n / i );
}
}
}
// Driver code
public static void Main()
{
int n = 10;
printAllOrder(n);
}
}
// This code is contributed by ihritik
PHP
<?php
// PHP implementation of the above approach
// Function to print all possible order
function printAllOrder($n)
{
// total number of elements in a matrix
// of order m * n is equal (m*n)
// where m is number of rows and n is
// number of columns
for ($i = 1; $i <= $n; $i++)
{
// if n is divisible by i then i
// and n/i will be the one
// possible order of the matrix
if ($n % $i == 0)
{
// print the given format
echo $i, " ", ($n / $i), "\n";
}
}
}
// Driver code
$n = 10;
printAllOrder($n);
// This code is contributed by Ryuga
?>
JavaScript
<script>
// Java Script implementation of the above approach
// Function to print all possible order
function printAllOrder( n)
{
// total number of elements in a matrix
// of order m * n is equal (m*n)
// where m is number of rows and n is
// number of columns
for (let i = 1; i <= n; i++) {
// if n is divisible by i then i
// and n/i will be the one
// possible order of the matrix
if (n % i == 0) {
// print the given format
document.write( i + " " + n / i+"<br>" );
}
}
}
// Driver code
let n = 10;
printAllOrder(n);
// This code is contributed by sravan
</script>
Output: 1 10
2 5
5 2
10 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Queries on number of Binary sub-matrices of Given size Given a Binary Matrix of size N X M, the task is to answer Q queries of the following type; Query(a, b): Find the number of sub matrices of size a X a with each of its element consisting of the Binary Number b. We basically need to find submatrices of given sizes with all 1s or all 0s. Examples: Inp
12 min read
Count number of free cell present in the Matrix Given a Matrix size N*N and an integer K. Initially, the matrix contains only 0. You are given K tasks and for each task, you are given two coordinates (r, c). Where coordinates (r, c) denotes the rth row and the cth column of the given matrix. You have to perform each task sequentially in the given
11 min read
Count of odd sum Submatrix with odd element count in the Matrix Given a matrix mat[][] of size N x N, the task is to count the number of submatrices with the following properties: The sum of all elements in the submatrix is odd.The number of elements in the submatrix is odd.Examples: Input: mat[][] = {{1, 2, 3}, {7, 5, 9}, {6, 8, 10}}Output: 8Explanation: As her
15 min read
Count unique paths is a matrix whose product of elements contains odd number of divisors Given a matrix mat[][] of dimension NxM, the task is to count the number of unique paths from the top-left cell, i.e. mat[0][0], to the bottom-right cell, i.e. mat[N - 1][M - 1] of the given matrix such that product of elements in that path contains an odd number of divisors. Possible moves from any
15+ min read
Count pairs from two sorted matrices with given sum Given two sorted matrices mat1 and mat2 of size n x n of distinct elements. Given a value x, the task is to count all pairs from both matrices whose sum is equal to x. Note: The pair has an element from each matrix. Matrices are strictly sorted which means that matrices are sorted in a way such that
15 min read