- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given with a matrix of size n x n and a condition that a[i][j] = 0 and the task is to calculate the maximum difference of indices having a[i][j] = 0. So, we can clearly state that there must be at least one zero in a matrix.

**Input**

int matrix[][] = { {0, 1, 1}, {0, 0, 0}, {4, 5, 1}}

**Output** −Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix is −

**Explanation** − we have element 0 at matrix[0][0], matrix[1][0], matrix[1][1] and matrix[1][2]. So the maximum difference of indices will be at matrix[1][0] which is having element 0. So the maximum difference is 1.

**Input**

int matrix[][] = { {0, 1, 1}, {0, 2, 9}, {4, 0, 1}}

**Output** − Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix is −

**Explanation** − we have element 0 at matrix[0][0], matrix[1][0] and matrix[2][1]. So the maximum difference of indices will be at matrix[1][0] and matrix[2][1] which is having element 0. So the maximum difference is 1.

Input the matrix such that it should contain at least one 1 at any indices.

Define the row and column maximum size i.e. size of n x n.

Take a temporary variable that will store the maximum difference value.

Start loop For from 0 till row_size

Inside the loop, start another loop For from 0 till col_size

Check IF matrix[i][j] = 0

Then set max value to be the max value as difference between the indices.

Return the max value

Print the result.

#include <bits/stdc++.h> using namespace std; #define row 3 #define col 3 //find maximum difference int maximum(int matrix[row][col]){ int max_val = 0; for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ if (matrix[i][j] == 0){ max_val = max(max_val, abs(i - j)); } } } return max_val; } int main(){ int matrix[row][col] = { { 1, 2, 0}, { 0, 4, 0}, { 0, 1, 0}}; cout<<"Maximum difference of indices with A[i][j] = 0 is: "<<maximum(matrix); return 0; }

If we run the above code we will get the following output −

Maximum difference of indices with A[i][j] = 0 is: 2

- Related Questions & Answers
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two in C++
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Program to find a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?
- Maximum value of arr[i] % arr[j] for a given array in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++

Advertisements