Find the missing number in range [1, N*M+1] represented as Matrix of size N*M
Given an N x M matrix mat[][] where all elements are natural numbers starting from 1 and are continuous except 1 element, find that element.
Examples:
Input: mat[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 11, 12, 13}}
N = 3, M = 4
Output: 10
Explanation: Missing number is 10 at row no 3.
Input: mat[][] = {{1, 2, 3},
{5, 6, 7},
{8, 9, 10}}
N = 3, M = 3
Output: 4
Naive Approach:
The naive approach to solve the problem is to push entire matrix elements to vector and sort the vector in ascending order. Now loop from 1 to M*N and check if it exists in the vector or not. Use binary search to find out their existence. If an integer does not exist, then that that is the answer.
Algorithm:
1. Create a function to find the missing element in matrix, that takes matrix (2D array) and its dimensions (N and M) as input.
2. Create a vector v to store all elements of matrix.
3. Traverse the matrix in row-major order and push each element to vector v.
4. Sort the vector v in ascending order.
5. Loop through all elements from 1 to N*M:
a. Find the position of current element i in the vector v using binary search.
b. If the position of i is equal to the size of vector v or the element at that position is not i:
i. Then i is the missing element, so return i.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define Max 1000
int findMissing( int mat[][Max], int N, int M) {
vector< int > v;
for ( int i=0; i<N; i++) {
for ( int j=0; j<M; j++) {
v.push_back(mat[i][j]);
}
}
sort(v.begin(), v.end());
for ( int i=1; i<=N*M; i++) {
int pos = lower_bound(v.begin(), v.end(), i) - v.begin();
if (pos == v.size() || v[pos] != i) {
return i;
}
}
}
int main() {
int N = 3;
int M = 4;
int Mat[][Max] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 11, 12, 13 } };
int missing = findMissing(Mat, N, M);
cout << missing << endl;
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Vector;
class MissingElementInMatrix {
static final int Max = 1000 ;
static int findMissing( int [][] mat, int N, int M) {
Vector<Integer> v = new Vector<Integer>();
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < M; j++) {
v.add(mat[i][j]);
}
}
Arrays.sort(v.toArray());
for ( int i = 1 ; i <= N * M; i++) {
int pos = Arrays.binarySearch(v.toArray(), i);
if (pos < 0 ) {
return i;
}
}
return - 1 ;
}
public static void main(String[] args) {
int N = 3 ;
int M = 4 ;
int [][] Mat = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 11 , 12 , 13 } };
int missing = findMissing(Mat, N, M);
System.out.println(missing);
}
}
|
Python3
def find_missing(matrix, N, M):
v = []
for i in range (N):
for j in range (M):
v.append(matrix[i][j])
v.sort()
for i in range ( 1 , N * M + 1 ):
pos = v.index(i) if i in v else len (v)
if pos = = len (v) or v[pos] ! = i:
return i
if __name__ = = "__main__" :
N = 3
M = 4
Mat = [[ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 11 , 12 , 13 ]]
missing = find_missing(Mat, N, M)
print (missing)
|
C#
using System;
using System.Collections.Generic;
namespace Geek
{
class GFG
{
static int FindMissing( int [][] mat, int N, int M)
{
List< int > v = new List< int >();
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
v.Add(mat[i][j]);
}
}
v.Sort();
for ( int i = 1; i <= N * M; i++)
{
int pos = v.BinarySearch(i);
if (pos < 0)
{
return i;
}
}
return -1;
}
static void Main( string [] args)
{
int N = 3;
int M = 4;
int [][] Mat = new int [][] {
new int [] { 1, 2, 3, 4 },
new int [] { 5, 6, 7, 8 },
new int [] { 9, 11, 12, 13 }
};
int missing = FindMissing(Mat, N, M);
Console.WriteLine(missing);
Console.ReadLine();
}
}
}
|
Javascript
function findMissing(mat, N, M) {
const arr = [];
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
arr.push(mat[i][j]);
}
}
arr.sort((a, b) => a - b);
for (let i = 1; i <= N * M; i++) {
const pos = arr.findIndex((element) => element >= i);
if (pos === -1 || arr[pos] !== i) {
return i;
}
}
}
function main() {
const N = 3;
const M = 4;
const Mat = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 11, 12, 13]
];
const missing = findMissing(Mat, N, M);
console.log(missing);
}
main();
|
Time Complexity: O( (N*M)*log(N*M) ) because sort function has been called. Also searching each element from 1 to N*M using lower_bound takes O( (N*M)*log(N*M) ) time. So overall complexity is O( (N*M)*log(N*M) ). Here, N and M are rows and columns respectively.
Space Complexity: O( (N*M) ) as vector v has been created which takes O( (N*M) ) space. Here, N and M are rows and columns respectively.
Approach: The problem can be solved by checking multiples of M at the last element of each row of the matrix, if it does not match then the missing element is in that row. Apply linear search and find it.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
#define Max 1000
int check( int Mat[][Max], int N, int M)
{
if (Mat[0][0] != 1)
return 1;
int X = Mat[0][0] + M - 1;
for ( int i = 0; i < N; i++) {
if (Mat[i][M - 1] == X)
X = X + M;
else {
int Y = X - M + 1;
int j = 0;
while (Y <= X) {
if (Mat[i][j] == Y) {
Y++;
j++;
}
else
return Y;
}
}
}
}
int main()
{
int N = 3;
int M = 4;
int Mat[][Max] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 11, 12, 13 } };
cout << check(Mat, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int check( int Mat[][], int N, int M)
{
if (Mat[ 0 ][ 0 ] != 1 )
return 1 ;
int X = Mat[ 0 ][ 0 ] + M - 1 ;
for ( int i = 0 ; i < N; i++) {
if (Mat[i][M - 1 ] == X)
X = X + M;
else {
int Y = X - M + 1 ;
int j = 0 ;
while (Y <= X) {
if (Mat[i][j] == Y) {
Y++;
j++;
}
else
return Y;
}
}
}
return 0 ;
}
public static void main(String[] args)
{
int N = 3 ;
int M = 4 ;
int Mat[][] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 11 , 12 , 13 } };
System.out.print(check(Mat, N, M));
}
}
|
Python3
def check(Mat, N, M):
if (Mat[ 0 ][ 0 ] ! = 1 ):
return 1
X = Mat[ 0 ][ 0 ] + M - 1 ;
for i in range (N):
if (Mat[i][M - 1 ] = = X):
X = X + M
else :
Y = X - M + 1
j = 0 ;
while (Y < = X):
if (Mat[i][j] = = Y):
Y = Y + 1
j = j + 1
else :
return Y
if __name__ = = "__main__" :
N, M = 3 , 4
Mat = [[ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 11 , 12 , 13 ]]
ans = check(Mat, N, M)
print (ans)
|
C#
using System;
class GFG {
static int check( int [,] Mat, int N, int M)
{
if (Mat[0, 0] != 1)
return 1;
int X = Mat[0, 0] + M - 1;
for ( int i = 0; i < N; i++) {
if (Mat[i, M - 1] == X)
X = X + M;
else {
int Y = X - M + 1;
int j = 0;
while (Y <= X) {
if (Mat[i, j] == Y) {
Y++;
j++;
}
else
return Y;
}
}
}
return 0;
}
public static void Main()
{
int N = 3;
int M = 4;
int [, ] Mat = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 11, 12, 13 } };
Console.Write(check(Mat, N, M));
}
}
|
Javascript
<script>
let Max = 1000
function check(Mat, N, M) {
if (Mat[0][0] != 1)
return 1;
let X = Mat[0][0] + M - 1;
for (let i = 0; i < N; i++) {
if (Mat[i][M - 1] == X)
X = X + M;
else {
let Y = X - M + 1;
let j = 0;
while (Y <= X) {
if (Mat[i][j] == Y) {
Y++;
j++;
}
else
return Y;
}
}
}
}
let N = 3;
let M = 4;
let Mat = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 11, 12, 13]];
document.write(check(Mat, N, M));
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Efficient Approach: The efficient approach is to check multiples of the last element in rows using a binary search algorithm. If it does not match then the missing element is in that row or its previous rows and if it matches then the missing element is in the next rows. First, find that row using binary search then apply binary search in that row in the same way to find the column.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define Max 1000
int check( int Mat[][Max], int N, int M)
{
if (Mat[0][0] != 1)
return 1;
int X = Mat[0][0] + M - 1;
int m, l = 0, h = N - 1;
while (l < h) {
m = l + (h - l) / 2;
if (Mat[m][M - 1] == X * (m + 1))
l = m + 1;
else
h = m;
}
int R = h;
X = X * h;
l = 0, h = M - 1;
while (l < h) {
m = l + (h - l) / 2;
if (Mat[R][m] == X + (m + 1))
l = m + 1;
else
h = m;
}
return X + h + 1;
}
int main()
{
int N = 3;
int M = 4;
int Mat[][Max] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 11, 12, 13 } };
cout << check(Mat, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int check( int Mat[][], int N, int M)
{
if (Mat[ 0 ][ 0 ] != 1 )
return 1 ;
int X = Mat[ 0 ][ 0 ] + M - 1 ;
int m, l = 0 , h = N - 1 ;
while (l < h) {
m = l + (h - l) / 2 ;
if (Mat[m][M - 1 ] == X * (m + 1 ))
l = m + 1 ;
else
h = m;
}
int R = h;
X = X * h;
l = 0 ;
h = M - 1 ;
while (l < h) {
m = l + (h - l) / 2 ;
if (Mat[R][m] == X + (m + 1 ))
l = m + 1 ;
else
h = m;
}
return X + h + 1 ;
}
public static void main(String[] args)
{
int N = 3 ;
int M = 4 ;
int Mat[][] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 11 , 12 , 13 } };
System.out.print(check(Mat, N, M));
}
}
|
Python3
def check(Mat, N, M):
if (Mat[ 0 ][ 0 ] ! = 1 ):
return 1 ;
X = Mat[ 0 ][ 0 ] + M - 1 ;
m = 0 ;
l = 0
h = N - 1 ;
while (l < h):
m = l + (h - l) / / 2 ;
if (Mat[m][M - 1 ] = = X * (m + 1 )):
l = m + 1 ;
else :
h = m;
R = h;
X = X * h;
l = 0 ;
h = M - 1 ;
while (l < h):
m = l + (h - l) / / 2 ;
if (Mat[R][m] = = X + (m + 1 )):
l = m + 1 ;
else :
h = m;
return X + h + 1 ;
if __name__ = = '__main__' :
N = 3 ;
M = 4 ;
Mat = [[ 1 , 2 , 3 , 4 ],[ 5 , 6 , 7 , 8 ],[ 9 , 11 , 12 , 13 ]] ;
print (check(Mat, N, M));
|
C#
using System;
public class GFG{
static int check( int [,]Mat, int N, int M)
{
if (Mat[0,0] != 1)
return 1;
int X = Mat[0,0] + M - 1;
int m, l = 0, h = N - 1;
while (l < h) {
m = l + (h - l) / 2;
if (Mat[m,M - 1] == X * (m + 1))
l = m + 1;
else
h = m;
}
int R = h;
X = X * h;
l = 0;
h = M - 1;
while (l < h) {
m = l + (h - l) / 2;
if (Mat[R,m] == X + (m + 1))
l = m + 1;
else
h = m;
}
return X + h + 1;
}
public static void Main(String[] args)
{
int N = 3;
int M = 4;
int [,]Mat = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 11, 12, 13 } };
Console.Write(check(Mat, N, M));
}
}
|
Javascript
<script>
function check(Mat , N , M) {
if (Mat[0][0] != 1)
return 1;
var X = Mat[0][0] + M - 1;
var m, l = 0, h = N - 1;
while (l < h) {
m = l + parseInt((h - l) / 2);
if (Mat[m][M - 1] == X * (m + 1))
l = m + 1;
else
h = m;
}
var R = h;
X = X * h;
l = 0;
h = M - 1;
while (l < h) {
m = l + parseInt((h - l) / 2);
if (Mat[R][m] == X + (m + 1))
l = m + 1;
else
h = m;
}
return X + h + 1;
}
var N = 3;
var M = 4;
var Mat = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 11, 12, 13 ] ];
document.write(check(Mat, N, M));
</script>
|
Time Complexity: O(log N + log M)
Auxiliary Space: O(1)
Last Updated :
29 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...