Unsafe elements in a matrix
Practice
4 (21 votes)
Arrays
Data structures
Multi Dimensional
Problem
46% Success 6783 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You have a matrix \(S\) consisting of \(N\) rows and \(M\) columns. Let \(u\) be the maximum element of the matrix and \(v\) be the smallest element of the matrix. If any element whose value is equal to \(u\) or \(v\) are called unsafe elements and they disfigure the complete row and column of the matrix. More formally, if any element is equal to \(u\) or \(v\) and contains cell number \((x,\ y)\), that is, \(S[x][y]=u\) or \(S[x][y]=v\) are unsafe so that they also disfigure all the cells that have row \(x\) or column \(y\) and also are unsafe.

Your task is to find the number of safe elements.

Input format

  • The first line contains \(T\) denoting the number of test cases.
  • The first line of each test case consists of two space-separated integers \(N\) and \(M\).
  • Next \(N\) lines consist of \(M\) space-separated integers.

Output format

For each test case, print a single integer denoting the safe elements.

Constraints

\(1\leq T\leq100000\)

\(1\leq N*M \leq1e5\)

\(1\leq S_(i,j)\leq1e5\)   \(\forall i \in [1,N]\)  \(\forall j \in [1,M] \)

Sum of \(N\times M\) over all test cases does not exceed \(1e6\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
20 votes
Tags:
ApprovedEasy
Points:30
28 votes
Tags:
Ad-HocApprovedData StructuresEasy
Points:30
49 votes
Tags:
ArraysData StructuresEasyMulti-dimensional