Skip to content
Related Articles

Related Articles

Minimize the maximum of Matrix whose rows and columns are in A.P

View Discussion
Improve Article
Save Article
  • Last Updated : 25 Jul, 2022
View Discussion
Improve Article
Save Article

Given two integers N and M denoting a matrix of size N*M, the task is to form a matric 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++




// C++ code to implement the approach
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
// Function to find the matrix
void matrix(int A, int B)
{
 
    // Number of row is less than or
    // equal to number of column
    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;
        }
    }
 
    // Number of column is less
    // than number of row
    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;
        }
    }
}
 
// Driver Code
int main()
{
    int A = 2;
    int B = 4;
 
    // Function call
    matrix(A, B);
    return 0;
}
 
// This code is contributed by aarohirai2616.

Java




// Java code to implement the approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the matrix
    public static ArrayList<ArrayList<Integer> >
    matrix(int A, int B)
    {
        ArrayList<ArrayList<Integer> > res
            = new ArrayList<ArrayList<Integer> >();
 
        // Number of row is less than or
        // equal to number of column
        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);
            }
        }
 
        // Number of column is less
        // than number of row
        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;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int A = 2;
        int B = 4;
 
        // Function call
        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




# Python3 code to implement the approach
 
# Function to find the matrix
 
 
def matrix(A, B):
 
    # Number of row is less than or
    # equal to number of column
    if (A <= B):
        for j in range(A):
            for k in range(B):
                print(1 + (A + 1) * j + (j + 1) * k, end=" ")
 
            print()
 
    # Number of column is less
    # than number of row
    else:
        for j in range(A):
            for k in range(B):
                print(1 + (B + 1) * k + (k + 1) * j, end=" ")
 
            print()
 
 
# Driver Code
if __name__ == "__main__":
 
    A = 2
    B = 4
 
    # Function call
    matrix(A, B)
 
    # This code is contributed by AnkThon

C#




// C# code to implement the approach
 
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to find the matrix
    public static List<List<int> > matrix(int A, int B)
    {
        List<List<int> > res = new List<List<int> >();
 
        // Number of row is less than or
        // equal to number of column
        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);
            }
        }
 
        // Number of column is less
        // than number of row
        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;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int A = 2;
        int B = 4;
 
        // Function call
        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();
        }
    }
}
 
// This code is contributed by phasing17

Javascript




// Javascript  code to implement the approach
<script>
 
// Function to find the matrix
function matrix(A,B)
{
   
    // Number of row is less than or
    // equal to number of column
    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>");
        }
    }
 
    // Number of column is less
    // than number of row
    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>");
        }
    }
}
 
// Driver Code
 
    let A = 2;
    let B = 4;
   
    // Function call
    matrix(A, B);
    
   // This code is contributed by satwik4409.
</script>

Output

1 2 3 4 
4 6 8 10 

Time Complexity: O(N * M)
Auxiliary Space: O(N * M)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!