Write ac Program to find maximum and minimum value in each column of a two dimensional array

Given a NxM matrix. The task is to find the maximum element in this matrix.
Examples
 

Input: mat[4][4] = {{1, 2, 3, 4}, {25, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; Output: 25 Input: mat[3][4] = {{9, 8, 7, 6}, {5, 4, 3, 2}, {1, 0, 12, 45}}; Output: 45

Approach: The idea is to traverse the matrix using two nested loops, one for rows and one for columns and find the maximum element. Initialize a variable maxElement with a minimum value and traverse the matrix and compare every time if the current element is greater than a maxElement. If yes then update maxElement with the current element.Below is the implementation of the above approach: 

int findMax(int mat[N][M])

    int maxElement = INT_MIN;

    for (int i = 0; i < N; i++) {

        for (int j = 0; j < M; j++) {

            if (mat[i][j] > maxElement) {

    int mat[N][M] = { { 1, 2, 3, 4 },

    cout << findMax(mat) << endl;

    static int findMax(int mat[][])

        int maxElement = Integer.MIN_VALUE;

        for (int i = 0; i < N; i++) {

            for (int j = 0; j < M; j++) {

                if (mat[i][j] > maxElement) {

    public static void main(String args[])

        int mat[][] = { { 1, 2, 3, 4 },

        System.out.println(findMax(mat)) ;

    maxElement = -sys.maxsize - 1

            if (mat[i][j] > maxElement):

if __name__ == '__main__':

    static int findMax(int[,] mat)

        int maxElement = int.MinValue;

        for (int i = 0; i < N; i++) {

            for (int j = 0; j < M; j++) {

                if (mat[i,j] > maxElement) {

    public static void Main()

        int[,]mat = {{ 1, 2, 3, 4},

        Console.Write(findMax(mat)) ;

    $maxElement = PHP_INT_MIN;

    for ($i = 0; $i < 4; $i++)

        for ($j = 0; $j < 4; $j++)

            if ($mat[$i][$j] > $maxElement)

                $maxElement = $mat[$i][$j];

$mat = array(array(1, 2, 3, 4),

echo findMax($mat) . "\n";

        let maxElement = Number.MIN_VALUE;

        for (let i = 0; i < N; i++) {

            for (let j = 0; j < M; j++) {

                if (mat[i][j] > maxElement) {

        let mat = [[ 1, 2, 3, 4 ],

        document.write(findMax(mat)) ;

Time Complexity: O(N*M)
 


Article Tags :

Given a matrix, the task is to find the minimum element of each row and each column.
Examples: 
 

Input: [1, 2, 3] [1, 4, 9] [76, 34, 21] Output: Minimum element of each row is {1, 1, 21} Minimum element of each column is {1, 2, 3} Input: [1, 2, 3, 21] [12, 1, 65, 9] [11, 56, 34, 2] Output: Minimum element of each row is {1, 1, 21} Minimum element of each column is {1, 2, 3}

Approach: The idea is to run the loop for no_of_rows. Check each element inside the row and find for the minimum element. Finally, print the element. Similarly, check each element inside the column and find for the minimum element. Finally, print the element.Below is the implementation of the above approach:

void smallestInRow(int mat[][MAX], int n, int m)

    for (int i = 0; i < n; i++) {

        for (int j = 1; j < m; j++) {

void smallestInCol(int mat[][MAX], int n, int m)

    for (int i = 0; i < m; i++) {

        for (int j = 1; j < n; j++) {

    int mat[][MAX] = { { 2, 1, 7 },

    cout << "Minimum element of each row is ";

    smallestInRow(mat, n, m);

    cout << "\nMinimum element of each column is ";

    smallestInCol(mat, n, m);

void smallestInRow(int mat[][MAX], int n, int m)

  for (int i = 0; i < n; i++) {

    for (int j = 1; j < m; j++) {

void smallestInCol(int mat[][MAX], int n, int m)

  for (int i = 0; i < m; i++) {

    for (int j = 1; j < n; j++) {

  int mat[][MAX] = { { 2, 1, 7 },

  printf("Minimum element of each row is ");

  smallestInRow(mat, n, m);

  printf("\nMinimum element of each column is ");

  smallestInCol(mat, n, m);

    final static int MAX = 100;

    static void smallestInRow(int mat[][], int n, int m) {

        for (int i = 0; i < n; i++) {

            for (int j = 1; j < m; j++) {

            System.out.print(minm + ", ");

    static void smallestInCol(int mat[][], int n, int m) {

        for (int i = 0; i < m; i++) {

            for (int j = 1; j < n; j++) {

            System.out.print(minm + ", ");

    public static void main(String args[]) {

        int mat[][] = {{2, 1, 7},

        System.out.print("Minimum element of each row is ");

        smallestInRow(mat, n, m);

        System.out.print("\nMinimum element of each column is ");

        smallestInCol(mat, n, m);

def smallestInRow(mat, n, m):

def smallestInCol(mat, n, m):

if __name__ == '__main__':

    print("Minimum element of each row is",

    print("Minimum element of each column is",

readonly static int MAX = 100;

static void smallestInRow(int [,]mat,

    for (int i = 0; i < n; i++)

        for (int j = 1; j < m; j++)

        Console.Write(minm + ", ");

static void smallestInCol(int [,]mat,

    for (int i = 0; i < m; i++)

        for (int j = 1; j < n; j++)

        Console.Write(minm + ", ");

public static void Main()

    Console.Write("Minimum element of " +

    smallestInRow(mat, n, m);

    Console.Write("\nMinimum element of " +

    smallestInCol(mat, n, m);

function smallestInRow(&$mat, $n, $m)

    for ($i = 0; $i < $n; $i++)

        for ($j = 1; $j < $m; $j++)

            if ($mat[$i][$j] < $minm)

function smallestInCol(&$mat, $n, $m)

    for ($i = 0; $i < $m; $i++)

        for ($j = 1; $j < $n; $j++)

            if ($mat[$j][$i] < $minm)

$mat = array(array( 2, 1, 7 ),

echo "Minimum element of each row is ";

smallestInRow($mat, $n, $m);

echo "\nMinimum element of each column is ";

smallestInCol($mat, $n, $m);

    function smallestInRow(mat,n,m) {

        for (let i = 0; i < n; i++) {

            for (let j = 1; j < m; j++) {

            document.write(minm + ", ");

        document.write("}"+"<br>");

    function smallestInCol(mat,n,m) {

        for (let i = 0; i < m; i++) {

            for (let j = 1; j < n; j++) {

            document.write(minm + ", ");

        document.write("Minimum element of each row is ");

        smallestInRow(mat, n, m);

        document.write("\nMinimum element of each column is ");

        smallestInCol(mat, n, m);

Output:  Minimum element of each row is { 1, 2, 4, } Minimum element of each column is { 2, 1, 2, }

Time complexity: O(n*m), as we are using nested for loops to traverse the Matrix.

Auxiliary Space: O(1), as we are not using any extra space.
 


Article Tags :

Postingan terbaru

LIHAT SEMUA