Minimize the maximum of Matrix whose rows and columns are in A.P
Last Updated :
23 Dec, 2022
Given two integers N and M denoting a matrix of size N*M, the task is to form a matrix following the below criteria and make the maximum element as minimum as possible:
- Elements in each row are in arithmetic progression from left to right.
- Elements in each column are also in arithmetic progression from top to bottom.
- The common differences among all the rows and columns are unique.
Note: If there are multiple possible answers, print any of them.
Examples:
Input: N = 2, N = 4
Output: {{1, 2, 3, 4}, {4, 6, 8, 10}}
Explanation: In the above the common difference for 1st row is 1.
The common difference for the second row is 2.
The common difference for the columns are 3, 4, 5, and 6 respectively.
So all the common differences are unique and
the maximum value is 10 which is the minimum possible.
Input: A = 1, B = 3
Output: {1, 2, 3}
Approach: The problem can be solved based on the following observation:
Observations:
To minimize the maximum element we need to make the common difference minimum along the longer side. We should start from the minimum common difference of 1 for that side. Then increment common difference by 1 along those sides. After that think about common difference for the other side.
For example if there are 2 rows and 4 columns, there are more columns than rows. Each row have 4 elements. Therefore, the common difference for the first row we should set as 1, then for second row as 2.
After the row are traversed, set the common difference for first column as 3 and continue this.
Follow the steps mentioned below to implement the above idea:
- First, check N ≤ M or not:
- If it is, then:
- The first row elements are strictly increasing from left to right with a fixed common difference of 1 similarly increment the common difference by 1 and follow the same process for each row.
- And then for the 1st column elements are strictly increasing from top to bottom with a fixed common difference A+1 and the common difference is incremented by 1 for each next column.
- If not then:
- The first column elements are strictly increasing from top to bottom with a fixed common difference of 1 and increment the common difference by 1 for each of the next columns and follow the same process for them.
- For the 1st row, elements are strictly increasing from left to right with a fixed common difference B+1 and the common difference is incremented by 1 for each of the next rows.
- Return the matrix thus formed as the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void matrix( int A, int B)
{
if (A <= B) {
for ( int j = 0; j < A; j++) {
for ( int k = 0; k < B; k++) {
cout << 1 + (A + 1) * j + (j + 1) * k
<< " " ;
}
cout << endl;
}
}
else {
for ( int j = 0; j < A; j++) {
for ( int k = 0; k < B; k++) {
cout << 1 + (B + 1) * k + (k + 1) * j
<< " " ;
}
cout << endl;
}
}
}
int main()
{
int A = 2;
int B = 4;
matrix(A, B);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static ArrayList<ArrayList<Integer> >
matrix( int A, int B)
{
ArrayList<ArrayList<Integer> > res
= new ArrayList<ArrayList<Integer> >();
if (A <= B) {
for ( int j = 0 ; j < A; j++) {
ArrayList<Integer> temp
= new ArrayList<Integer>();
for ( int k = 0 ; k < B; k++) {
temp.add( 1 + (A + 1 ) * j + (j + 1 ) * k);
}
res.add(temp);
}
}
else {
for ( int j = 0 ; j < A; j++) {
ArrayList<Integer> temp
= new ArrayList<Integer>();
for ( int k = 0 ; k < B; k++) {
temp.add( 1 + (B + 1 ) * k + (k + 1 ) * j);
}
res.add(temp);
}
}
return res;
}
public static void main(String[] args)
{
int A = 2 ;
int B = 4 ;
ArrayList<ArrayList<Integer> > ans = matrix(A, B);
for ( int i = 0 ; i < ans.size(); i++) {
for ( int j = 0 ; j < ans.get(i).size(); j++)
System.out.print(ans.get(i).get(j) + " " );
System.out.println();
}
}
}
|
Python3
def matrix(A, B):
if (A < = B):
for j in range (A):
for k in range (B):
print ( 1 + (A + 1 ) * j + (j + 1 ) * k, end = " " )
print ()
else :
for j in range (A):
for k in range (B):
print ( 1 + (B + 1 ) * k + (k + 1 ) * j, end = " " )
print ()
if __name__ = = "__main__" :
A = 2
B = 4
matrix(A, B)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static List<List< int > > matrix( int A, int B)
{
List<List< int > > res = new List<List< int > >();
if (A <= B) {
for ( int j = 0; j < A; j++) {
List< int > temp = new List< int >();
for ( int k = 0; k < B; k++) {
temp.Add(1 + (A + 1) * j + (j + 1) * k);
}
res.Add(temp);
}
}
else {
for ( int j = 0; j < A; j++) {
List< int > temp = new List< int >();
for ( int k = 0; k < B; k++) {
temp.Add(1 + (B + 1) * k + (k + 1) * j);
}
res.Add(temp);
}
}
return res;
}
public static void Main( string [] args)
{
int A = 2;
int B = 4;
List<List< int > > ans = matrix(A, B);
for ( int i = 0; i < ans.Count; i++) {
for ( int j = 0; j < ans[i].Count; j++)
Console.Write(ans[i][j] + " " );
Console.WriteLine();
}
}
}
|
Javascript
<script>
function matrix(A,B)
{
if (A <= B) {
for (let j = 0; j < A; j++) {
for (let k = 0; k < B; k++) {
let ans=1 + (A + 1) * j + (j + 1) * k;
document.write(ans+ " " );
}
document.write( "<br>" );
}
}
else {
for (let j = 0; j < A; j++) {
for (let k = 0; k < B; k++) {
let ans1=1 + (B + 1) * k + (k + 1) * j;
document.write(ans1+ " " );
}
document.write( "<br>" );
}
}
}
let A = 2;
let B = 4;
matrix(A, B);
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(N * M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...