
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find out the number of consecutive elements in a matrix whose gcd is greater than 1 in Python
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
Example
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]]))
Input
3, 4, [[3, 7, 9, 12], [5, 9, 4, 6], [7, 8, 5, 10]]
Output
3
- Related Articles
- Python Program to find out the number of sets greater than a given value
- C++ program to find nearest integer for which the number and its digits sum gcd is greater than 1
- Number of elements greater than modified mean in matrix in C++
- Python – Sort Matrix by Number of elements greater than its previous element
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Longest Subarray with GCD Greater than 1
- Find Elements Greater Than a Given Number In a Subarray in Java
- Find the number of elements greater than k in a sorted array using C++
- Program to find number of elements in matrix follows row column criteria in Python
- How to find the smallest number greater than x in Python?
- Python - Consecutive Ranges of K greater than N
- 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 Sort Matrix Rows by summation of consecutive difference of elements
- Program to find number of elements in A are strictly less than at least k elements in B in Python

Advertisements