Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix
Last Updated :
09 Sep, 2022
Given a matrix of order n*n, the task is to find the maximum value of |i-j| such that Aij = 0. Given matrix must contain at least one 0.
Examples:
Input: matrix[][] =
{{2, 3, 0},
{0, 2, 0},
{0, 1, 1}}
Output: 2
mat(0, 2) has a value 0 and difference of index is maximum i.e. 2.
Input: matrix[][] =
{{2, 3, 4},
{0, 2, 0},
{6, 1, 1}}
Output: 1
Approach:
For finding the maximum value of |i-j| such that Aij = 0, traverse the whole matrix and for each occurrence of zero calculate the mod of (i-j) and store it corresponding to same position in an auxiliary matrix. At last, find the maximum value from the auxiliary matrix.
Apart from using an auxiliary matrix, the maximum value of |i-j| can be stored in a variable and can be updated while its calculation. this will save the extra use of space.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define n 4
using namespace std;
int calculateDiff( int matrix[][n])
{
int result = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (matrix[i][j] == 0)
result = max(result, abs (i - j));
}
}
return result;
}
int main()
{
int matrix[n][n] = { { 2, 3, 0, 1 },
{ 0, 2, 0, 1 },
{ 0, 1, 1, 3 },
{ 1, 2, 3, 3 } };
cout << calculateDiff(matrix);
return 0;
}
|
Java
import java.math.*;
class GFG {
static int n = 4 ;
static int calculateDiff( int matrix[][])
{
int result = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
if (matrix[i][j] == 0 )
result = Math.max(result, Math.abs(i - j));
}
}
return result;
}
public static void main(String args[])
{
int matrix[][] = new int [][] {{ 2 , 3 , 0 , 1 },
{ 0 , 2 , 0 , 1 },
{ 0 , 1 , 1 , 3 },
{ 1 , 2 , 3 , 3 } };
System.out.println(calculateDiff(matrix));
}
}
|
Python3
def calculateDiff(matrix, n):
result = 0
for i in range ( 0 , n):
for j in range ( 0 , n):
if (matrix[i][j] = = 0 ):
result = max (result, abs (i - j))
return result
if __name__ = = '__main__' :
matrix = [[ 2 , 3 , 0 , 1 ],
[ 0 , 2 , 0 , 1 ],
[ 0 , 1 , 1 , 3 ],
[ 1 , 2 , 3 , 3 ]]
n = len (matrix)
print (calculateDiff(matrix, n))
|
C#
using System;
class GFG
{
static int n = 4;
static int calculateDiff( int [,]matrix)
{
int result = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (matrix[i, j] == 0)
result = Math.Max(result,
Math.Abs(i - j));
}
}
return result;
}
static void Main()
{
int [,]matrix = new int [,]
{
{ 2, 3, 0, 1 },
{ 0, 2, 0, 1 },
{ 0, 1, 1, 3 },
{ 1, 2, 3, 3 }
};
Console.WriteLine(calculateDiff(matrix));;
}
}
|
PHP
<?php
function calculateDiff( $matrix )
{
$n = 4;
$result = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
if ( $matrix [ $i ][ $j ] == 0)
$result = max( $result ,
abs ( $i - $j ));
}
}
return $result ;
}
$matrix = array ( array ( 2, 3, 0, 1 ),
array ( 0, 2, 0, 1 ),
array ( 0, 1, 1, 3 ),
array ( 1, 2, 3, 3 ));
echo calculateDiff( $matrix );
|
Javascript
<script>
var n = 4
function calculateDiff(matrix)
{
var result = 0;
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < n; j++) {
if (matrix[i][j] == 0)
result = Math.max(result, Math.abs(i - j));
}
}
return result;
}
var matrix = [ [ 2, 3, 0, 1 ],
[ 0, 2, 0, 1 ],
[ 0, 1, 1, 3 ],
[ 1, 2, 3, 3 ] ];
document.write( calculateDiff(matrix));
</script>
|
Time complexity: O(n^2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...