Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on Matrix
  • Practice Matrix
  • MCQs on Matrix
  • Tutorial on Matrix
  • Matrix Traversal
  • Sorting in Matrix
  • Matrix Rotation
  • Transpose of Matrix
  • Inverse of Matrix
  • Determinant of Matrix
  • Matrix Application
  • Adjoint & Inverse Matrix
  • Sparse Matrix
  • Matrix Exponentiation
Open In App
Next Article:
Find a common element in all rows of a given row-wise sorted matrix
Next article icon

Find a common element in all rows of a given row-wise sorted matrix

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a matrix where every row is sorted in increasing order. Write a function that finds and returns a common element in all rows. If there is no common element, then returns -1. 
Example: 
 

Input: mat[4][5] = { {1, 2, 3, 4, 5},
{2, 4, 5, 8, 10},
{3, 5, 7, 9, 11},
{1, 3, 5, 7, 9},
};
Output: 5


A O(m*n*n) simple solution is to take every element of first row and search it in all other rows, till we find a common element. Time complexity of this solution is O(m*n*n) where m is number of rows and n is number of columns in given matrix. This can be improved to O(m*n*Logn) if we use Binary Search instead of linear search.
We can solve this problem in O(mn) time using the approach similar to merge of Merge Sort. The idea is to start from the last column of every row. If elements at all last columns are same, then we found the common element. Otherwise we find the minimum of all last columns. Once we find a minimum element, we know that all other elements in last columns cannot be a common element, so we reduce last column index for all rows except for the row which has minimum value. We keep repeating these steps till either all elements at current last column don't become same, or a last column index reaches 0.
Below is the implementation of above idea.
 

C++
// A C++ program to find a common element in all rows of a // row wise sorted array #include <bits/stdc++.h> using namespace std;  // Specify number of rows and columns #define M 4 #define N 5  // Returns common element in all rows of mat[M][N]. If there is no // common element, then -1 is returned int findCommon(int mat[M][N]) {     // An array to store indexes of current last column     int column[M];     int min_row; // To store index of row whose current     // last element is minimum      // Initialize current last element of all rows     int i;     for (i = 0; i < M; i++)         column[i] = N - 1;      min_row = 0; // Initialize min_row as first row      // Keep finding min_row in current last column, till either     // all elements of last column become same or we hit first column.     while (column[min_row] >= 0) {         // Find minimum in current last column         for (i = 0; i < M; i++) {             if (mat[i][column[i]] < mat[min_row][column[min_row]])                 min_row = i;         }          // eq_count is count of elements equal to minimum in current last         // column.         int eq_count = 0;          // Traverse current last column elements again to update it         for (i = 0; i < M; i++) {             // Decrease last column index of a row whose value is more             // than minimum.             if (mat[i][column[i]] > mat[min_row][column[min_row]]) {                 if (column[i] == 0)                     return -1;                  column[i] -= 1; // Reduce last column index by 1             }             else                 eq_count++;         }          // If equal count becomes M, return the value         if (eq_count == M)             return mat[min_row][column[min_row]];     }     return -1; }  // Driver Code int main() {     int mat[M][N] = {         { 1, 2, 3, 4, 5 },         { 2, 4, 5, 8, 10 },         { 3, 5, 7, 9, 11 },         { 1, 3, 5, 7, 9 },     };     int result = findCommon(mat);     if (result == -1)         cout << "No common element";     else         cout << "Common element is " << result;     return 0; }  // This code is contributed // by Akanksha Rai 
C
// A C program to find a common element in all rows of a // row wise sorted array #include <stdio.h>  // Specify number of rows and columns #define M 4 #define N 5  // Returns common element in all rows of mat[M][N]. If there is no // common element, then -1 is returned int findCommon(int mat[M][N]) {     // An array to store indexes of current last column     int column[M];     int min_row; // To store index of row whose current     // last element is minimum      // Initialize current last element of all rows     int i;     for (i = 0; i < M; i++)         column[i] = N - 1;      min_row = 0; // Initialize min_row as first row      // Keep finding min_row in current last column, till either     // all elements of last column become same or we hit first column.     while (column[min_row] >= 0) {         // Find minimum in current last column         for (i = 0; i < M; i++) {             if (mat[i][column[i]] < mat[min_row][column[min_row]])                 min_row = i;         }          // eq_count is count of elements equal to minimum in current last         // column.         int eq_count = 0;          // Traverse current last column elements again to update it         for (i = 0; i < M; i++) {             // Decrease last column index of a row whose value is more             // than minimum.             if (mat[i][column[i]] > mat[min_row][column[min_row]]) {                 if (column[i] == 0)                     return -1;                  column[i] -= 1; // Reduce last column index by 1             }             else                 eq_count++;         }          // If equal count becomes M, return the value         if (eq_count == M)             return mat[min_row][column[min_row]];     }     return -1; }  // driver program to test above function int main() {     int mat[M][N] = {         { 1, 2, 3, 4, 5 },         { 2, 4, 5, 8, 10 },         { 3, 5, 7, 9, 11 },         { 1, 3, 5, 7, 9 },     };     int result = findCommon(mat);     if (result == -1)         printf("No common element");     else         printf("Common element is %d", result);     return 0; } 
Java
// A Java program to find a common // element in all rows of a // row wise sorted array  class GFG {     // Specify number of rows and columns     static final int M = 4;     static final int N = 5;      // Returns common element in all rows     // of mat[M][N]. If there is no     // common element, then -1 is     // returned     static int findCommon(int mat[][])     {         // An array to store indexes         // of current last column         int column[] = new int[M];          // To store index of row whose current         // last element is minimum         int min_row;          // Initialize current last element of all rows         int i;         for (i = 0; i < M; i++)             column[i] = N - 1;          // Initialize min_row as first row         min_row = 0;          // Keep finding min_row in current last column, till either         // all elements of last column become same or we hit first column.         while (column[min_row] >= 0) {             // Find minimum in current last column             for (i = 0; i < M; i++) {                 if (mat[i][column[i]] < mat[min_row][column[min_row]])                     min_row = i;             }              // eq_count is count of elements equal to minimum in current last             // column.             int eq_count = 0;              // Traverse current last column elements again to update it             for (i = 0; i < M; i++) {                 // Decrease last column index of a row whose value is more                 // than minimum.                 if (mat[i][column[i]] > mat[min_row][column[min_row]]) {                     if (column[i] == 0)                         return -1;                      // Reduce last column index by 1                     column[i] -= 1;                 }                 else                     eq_count++;             }              // If equal count becomes M,             // return the value             if (eq_count == M)                 return mat[min_row][column[min_row]];         }         return -1;     }      // Driver code     public static void main(String[] args)     {         int mat[][] = { { 1, 2, 3, 4, 5 },                         { 2, 4, 5, 8, 10 },                         { 3, 5, 7, 9, 11 },                         { 1, 3, 5, 7, 9 } };         int result = findCommon(mat);         if (result == -1)             System.out.print("No common element");         else             System.out.print("Common element is " + result);     } }  // This code is contributed by Anant Agarwal. 
Python 3
# Python 3 program to find a common element  # in all rows of a row wise sorted array  # Specify number of rows  # and columns M = 4 N = 5  # Returns common element in all rows  # of mat[M][N]. If there is no common  # element, then -1 is returned def findCommon(mat):      # An array to store indexes of      # current last column     column = [N - 1] * M      min_row = 0 # Initialize min_row as first row      # Keep finding min_row in current last      # column, till either all elements of      # last column become same or we hit first column.     while (column[min_row] >= 0):              # Find minimum in current last column         for i in range(M):             if (mat[i][column[i]] <                  mat[min_row][column[min_row]]):                 min_row = i              # eq_count is count of elements equal          # to minimum in current last column.         eq_count = 0          # Traverse current last column elements         # again to update it         for i in range(M):                          # Decrease last column index of a row              # whose value is more than minimum.             if (mat[i][column[i]] >                  mat[min_row][column[min_row]]):                 if (column[i] == 0):                     return -1                  column[i] -= 1 # Reduce last column                                # index by 1                      else:                 eq_count += 1          # If equal count becomes M, return the value         if (eq_count == M):             return mat[min_row][column[min_row]]     return -1  # Driver Code if __name__ == "__main__":          mat = [[1, 2, 3, 4, 5],            [2, 4, 5, 8, 10],            [3, 5, 7, 9, 11],            [1, 3, 5, 7, 9]]      result = findCommon(mat)     if (result == -1):         print("No common element")     else:         print("Common element is", result)  # This code is contributed by ita_c 
C#
// A C# program to find a common // element in all rows of a // row wise sorted array using System;  class GFG {      // Specify number of rows and columns     static int M = 4;     static int N = 5;      // Returns common element in all rows     // of mat[M][N]. If there is no     // common element, then -1 is     // returned     static int findCommon(int[, ] mat)     {          // An array to store indexes         // of current last column         int[] column = new int[M];          // To store index of row whose         // current last element is minimum         int min_row;          // Initialize current last element         // of all rows         int i;         for (i = 0; i < M; i++)             column[i] = N - 1;          // Initialize min_row as first row         min_row = 0;          // Keep finding min_row in current         // last column, till either all         // elements of last column become         // same or we hit first column.         while (column[min_row] >= 0) {              // Find minimum in current             // last column             for (i = 0; i < M; i++) {                 if (mat[i, column[i]] < mat[min_row, column[min_row]])                     min_row = i;             }              // eq_count is count of elements             // equal to minimum in current             // last column.             int eq_count = 0;              // Traverse current last column             // elements again to update it             for (i = 0; i < M; i++) {                  // Decrease last column index                 // of a row whose value is more                 // than minimum.                 if (mat[i, column[i]] > mat[min_row, column[min_row]]) {                     if (column[i] == 0)                         return -1;                      // Reduce last column index                     // by 1                     column[i] -= 1;                 }                 else                     eq_count++;             }              // If equal count becomes M,             // return the value             if (eq_count == M)                 return mat[min_row,                            column[min_row]];         }          return -1;     }      // Driver code     public static void Main()     {         int[, ] mat = { { 1, 2, 3, 4, 5 },                         { 2, 4, 5, 8, 10 },                         { 3, 5, 7, 9, 11 },                         { 1, 3, 5, 7, 9 } };          int result = findCommon(mat);          if (result == -1)             Console.Write("No common element");         else             Console.Write("Common element is "                           + result);     } }  // This code is contributed by Sam007. 
JavaScript
<script>  // A Javascript program to find a common // element in all rows of a // row wise sorted array              // Specify number of rows and columns     let M = 4;     let N = 5;          // Returns common element in all rows     // of mat[M][N]. If there is no     // common element, then -1 is     // returned     function findCommon(mat)     {         // An array to store indexes         // of current last column         let column=new Array(M);                  // To store index of row whose current         // last element is minimum         let min_row;         // Initialize current last element of all rows         let i;         for (i = 0; i < M; i++)             column[i] = N - 1;                  // Initialize min_row as first row         min_row = 0;            // Keep finding min_row in current          // last column, till either         // all elements of last column become          // same or we hit first column.         while (column[min_row] >= 0) {             // Find minimum in current last column             for (i = 0; i < M; i++) {                 if (mat[i][column[i]] < mat[min_row][column[min_row]])                     min_row = i;             }                // eq_count is count of elements equal to             // minimum in current last             // column.             let eq_count = 0;                // Traverse current last column              // elements again to update it             for (i = 0; i < M; i++) {                 // Decrease last column index of a row whose value is more                 // than minimum.                 if (mat[i][column[i]] > mat[min_row][column[min_row]]) {                     if (column[i] == 0)                         return -1;                        // Reduce last column index by 1                     column[i] -= 1;                 }                 else                     eq_count++;             }                // If equal count becomes M,             // return the value             if (eq_count == M)                 return mat[min_row][column[min_row]];         }         return -1;     }          // Driver Code     let mat = [[1, 2, 3, 4, 5],            [2, 4, 5, 8, 10],            [3, 5, 7, 9, 11],            [1, 3, 5, 7, 9]];     let result = findCommon(mat)     if (result == -1)     {         document.write("No common element");              }     else     {         document.write("Common element is ", result);     }          // This code is contributed by rag2127      </script> 

Output
Common element is 5

Time complexity: O(M x N).
Auxiliary Space: O(M)

Explanation for working of above code 
Let us understand working of above code for following example.
Initially entries in last column array are N-1, i.e., {4, 4, 4, 4} 
    {1, 2, 3, 4, 5}, 
    {2, 4, 5, 8, 10}, 
    {3, 5, 7, 9, 11}, 
    {1, 3, 5, 7, 9},
The value of min_row is 0, so values of last column index for rows with value greater than 5 is reduced by one. So column[] becomes {4, 3, 3, 3}. 
    {1, 2, 3, 4, 5}, 
    {2, 4, 5, 8, 10}, 
    {3, 5, 7, 9, 11}, 
    {1, 3, 5, 7, 9},
The value of min_row remains 0  and value of last column index for rows with value greater than 5 is reduced by one. So column[] becomes {4, 2, 2, 2}. 
    {1, 2, 3, 4, 5}, 
    {2, 4, 5, 8, 10}, 
    {3, 5, 7, 9, 11}, 
    {1, 3, 5, 7, 9},
The value of min_row remains 0 and value of last column index for rows with value greater than 5 is reduced by one. So column[] becomes {4, 2, 1, 2}. 
    {1, 2, 3, 4, 5}, 
    {2, 4, 5, 8, 10}, 
    {3, 5, 7, 9, 11}, 
    {1, 3, 5, 7, 9},
Now all values in current last columns of all rows is same, so 5 is returned.
A Hashing Based Solution 
We can also use hashing. This solution works even if the rows are not sorted. It can be used to print all common elements.
 

Step1:  Create a Hash Table with all key as distinct elements 
of row1. Value for all these will be 0.

Step2:
For i = 1 to M-1
For j = 0 to N-1
If (mat[i][j] is already present in Hash Table)
If (And this is not a repetition in current row.
This can be checked by comparing HashTable value with
row number)
Update the value of this key in HashTable with current
row number

Step3: Iterate over HashTable and print all those keys for
which value = M


 

C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;  // Specify number of rows and columns #define M 4 #define N 5  // Returns common element in all rows of mat[M][N]. If there is no // common element, then -1 is returned int findCommon(int grid[M][N]) {     // A hash map to store count of elements     unordered_map<int, int> cnt;      int i, j;      for (i = 0; i < M; i++) {          // Increment the count of first         // element of the row         cnt[grid[i][0]]++;          // Starting from the second element         // of the current row         for (j = 1; j < N; j++) {              // If current element is different from             // the previous element i.e. it is appearing             // for the first time in the current row             if (grid[i][j] != grid[i][j - 1])                 cnt[grid[i][j]]++;         }     }      // Find element having count equal to number of rows     for (auto ele : cnt) {         if (ele.second == M)             return ele.first;     }      // No such element found     return -1; }  // Driver Code int main() {     int mat[M][N] = {         { 1, 2, 3, 4, 5 },         { 2, 4, 5, 8, 10 },         { 3, 5, 7, 9, 11 },         { 1, 3, 5, 7, 9 },     };     int result = findCommon(mat);     if (result == -1)         cout << "No common element";     else         cout << "Common element is " << result;      return 0; } 
Java
// Java implementation of the approach import java.util.*;  class GFG  {  // Specify number of rows and columns static int M = 4; static int N = 5;  // Returns common element in all rows of mat[M][N]. // If there is no common element, then -1 is returned static int findCommon(int mat[][]) {     // A hash map to store count of elements     HashMap<Integer,              Integer> cnt = new HashMap<Integer,                                         Integer>();      int i, j;      for (i = 0; i < M; i++)      {          // Increment the count of first         // element of the row         if(cnt.containsKey(mat[i][0]))         {             cnt.put(mat[i][0],              cnt.get(mat[i][0]) + 1);         }         else         {             cnt.put(mat[i][0], 1);         }          // Starting from the second element         // of the current row         for (j = 1; j < N; j++)          {              // If current element is different from             // the previous element i.e. it is appearing             // for the first time in the current row             if (mat[i][j] != mat[i][j - 1])                 if(cnt.containsKey(mat[i][j]))                 {                     cnt.put(mat[i][j],                      cnt.get(mat[i][j]) + 1);                 }                 else                 {                     cnt.put(mat[i][j], 1);                 }         }     }      // Find element having count      // equal to number of rows     for (Map.Entry<Integer,                     Integer> ele : cnt.entrySet())     {         if (ele.getValue() == M)             return ele.getKey();     }      // No such element found     return -1; }  // Driver Code public static void main(String[] args)  {     int mat[][] = {{ 1, 2, 3, 4, 5 },                    { 2, 4, 5, 8, 10 },                    { 3, 5, 7, 9, 11 },                    { 1, 3, 5, 7, 9 }};     int result = findCommon(mat);     if (result == -1)         System.out.println("No common element");     else         System.out.println("Common element is " + result);     } }  // This code is contributed by Rajput-Ji 
Python
# Python3 implementation of the approach from collections import defaultdict  # Specify number of rows and columns M = 4 N = 5  # Returns common element in all rows of # mat[M][N]. If there is no # common element, then -1 is returned def findCommon(grid):     global M     global N      # A hash map to store count of elements     cnt = dict()     cnt = defaultdict(lambda: 0, cnt)      i = 0     j = 0      while (i < M ):           # Increment the count of first         # element of the row         cnt[grid[i][0]] = cnt[grid[i][0]] + 1          j = 1                  # Starting from the second element         # of the current row         while (j < N ) :              # If current element is different from             # the previous element i.e. it is appearing             # for the first time in the current row             if (grid[i][j] != grid[i][j - 1]):                 cnt[grid[i][j]] = cnt[grid[i][j]] + 1             j = j + 1         i = i + 1              # Find element having count equal to number of rows     for ele in cnt:         if (cnt[ele] == M):             return ele          # No such element found     return -1  # Driver Code mat = [[1, 2, 3, 4, 5 ],         [2, 4, 5, 8, 10],         [3, 5, 7, 9, 11],         [1, 3, 5, 7, 9 ],]      result = findCommon(mat) if (result == -1):     print("No common element") else:     print("Common element is ", result)  # This code is contributed by Arnab Kundu 
C#
// C# implementation of the approach using System; using System.Collections.Generic;   class GFG  {  // Specify number of rows and columns static int M = 4; static int N = 5;  // Returns common element in all rows of mat[M,N]. // If there is no common element, then -1 is returned static int findCommon(int [,]grid) {     // A hash map to store count of elements     Dictionary<int,                int> cnt = new Dictionary<int,                                           int>();      int i, j;      for (i = 0; i < M; i++)      {          // Increment the count of first         // element of the row         if(cnt.ContainsKey(grid[i, 0]))         {             cnt[grid[i, 0]]= cnt[grid[i, 0]] + 1;         }         else         {             cnt.Add(grid[i, 0], 1);         }          // Starting from the second element         // of the current row         for (j = 1; j < N; j++)          {              // If current element is different from             // the previous element i.e. it is appearing             // for the first time in the current row             if (grid[i, j] != grid[i, j - 1])                 if(cnt.ContainsKey(mat[i, j]))                 {                     cnt[grid[i, j]]= cnt[grid[i, j]] + 1;                 }                 else                 {                     cnt.Add(grid[i, j], 1);                 }         }     }      // Find element having count      // equal to number of rows     foreach(KeyValuePair<int, int> ele in cnt)     {         if (ele.Value == M)             return ele.Key;     }      // No such element found     return -1; }  // Driver Code public static void Main(String[] args)  {     int [,]mat = {{ 1, 2, 3, 4, 5 },                   { 2, 4, 5, 8, 10 },                   { 3, 5, 7, 9, 11 },                   { 1, 3, 5, 7, 9 }};     int result = findCommon(mat);     if (result == -1)         Console.WriteLine("No common element");     else         Console.WriteLine("Common element is " + result);     } }   // This code is contributed by 29AjayKumar 
JavaScript
<script> // Javascript implementation of the approach          // Specify number of rows and columns     let M = 4;     let N = 5;          // Returns common element in all rows of mat[M][N].     // If there is no common element, then -1 is returned     function findCommon(mat)     {         // A hash map to store count of elements         let cnt = new Map();         let i, j;         for (i = 0; i < M; i++)         {             // Increment the count of first             // element of the row             if(cnt.has(mat[i][0]))             {                 cnt.set(mat[i][0],cnt.get(mat[i][0])+1);             }             else             {                 cnt.set(mat[i][0],1);             }                          // Starting from the second element             // of the current row             for (j = 1; j < N; j++)             {                 // If current element is different from                 // the previous element i.e. it is appearing                 // for the first time in the current row                 if (mat[i][j] != mat[i][j - 1])                 {                     if(cnt.has(mat[i][j]))                     {                         cnt.set(mat[i][j], cnt.get(mat[i][j]) + 1);                     }                     else                     {                         cnt.set(mat[i][j], 1);                     }                 }             }         }                  // Find element having count         // equal to number of rows         for( let [key, value] of cnt.entries())         {             if(value == M)                 return key;         }                  // No such element found         return -1;     }          // Driver Code     let mat = [[1, 2, 3, 4, 5 ],         [2, 4, 5, 8, 10],         [3, 5, 7, 9, 11],         [1, 3, 5, 7, 9 ],]     let result = findCommon(mat);     if (result == -1)         document.write("No common element");     else         document.write("Common element is " + result);          //  This code is contributed by avanitrachhadiya2155 </script> 

Output
Common element is 5

Time complexity: O(n*m) under the assumption that search and insert in HashTable take O(1) time

Auxiliary Space: O(n) due to unordered_map.

 Thanks to Nishant for suggesting this solution in a comment below.
Exercise: Given n sorted arrays of size m each, find all common elements in all arrays in O(mn) time.
 


Next Article
Find a common element in all rows of a given row-wise sorted matrix

K

kartik
Improve
Article Tags :
  • Matrix
  • DSA
Practice Tags :
  • Matrix

Similar Reads

    Common elements in all rows of a given matrix
    Given an m x n matrix, find all common elements present in all rows in O(mn) time and one traversal of matrix.Example: Input:mat[4][5] = {{1, 2, 1, 4, 8}, {3, 7, 8, 5, 1}, {8, 7, 7, 3, 1}, {8, 1, 2, 7, 9}, };Output: 1 8 or 8 18 and 1 are present in all rows.A simple solution is to consider every ele
    7 min read
    Find the original matrix when largest element in a row and a column are given
    Given two arrays A[] and B[] of N and M integers respectively. Also given is a N X M binary matrix where 1 indicates that there was a positive integer in the original matrix and 0 indicates that the position is filled with 0 in the original matrix. The task is to form back the original matrix such t
    6 min read
    Find all permuted rows of a given row in a matrix
    Given a matrix mat[][] of order m*n, and an index ind. The task is to find all the rows in the matrix mat[][] which are permutations of rows at index ind.Note: All the elements of a row are distinct.Examples: Input: mat[][] = [[3, 1, 4, 2], [1, 6, 9, 3], [1, 2, 3, 4], [4, 3, 2, 1]] ind = 3 Output: 0
    9 min read
    Find distinct elements common to all rows of a matrix
    Given a n x n matrix. The problem is to find all the distinct elements common to all rows of the matrix. The elements can be printed in any order. Examples: Input : mat[][] = { {2, 1, 4, 3}, {1, 2, 3, 2}, {3, 6, 2, 3}, {5, 2, 5, 3} } Output : 2 3 Input : mat[][] = { {12, 1, 14, 3, 16}, {14, 2, 1, 3,
    15+ min read
    Find row and column pair in given Matrix with equal row and column sum
    Given a matrix Mat of size N x M, the task is to find all the pairs of rows and columns where the sum of elements in the row is equal to the sum of elements in the columns. Examples: Input: M = {{1, 2, 2}, {1, 5, 6}, {3, 8, 9}}Output: {{1, 1}}Explanation: The sum of elements of rows and columns of m
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences