Find the position of the given row in a 2-D array
Last Updated :
07 Dec, 2022
Given a matrix mat[][] of size m * n which is sorted in a row-wise fashion and an array row[], the task is to check if any row in the matrix is equal to the given array row[].
Examples:
Input: mat[][] = {
{1, 1, 2, 3, 1},
{2, 1, 3, 3, 2},
{2, 4, 5, 8, 3},
{4, 5, 5, 8, 3},
{8, 7, 10, 13, 6}}
row[] = {4, 5, 5, 8, 3}
Output: 4
4th row is equal to the given array.
Input: mat[][] = {
{0, 0, 1, 0},
{10, 9, 22, 23},
{40, 40, 40, 40},
{43, 44, 55, 68},
{81, 73, 100, 132},
{100, 75, 125, 133}}
row[] = {10, 9, 22, 23}
Output: 2
Naive approach:
Similar to a linear search on a 1-D array, perform the linear search on the matrix and compare every row of the matrix with the given array. If some row matches with the array, print its row number else print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int m = 6, n = 4;
int linearCheck( int ar[][n], int arr[])
{
for ( int i = 0; i < m; i++) {
bool matched = true ;
for ( int j = 0; j < n; j++) {
if (ar[i][j] != arr[j]) {
matched = false ;
break ;
}
}
if (matched)
return i + 1;
}
return -1;
}
int main()
{
int mat[m][n] = { { 0, 0, 1, 0 },
{ 10, 9, 22, 23 },
{ 40, 40, 40, 40 },
{ 43, 44, 55, 68 },
{ 81, 73, 100, 132 },
{ 100, 75, 125, 133 } };
int row[n] = { 10, 9, 22, 23 };
cout << linearCheck(mat, row);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int m = 6 , n = 4 ;
static int linearCheck( int ar[][], int arr[])
{
for ( int i = 0 ; i < m; i++)
{
boolean matched = true ;
for ( int j = 0 ; j < n; j++)
{
if (ar[i][j] != arr[j])
{
matched = false ;
break ;
}
}
if (matched)
return i + 1 ;
}
return - 1 ;
}
public static void main (String[] args)
{
int mat[][] = { { 0 , 0 , 1 , 0 },
{ 10 , 9 , 22 , 23 },
{ 40 , 40 , 40 , 40 },
{ 43 , 44 , 55 , 68 },
{ 81 , 73 , 100 , 132 },
{ 100 , 75 , 125 , 133 } };
int row[] = { 10 , 9 , 22 , 23 };
System.out.println (linearCheck(mat, row));
}
}
|
Python3
m, n = 6 , 4 ;
def linearCheck(ar, arr):
for i in range (m):
matched = True ;
for j in range (n):
if (ar[i][j] ! = arr[j]):
matched = False ;
break ;
if (matched):
return i + 1 ;
return - 1 ;
if __name__ = = "__main__" :
mat = [
[ 0 , 0 , 1 , 0 ],
[ 10 , 9 , 22 , 23 ],
[ 40 , 40 , 40 , 40 ],
[ 43 , 44 , 55 , 68 ],
[ 81 , 73 , 100 , 132 ],
[ 100 , 75 , 125 , 133 ]
];
row = [ 10 , 9 , 22 , 23 ];
print (linearCheck(mat, row));
|
C#
using System;
class GFG
{
static int m = 6;
static int n = 4;
static int linearCheck( int [,]ar, int []arr)
{
for ( int i = 0; i < m; i++)
{
bool matched = true ;
for ( int j = 0; j < n; j++)
{
if (ar[i,j] != arr[j])
{
matched = false ;
break ;
}
}
if (matched)
return i + 1;
}
return -1;
}
static public void Main ()
{
int [,]mat = { { 0, 0, 1, 0 },
{ 10, 9, 22, 23 },
{ 40, 40, 40, 40 },
{ 43, 44, 55, 68 },
{ 81, 73, 100, 132 },
{ 100, 75, 125, 133 } };
int []row = { 10, 9, 22, 23 };
Console.Write(linearCheck(mat, row));
}
}
|
Javascript
<script>
let m = 6, n = 4;
function linearCheck(ar, arr)
{
for (let i = 0; i < m; i++)
{
let matched = true ;
for (let j = 0; j < n; j++)
{
if (ar[i][j] != arr[j])
{
matched = false ;
break ;
}
}
if (matched)
return i + 1;
}
return -1;
}
let mat = [ [ 0, 0, 1, 0 ],
[ 10, 9, 22, 23 ],
[ 40, 40, 40, 40 ],
[ 43, 44, 55, 68 ],
[ 81, 73, 100, 132 ],
[ 100, 75, 125, 133 ] ];
let row = [ 10, 9, 22, 23 ];
document.write(linearCheck(mat, row));
</script>
|
Time Complexity: O(m * n)
Auxiliary Space: O(1)
Efficient approach:
Since the matrix is sorted in a row-wise fashion, we can use binary search similar to what we do in a 1-D array. It is necessary for the array to be sorted in a row-wise fashion.
Below are the steps to find a row in the matrix using binary search,
- Compare arr[] with the middle row.
- If arr[] matches entirely with the middle row, we return the mid index.
- Else If arr[] is greater than the mid-row(there exists at least one j, 0<=j<n such that ar[mid][j]<arr[j]), then arr[] can only lie in right half subarray after the mid-row. So we check in the bottom half.
- Else (arr[] is smaller), we check in the upper half.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int m = 6, n = 4;
int compareRow( int a1[], int a2[])
{
for ( int i = 0; i < n; i++) {
if (a1[i] < a2[i])
return 1;
else if (a1[i] > a2[i])
return -1;
}
return 0;
}
int binaryCheck( int ar[][n], int arr[])
{
int l = 0, r = m - 1;
while (l <= r) {
int mid = (l + r) / 2;
int temp = compareRow(ar[mid], arr);
if (temp == 0)
return mid + 1;
else if (temp == 1)
l = mid + 1;
else
r = mid - 1;
}
return -1;
}
int main()
{
int mat[m][n] = { { 0, 0, 1, 0 },
{ 10, 9, 22, 23 },
{ 40, 40, 40, 40 },
{ 43, 44, 55, 68 },
{ 81, 73, 100, 132 },
{ 100, 75, 125, 133 } };
int row[n] = { 10, 9, 22, 23 };
cout << binaryCheck(mat, row);
return 0;
}
|
Java
class GFG
{
static int m = 6 , n = 4 ;
static int compareRow( int a1[], int a2[])
{
for ( int i = 0 ; i < n; i++)
{
if (a1[i] < a2[i])
return 1 ;
else if (a1[i] > a2[i])
return - 1 ;
}
return 0 ;
}
static int binaryCheck( int ar[][], int arr[])
{
int l = 0 , r = m - 1 ;
while (l <= r)
{
int mid = (l + r) / 2 ;
int temp = compareRow(ar[mid], arr);
if (temp == 0 )
return mid + 1 ;
else if (temp == 1 )
l = mid + 1 ;
else
r = mid - 1 ;
}
return - 1 ;
}
public static void main(String[] args)
{
int mat[][] = { { 0 , 0 , 1 , 0 },
{ 10 , 9 , 22 , 23 },
{ 40 , 40 , 40 , 40 },
{ 43 , 44 , 55 , 68 },
{ 81 , 73 , 100 , 132 },
{ 100 , 75 , 125 , 133 } };
int row[] = { 10 , 9 , 22 , 23 };
System.out.println(binaryCheck(mat, row));
}
}
|
Python3
m = 6 ;
n = 4 ;
def compareRow(a1, a2) :
for i in range (n) :
if (a1[i] < a2[i]) :
return 1 ;
elif (a1[i] > a2[i]) :
return - 1 ;
return 0 ;
def binaryCheck(ar, arr) :
l = 0 ; r = m - 1 ;
while (l < = r) :
mid = (l + r) / / 2 ;
temp = compareRow(ar[mid], arr);
if (temp = = 0 ) :
return mid + 1 ;
elif (temp = = 1 ) :
l = mid + 1 ;
else :
r = mid - 1 ;
return - 1 ;
if __name__ = = "__main__" :
mat = [
[ 0 , 0 , 1 , 0 ],
[ 10 , 9 , 22 , 23 ],
[ 40 , 40 , 40 , 40 ],
[ 43 , 44 , 55 , 68 ],
[ 81 , 73 , 100 , 132 ],
[ 100 , 75 , 125 , 133 ]
];
row = [ 10 , 9 , 22 , 23 ];
print (binaryCheck(mat, row));
|
C#
using System;
class GFG
{
static int m = 6, n = 4;
static int compareRow( int []a1, int []a2)
{
for ( int i = 0; i < n; i++)
{
if (a1[i] < a2[i])
return 1;
else if (a1[i] > a2[i])
return -1;
}
return 0;
}
static int binaryCheck( int [,]ar, int []arr)
{
int l = 0, r = m - 1;
while (l <= r)
{
int mid = (l + r) / 2;
int temp = compareRow(GetRow(ar, mid), arr);
if (temp == 0)
return mid + 1;
else if (temp == 1)
l = mid + 1;
else
r = mid - 1;
}
return -1;
}
public static int [] GetRow( int [,] matrix, int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new int [rowLength];
for ( var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
public static void Main(String[] args)
{
int [,]mat = {{ 0, 0, 1, 0 },
{ 10, 9, 22, 23 },
{ 40, 40, 40, 40 },
{ 43, 44, 55, 68 },
{ 81, 73, 100, 132 },
{ 100, 75, 125, 133 }};
int []row = { 10, 9, 22, 23 };
Console.WriteLine(binaryCheck(mat, row));
}
}
|
Javascript
<script>
var m = 6, n = 4;
function compareRow(a1, a2)
{
for ( var i = 0; i < n; i++) {
if (a1[i] < a2[i])
return 1;
else if (a1[i] > a2[i])
return -1;
}
return 0;
}
function binaryCheck(ar, arr)
{
var l = 0, r = m - 1;
while (l <= r) {
var mid = parseInt((l + r) / 2);
var temp = compareRow(ar[mid], arr);
if (temp == 0)
return mid + 1;
else if (temp == 1)
l = mid + 1;
else
r = mid - 1;
}
return -1;
}
var mat = [ [ 0, 0, 1, 0 ],
[ 10, 9, 22, 23 ],
[ 40, 40, 40, 40 ],
[ 43, 44, 55, 68 ],
[ 81, 73, 100, 132 ],
[ 100, 75, 125, 133 ] ];
var row = [10, 9, 22, 23];
document.write( binaryCheck(mat, row));
</script>
|
Time Complexity: O(n * log(m))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...