Find number of Positional Elements

Given a matrix of integers, task is to find out number of positional elements. A positional element is one which is either minimum or maximum in a row or in a column.

Examples:

Input : a = {{1, 3, 4}, {5, 2, 9}, {8, 7, 6}}
Output : 7
There are total 7 elements min elements are 1, 2, 6 and 4. And max elements are 9, 8 and 7.

Input : a = {{1, 1}, {1, 1}, {1, 1}}
Output : 4

Source :Goldman Sachs Interview Set

Idea is to store the maximum and minimum of every row and column and then check for the required condition.

Below is the implementation of above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find positional elements in
// a matrix.
#include <bits/stdc++.h>
using namespace std;
  
const int MAX = 100;
  
int countPositional(int a[][MAX], int m, int n)
{
    // rwomax[i] is going to store maximum of
    // i-th row and other arrays have similar
    // meaning
    int rowmax[m], rowmin[m];
    int colmax[n], colmin[n];
  
    // Find rminn and rmaxx for every row
    for (int i = 0; i < m; i++) {
        int rminn = INT_MAX;
        int rmaxx = INT_MIN;
        for (int j = 0; j < n; j++) {
            if (a[i][j] > rmaxx)
                rmaxx = a[i][j];
            if (a[i][j] < rminn)
                rminn = a[i][j];
        }
        rowmax[i] = rmaxx;
        rowmin[i] = rminn;
    }
  
    // Find cminn and cmaxx for every column
    for (int j = 0; j < n; j++) {
        int cminn = INT_MAX;
        int cmaxx = INT_MIN;
        for (int i = 0; i < m; i++) {
            if (a[i][j] > cmaxx)
                cmaxx = a[i][j];
            if (a[i][j] < cminn)
                cminn = a[i][j];
        }
  
        colmax[j] = cmaxx;
        colmin[j] = cminn;
    }
  
    // Check for optimal element
    int count = 0;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if ((a[i][j] == rowmax[i])
                || (a[i][j] == rowmin[i])
                || (a[i][j] == colmax[j])
                || (a[i][j] == colmin[j])) {
                count++;
            }
        }
    }
  
    return count;
}
  
// Driver code
int main()
{
    int a[][MAX] = { { 1, 3, 4 },
                     { 5, 2, 9 },
                     { 8, 7, 6 } };
    int m = 3, n = 3;
    cout << countPositional(a, m, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find positional elements in
// a matrix.
class GfG {
  
    static int MAX = 100;
  
    static int countPositional(int a[][], int m, int n)
    {
        // rwomax[i] is going to store maximum of
        // i-th row and other arrays have similar
        // meaning
        int rowmax[] = new int[m];
        int rowmin[] = new int[m];
        int colmax[] = new int[n];
        int colmin[] = new int[n];
  
        // Find rminn and rmaxx for every row
        for (int i = 0; i < m; i++) {
            int rminn = Integer.MAX_VALUE;
            int rmaxx = Integer.MIN_VALUE;
            for (int j = 0; j < n; j++) {
                if (a[i][j] > rmaxx)
                    rmaxx = a[i][j];
                if (a[i][j] < rminn)
                    rminn = a[i][j];
            }
            rowmax[i] = rmaxx;
            rowmin[i] = rminn;
        }
  
        // Find cminn and cmaxx for every column
        for (int j = 0; j < n; j++) {
            int cminn = Integer.MAX_VALUE;
            int cmaxx = Integer.MIN_VALUE;
            for (int i = 0; i < m; i++) {
                if (a[i][j] > cmaxx)
                    cmaxx = a[i][j];
                if (a[i][j] < cminn)
                    cminn = a[i][j];
            }
  
            colmax[j] = cmaxx;
            colmin[j] = cminn;
        }
  
        // Check for optimal element
        int count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ((a[i][j] == rowmax[i])
                    || (a[i][j] == rowmin[i])
                    || (a[i][j] == colmax[j])
                    || (a[i][j] == colmin[j])) {
                    count++;
                }
            }
        }
  
        return count;
    }
  
    public static void main(String[] args)
    {
        int a[][] = new int[][] { { 1, 3, 4 }, { 5, 2, 9 }, { 8, 7, 6 } };
        int m = 3, n = 3;
        System.out.println(countPositional(a, m, n));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find positional elements in
using System;
  
class GFG {
  
    static int countPositional(int[, ] a, int m, int n)
    {
        // rwomax[i] is going to store maximum of
        // i-th row and other arrays have similar
        // meaning
        int[] rowmax = new int[m];
        int[] rowmin = new int[m];
        int[] colmax = new int[n];
        int[] colmin = new int[n];
  
        // Find rminn and rmaxx for every row
        for (int i = 0; i < m; i++) {
            int rminn = int.MaxValue;
            int rmaxx = int.MinValue;
            for (int j = 0; j < n; j++) {
                if (a[i, j] > rmaxx)
                    rmaxx = a[i, j];
                if (a[i, j] < rminn)
                    rminn = a[i, j];
            }
            rowmax[i] = rmaxx;
            rowmin[i] = rminn;
        }
  
        // Find cminn and cmaxx for every column
        for (int j = 0; j < n; j++) {
            int cminn = int.MaxValue;
            int cmaxx = int.MinValue;
            for (int i = 0; i < m; i++) {
                if (a[i, j] > cmaxx)
                    cmaxx = a[i, j];
                if (a[i, j] < cminn)
                    cminn = a[i, j];
            }
  
            colmax[j] = cmaxx;
            colmin[j] = cminn;
        }
  
        // Check for optimal element
        int count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ((a[i, j] == rowmax[i])
                    || (a[i, j] == rowmin[i])
                    || (a[i, j] == colmax[j])
                    || (a[i, j] == colmin[j])) {
                    count++;
                }
            }
        }
  
        return count;
    }
  
    // Driver Code
    static public void Main()
    {
        int[, ] a = new int[, ] { { 1, 3, 4 }, { 5, 2, 9 }, { 8, 7, 6 } };
        int m = 3, n = 3;
        Console.WriteLine(countPositional(a, m, n));
    }
}
  
// This code is contributed by Tushil.

chevron_right


Output:

7


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.