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++
// 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; } |
Java
// 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)); } } |
C#
// 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. |
7
Recommended Posts:
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Find the smallest after deleting given elements
- Find unique elements in a matrix
- Find the largest after deleting the given elements
- Find the k smallest numbers after deleting given elements
- Find the sum of elements of the Matrix generated by the given rules
- Find the k largest numbers after deleting the given elements
- Find array elements with frequencies in range [l , r]
- Find pairs with given sum such that elements of pair are in different rows
- Find common elements in two ArrayLists in Java
- Find the sum of the diagonal elements of the given N X N spiral matrix
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Program to find the smallest element among three elements
- Find distinct elements common to all rows of a matrix
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.



