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++
#include <iostream>
using namespace std;
void printAllOrder( int n)
{
for ( int i = 1; i <= n; i++) {
if (n % i == 0) {
cout << i << " " << n / i << endl;
}
}
}
int main()
{
int n = 10;
printAllOrder(n);
return 0;
}
|
Java
class GFG
{
static void printAllOrder( int n)
{
for ( int i = 1 ; i <= n; i++) {
if (n % i == 0 ) {
System.out.println( i + " " + n / i );
}
}
}
public static void main(String []args)
{
int n = 10 ;
printAllOrder(n);
}
}
|
Python
def printAllOrder(n):
for i in range ( 1 ,n + 1 ):
if (n % i = = 0 ) :
print ( i ,n / / i )
n = 10
printAllOrder(n)
|
C#
using System;
class GFG
{
static void printAllOrder( int n)
{
for ( int i = 1; i <= n; i++) {
if (n % i == 0) {
Console.WriteLine( i + " " + n / i );
}
}
}
public static void Main()
{
int n = 10;
printAllOrder(n);
}
}
|
PHP
<?php
function printAllOrder( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $n % $i == 0)
{
echo $i , " " , ( $n / $i ), "\n" ;
}
}
}
$n = 10;
printAllOrder( $n );
?>
|
Javascript
<script>
function printAllOrder( n)
{
for (let i = 1; i <= n; i++) {
if (n % i == 0) {
document.write( i + " " + n / i+ "<br>" );
}
}
}
let n = 10;
printAllOrder(n);
</script>
|
Output:
1 10
2 5
5 2
10 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 May, 2021
Like Article
Save Article