- 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

Suppose we are given a matrix that contains n rows and m columns. We have to find out the largest number of consecutive elements in the matrix where the gcd of the elements is greater than 1. The consecutive elements can either lie horizontally or vertically in the matrix.

So, if the input is like

3 | 7 | 9 | 12 |

5 | 9 | 4 | 6 |

7 | 8 | 5 | 10 |

and m = 4, n = 3; then the output will be 3.

The fourth column of the given matrix is 12, 6, 10. The gcd of the elements of this column is 2. As there are three elements, the answer is 3.

To solve this, we will follow these steps −

- mat := a new 3d list of dimensions m x n x n
- res := 0
- for i in range 0 to n, do
- for j in range i to n, do
- gcd_temp := 0
- x := 0
- for k in range 0 to m, do
- if i is same as j, then
- mat[i, j, k] := input_list[i, k]

- otherwise,
- mat[i, j, k] = gcd of elements(mat[i, j-1, k], input_list[j, k])

- gcd_temp = gcd of elements (gcd_temp, mat[i, j, k])
- if gcd_temp > 1, then
- x := x + j - i + 1

- otherwise,
- res := maximum of res, x
- if mat[i, j, k] > 1, then
- gcd_temp := mat[i, j, k]
- x := j - i + 1

- if i is same as j, then

- for j in range i to n, do
- res := maximum of res, x

- for i in range 0 to n, do
- return res

Let us see the following implementation to get better understanding −

from math import gcd def solve(n, m, input_list): mat = [[[0] *m] *n] *n res = 0 for i in range(n): for j in range(i, n): gcd_temp = 0 x = 0 for k in range(m): if i == j: mat[i][j][k] = input_list[i][k] else: mat[i][j][k] = gcd(mat[i][j-1][k], input_list[j][k]) gcd_temp = gcd(gcd_temp, mat[i][j][k]) if gcd_temp > 1: x += j - i + 1 else: res = max(res,x) if mat[i][j][k] > 1: gcd_temp = mat[i][j][k] x = j - i + 1 res = max(res,x) return res print(solve(3, 4, [[3, 7, 9, 12], [5, 9, 4, 6], [7, 8, 5, 10]]))

3, 4, [[3, 7, 9, 12], [5, 9, 4, 6], [7, 8, 5, 10]]

3

- Related Questions & Answers
- Python Program to find out the number of sets greater than a given value
- Number of elements greater than modified mean in matrix in C++
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Python – Sort Matrix by Number of elements greater than its previous element
- Python - Consecutive Ranges of K greater than N
- How to find the smallest number greater than x in Python?
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- Program to count number of fraction pairs whose sum is 1 in python
- Program to find out the number of submatrices from a matrix where the sum of elements is equal to a specific value in C++
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Python Program to find out the determinant of a given special matrix
- Program to count number of consecutive lists whose sum is n in C++
- How to convert values greater than a threshold into 1 in column of a matrix in R?
- Find the Number of segments where all elements are greater than X using C++
- Count of subarrays whose maximum element is greater than k in C++

Advertisements