Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix
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++
// CPP for maximum |i-j| such that Aij = 0 #include <bits/stdc++.h> #define n 4 using namespace std; // function to return maximum |i-j| such that Aij = 0 int calculateDiff(int matrix[][n]) { int result = 0; // traverse the matrix 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 return result; } // driver program 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
// Java program for maximum |i-j| such that Aij = 0 import java.math.*; class GFG { static int n = 4; // function to return maximum |i-j| such that Aij = 0 static int calculateDiff(int matrix[][]) { int result = 0; // traverse the matrix 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 return result; } // driver program 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
# Python3 program for maximum # |i-j| such that Aij = 0 # function to return maximum # |i-j| such that Aij = 0 def calculateDiff(matrix, n): result = 0 # traverse the matrix 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 # Driver code 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)) # This code is contributed by # Kirti_Mangal |
C#
// C# for maximum |i-j| such that Aij = 0 using System; class GFG { static int n = 4; // function to return maximum |i-j| // such that Aij = 0 static int calculateDiff(int [,]matrix) { int result = 0; // traverse the matrix 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 return result; } // Driver code 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));; } } // This code is contributed by ANKITRAI1 |
PHP
<?php // PHP for maximum |i-j| such that Aij = 0 // function to return maximum |i-j| // such that Aij = 0 function calculateDiff($matrix) { $n = 4; $result = 0; // traverse the matrix 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 return $result; } // Driver Code $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); // This code is contributed // by Akanksha Rai |
2
Time complexity: O(n^2)
Recommended Posts:
- Pair with maximum difference in a Matrix
- Maximum difference of sum of elements in two rows in a matrix
- Find pair with maximum difference in any column of a Matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Count the pairs in an array such that the difference between them and their indices is equal
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Maximum trace possible for any sub-matrix of the given matrix
- Check if a pair with given absolute difference exists in a Matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Maximum XOR value in matrix
- Pair with maximum sum in a Matrix
- Maximum sum of elements from each row in the matrix
- Maximum sum rectangle in a 2D matrix | DP-27
- Maximum path sum in matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



