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 : 6
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 the 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) { // rowmax[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) { // rowmax[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)); } } |
Python3
# Python3 program to find positional elements in a matrix. import sys MAX = 100 def countPositional(a, m, n): # rowmax[i] is going to store maximum of # i-th row and other arrays have similar # meaning rowmax = [ 0 ] * m rowmin = [ 0 ] * m colmax = [ 0 ] * n colmin = [ 0 ] * n # Find rminn and rmaxx for every row for i in range (m) : rminn = sys.maxsize rmaxx = - sys.maxsize for j in range (n) : 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 j in range (n) : cminn = sys.maxsize cmaxx = - sys.maxsize for i in range (m) : 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 count = 0 for i in range (m) : for j in range (n) : if ((a[i][j] = = rowmax[i]) or (a[i][j] = = rowmin[i]) or (a[i][j] = = colmax[j]) or (a[i][j] = = colmin[j])) : count + = 1 return count # Driver code a = [ [ 1 , 3 , 4 ], [ 5 , 2 , 9 ], [ 8 , 7 , 6 ] ] m, n = 3 , 3 print (countPositional(a, m, n)) # This code is contributed by divyeshrabadiya07 |
C#
// C# program to find positional elements in using System; class GFG { static int countPositional( int [, ] a, int m, int n) { // rowmax[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. |
Javascript
<script> // Javascript program to find positional // elements in a matrix. let MAX = 100; function countPositional(a, m, n) { // rowmax[i] is going to store maximum of // i-th row and other arrays have similar // meaning let rowmax = new Array(m); let rowmin = new Array(m); let colmax = new Array(n); let colmin = new Array(n); // Find rminn and rmaxx for every row for (let i = 0; i < m; i++) { let rminn = Number.MAX_VALUE; let rmaxx = Number.MIN_VALUE; for (let 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 (let j = 0; j < n; j++) { let cminn = Number.MAX_VALUE; let cmaxx = Number.MIN_VALUE; for (let 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 let count = 0; for (let i = 0; i < m; i++) { for (let 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 let a = [ [ 1, 3, 4 ], [ 5, 2, 9 ], [ 8, 7, 6 ] ]; let m = 3, n = 3; document.write(countPositional(a, m, n)); // This code is contributed by suresh07 </script> |
Output
7
Please Login to comment...