Given a matrix mat[][] of size n × m, count the number of rows that are sorted either in strictly increasing order or strictly decreasing order.
Examples:
Input: mat[][] = [[1, 2, 3], [6, 5, 4], [7, 9, 8]]
Output: 2
Explanation: The first row is sorted in strictly increasing order while the second row is sorted in strictly decreasing order.Input: mat[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: 3
Explanation: All the rows are sorted in strictly increasing order.
Table of Content
Check Both Increasing and Decreasing Order Separately - O(m * n) Time and O(1) Space
We independently check whether each row is strictly increasing or strictly decreasing.
First, scan the row from left to right for increasing order; if that fails, check it again for decreasing order.
- Initialize count = 0.
- For each row, check whether it is strictly increasing.
- If not, check whether it is strictly decreasing.
- Stop each check when an invalid adjacent pair is found.
- If either condition is satisfied, increment count.
- Return count.
#include <bits/stdc++.h>
using namespace std;
int sortedCount(vector<vector<int>> &mat)
{
int n = mat.size();
if (n == 0)
return 0;
int m = mat[0].size();
int count = 0;
// Check each row one by one.
for (int i = 0; i < n; i++)
{
bool isIncreasing = true;
// Check whether the current row is strictly increasing.
for (int j = 1; j < m; j++)
{
// If the current element is not strictly greater
// than the previous element, the row is not increasing.
if (mat[i][j] <= mat[i][j - 1])
{
isIncreasing = false;
break;
}
}
// If the row is increasing, count it.
if (isIncreasing)
{
count++;
continue;
}
bool isDecreasing = true;
// Check whether the current row is strictly decreasing.
for (int j = 1; j < m; j++)
{
// If the current element is not strictly smaller
// than the previous element, the row is not decreasing.
if (mat[i][j] >= mat[i][j - 1])
{
isDecreasing = false;
break;
}
}
// If the row is decreasing, count it.
if (isDecreasing)
count++;
}
return count;
}
int main()
{
vector<vector<int>> mat = {{1, 2, 3}, {6, 5, 4}, {8, 7, 9}};
cout << sortedCount(mat) << endl;
return 0;
}
class GFG {
static int sortedCount(int[][] mat)
{
int n = mat.length;
if (n == 0)
return 0;
int m = mat[0].length;
int count = 0;
// Check each row one by one.
for (int i = 0; i < n; i++) {
boolean isIncreasing = true;
// Check whether the current row is strictly
// increasing.
for (int j = 1; j < m; j++) {
// If the current element is not strictly
// greater than the previous element, the
// row is not increasing.
if (mat[i][j] <= mat[i][j - 1]) {
isIncreasing = false;
break;
}
}
// If the row is increasing, count it.
if (isIncreasing) {
count++;
continue;
}
boolean isDecreasing = true;
// Check whether the current row is strictly
// decreasing.
for (int j = 1; j < m; j++) {
// If the current element is not strictly
// smaller than the previous element, the
// row is not decreasing.
if (mat[i][j] >= mat[i][j - 1]) {
isDecreasing = false;
break;
}
}
// If the row is decreasing, count it.
if (isDecreasing)
count++;
}
return count;
}
public static void main(String[] args)
{
int[][] mat
= { { 1, 2, 3 }, { 6, 5, 4 }, { 8, 7, 9 } };
System.out.println(sortedCount(mat));
}
}
def sortedCount(mat):
n = len(mat)
if n == 0:
return 0
m = len(mat[0])
count = 0
# Check each row one by one.
for i in range(n):
isIncreasing = True
# Check whether the current row is strictly increasing.
for j in range(1, m):
# If the current element is not strictly greater
# than the previous element, the row is not increasing.
if mat[i][j] <= mat[i][j - 1]:
isIncreasing = False
break
# If the row is increasing, count it.
if isIncreasing:
count += 1
continue
isDecreasing = True
# Check whether the current row is strictly decreasing.
for j in range(1, m):
# If the current element is not strictly smaller
# than the previous element, the row is not decreasing.
if mat[i][j] >= mat[i][j - 1]:
isDecreasing = False
break
# If the row is decreasing, count it.
if isDecreasing:
count += 1
return count
# Driver Code
if __name__ == "__main__":
mat = [
[1, 2, 3],
[6, 5, 4],
[8, 7, 9]
]
print(sortedCount(mat))
using System;
class GFG {
static int sortedCount(int[, ] mat)
{
int n = mat.GetLength(0);
if (n == 0)
return 0;
int m = mat.GetLength(1);
int count = 0;
// Check each row one by one.
for (int i = 0; i < n; i++) {
bool isIncreasing = true;
// Check whether the current row is strictly
// increasing.
for (int j = 1; j < m; j++) {
// If the current element is not strictly
// greater than the previous element, the
// row is not increasing.
if (mat[i, j] <= mat[i, j - 1]) {
isIncreasing = false;
break;
}
}
// If the row is increasing, count it.
if (isIncreasing) {
count++;
continue;
}
bool isDecreasing = true;
// Check whether the current row is strictly
// decreasing.
for (int j = 1; j < m; j++) {
// If the current element is not strictly
// smaller than the previous element, the
// row is not decreasing.
if (mat[i, j] >= mat[i, j - 1]) {
isDecreasing = false;
break;
}
}
// If the row is decreasing, count it.
if (isDecreasing)
count++;
}
return count;
}
static void Main()
{
int[, ] mat
= { { 1, 2, 3 }, { 6, 5, 4 }, { 8, 7, 9 } };
Console.WriteLine(sortedCount(mat));
}
}
function sortedCount(mat)
{
let n = mat.length;
if (n === 0)
return 0;
let m = mat[0].length;
let count = 0;
// Check each row one by one.
for (let i = 0; i < n; i++) {
let isIncreasing = true;
// Check whether the current row is strictly
// increasing.
for (let j = 1; j < m; j++) {
// If the current element is not strictly
// greater than the previous element, the row is
// not increasing.
if (mat[i][j] <= mat[i][j - 1]) {
isIncreasing = false;
break;
}
}
// If the row is increasing, count it.
if (isIncreasing) {
count++;
continue;
}
let isDecreasing = true;
// Check whether the current row is strictly
// decreasing.
for (let j = 1; j < m; j++) {
// If the current element is not strictly
// smaller than the previous element, the row is
// not decreasing.
if (mat[i][j] >= mat[i][j - 1]) {
isDecreasing = false;
break;
}
}
// If the row is decreasing, count it.
if (isDecreasing)
count++;
}
return count;
}
// Driver Code
let mat = [ [ 1, 2, 3 ], [ 6, 5, 4 ], [ 8, 7, 9 ] ];
console.log(sortedCount(mat));
Output
2
Check Both Increasing and Decreasing Order Simultaneously - O(m * n) Time and O(1) Space
The idea is to traverse each row and simultaneously check whether it is strictly increasing or strictly decreasing using two boolean flags.
- Initialize count = 0.
- Traverse each row and set isIncreasing and isDecreasing to true.
- Compare each element with the previous element and update both flags.
- If both flags become false, stop checking the current row.
- If either flag is true after complete traversal of a row, increment count.
- Return count.
#include <bits/stdc++.h>
using namespace std;
int sortedCount(vector<vector<int>> &mat)
{
int n = mat.size();
if (n == 0)
return 0;
int m = mat[0].size();
int count = 0;
// Check each row one by one.
for (int i = 0; i < n; i++)
{
// Initially, the row can be either increasing
// or decreasing.
bool isIncreasing = true;
bool isDecreasing = true;
// Compare every element with the previous element.
for (int j = 1; j < m; j++)
{
// If the current element is not strictly greater
// than the previous element, the row is not increasing.
if (mat[i][j] <= mat[i][j - 1])
isIncreasing = false;
// If the current element is not strictly smaller
// than the previous element, the row is not decreasing.
if (mat[i][j] >= mat[i][j - 1])
isDecreasing = false;
// If both conditions fail, the row cannot be
// sorted in either direction.
if (!isIncreasing && !isDecreasing)
break;
}
// If the row is either strictly increasing
// or strictly decreasing, count it.
if (isIncreasing || isDecreasing)
count++;
}
return count;
}
int main()
{
vector<vector<int>> mat = {{1, 2, 3}, {6, 5, 4}, {8, 7, 9}};
cout << sortedCount(mat) << endl;
return 0;
}
class GFG {
static int sortedCount(int[][] mat)
{
int n = mat.length;
if (n == 0)
return 0;
int m = mat[0].length;
int count = 0;
// Check each row one by one.
for (int i = 0; i < n; i++) {
// Initially, the row can be either increasing
// or decreasing.
boolean isIncreasing = true;
boolean isDecreasing = true;
// Compare every element with the previous
// element.
for (int j = 1; j < m; j++) {
// If the current element is not strictly
// greater than the previous element, the
// row is not increasing.
if (mat[i][j] <= mat[i][j - 1])
isIncreasing = false;
// If the current element is not strictly
// smaller than the previous element, the
// row is not decreasing.
if (mat[i][j] >= mat[i][j - 1])
isDecreasing = false;
// If both conditions fail, the row cannot
// be sorted in either direction.
if (!isIncreasing && !isDecreasing)
break;
}
// If the row is either strictly increasing
// or strictly decreasing, count it.
if (isIncreasing || isDecreasing)
count++;
}
return count;
}
public static void main(String[] args)
{
int[][] mat
= { { 1, 2, 3 }, { 6, 5, 4 }, { 8, 7, 9 } };
System.out.println(sortedCount(mat));
}
}
def sortedCount(mat):
n = len(mat)
if n == 0:
return 0
m = len(mat[0])
count = 0
# Check each row one by one.
for i in range(n):
# Initially, the row can be either increasing
# or decreasing.
isIncreasing = True
isDecreasing = True
# Compare every element with the previous element.
for j in range(1, m):
# If the current element is not strictly greater
# than the previous element, the row is not increasing.
if mat[i][j] <= mat[i][j - 1]:
isIncreasing = False
# If the current element is not strictly smaller
# than the previous element, the row is not decreasing.
if mat[i][j] >= mat[i][j - 1]:
isDecreasing = False
# If both conditions fail, the row cannot be
# sorted in either direction.
if not isIncreasing and not isDecreasing:
break
# If the row is either strictly increasing
# or strictly decreasing, count it.
if isIncreasing or isDecreasing:
count += 1
return count
# Driver Code
if __name__ == "__main__":
mat = [
[1, 2, 3],
[6, 5, 4],
[8, 7, 9]
]
print(sortedCount(mat))
using System;
class GFG {
static int sortedCount(int[, ] mat)
{
int n = mat.GetLength(0);
if (n == 0)
return 0;
int m = mat.GetLength(1);
int count = 0;
// Check each row one by one.
for (int i = 0; i < n; i++) {
// Initially, the row can be either increasing
// or decreasing.
bool isIncreasing = true;
bool isDecreasing = true;
// Compare every element with the previous
// element.
for (int j = 1; j < m; j++) {
// If the current element is not strictly
// greater than the previous element, the
// row is not increasing.
if (mat[i, j] <= mat[i, j - 1])
isIncreasing = false;
// If the current element is not strictly
// smaller than the previous element, the
// row is not decreasing.
if (mat[i, j] >= mat[i, j - 1])
isDecreasing = false;
// If both conditions fail, the row cannot
// be sorted in either direction.
if (!isIncreasing && !isDecreasing)
break;
}
// If the row is either strictly increasing
// or strictly decreasing, count it.
if (isIncreasing || isDecreasing)
count++;
}
return count;
}
static void Main()
{
int[, ] mat
= { { 1, 2, 3 }, { 6, 5, 4 }, { 8, 7, 9 } };
Console.WriteLine(sortedCount(mat));
}
}
function sortedCount(mat)
{
let n = mat.length;
if (n === 0)
return 0;
let m = mat[0].length;
let count = 0;
// Check each row one by one.
for (let i = 0; i < n; i++) {
// Initially, the row can be either increasing
// or decreasing.
let isIncreasing = true;
let isDecreasing = true;
// Compare every element with the previous element.
for (let j = 1; j < m; j++) {
// If the current element is not strictly
// greater than the previous element, the row is
// not increasing.
if (mat[i][j] <= mat[i][j - 1])
isIncreasing = false;
// If the current element is not strictly
// smaller than the previous element, the row is
// not decreasing.
if (mat[i][j] >= mat[i][j - 1])
isDecreasing = false;
// If both conditions fail, the row cannot be
// sorted in either direction.
if (!isIncreasing && !isDecreasing)
break;
}
// If the row is either strictly increasing
// or strictly decreasing, count it.
if (isIncreasing || isDecreasing)
count++;
}
return count;
}
// Driver Code
let mat = [ [ 1, 2, 3 ], [ 6, 5, 4 ], [ 8, 7, 9 ] ];
console.log(sortedCount(mat));
Output
2