📌  相关文章
📜  索引 (i, j) 的最大差值使得给定矩阵中的 A[i][j] = 0

📅  最后修改于: 2022-05-13 01:57:24.718000             🧑  作者: Mango

索引 (i, j) 的最大差值使得给定矩阵中的 A[i][j] = 0

给定一个 n*n 阶矩阵,任务是找到 |ij| 的最大值使得 Aij = 0。给定的矩阵必须至少包含一个 0。
例子:

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

方法:求|ij|的最大值使得 Aij = 0,遍历整个矩阵,对于每次出现的零,计算 (ij) 的 mod 并将其存储在辅助矩阵中对应的相同位置。最后,从辅助矩阵中找到最大值。
除了使用辅助矩阵,|ij| 的最大值可以存储在变量中,并且可以在计算时更新。这将节省额外的空间使用。
下面是上述方法的实现:

C++
// CPP for maximum |i-j| such that Aij = 0
#include 
#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


Javascript


输出:
2

时间复杂度: O(n^2)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程