Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 |
where m = 4, n = 3; then the output will be 3.
The fourth column of the given matrix is [12, 6, 10]. The gcd of these elements is 2, which is greater than 1. Since there are three consecutive elements, the answer is 3.
Approach
To solve this problem, we need to check all possible consecutive subsequences both horizontally (rows) and vertically (columns), then find the maximum length where the GCD is greater than 1.
The algorithm follows these steps ?
- Create a 3D matrix to store GCD values for different ranges
- For each possible starting and ending position, calculate the GCD
- Track the maximum consecutive length where GCD > 1
- Check both row-wise and column-wise sequences
Example
Let us see the implementation to get better understanding ?
from math import gcd
def solve(n, m, input_matrix):
# Create 3D matrix to store GCD values
mat = [[[0 for _ in range(m)] for _ in range(n)] for _ in range(n)]
res = 0
# Check column-wise consecutive elements
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_matrix[i][k]
else:
mat[i][j][k] = gcd(mat[i][j-1][k], input_matrix[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
# Test with the given matrix
matrix = [[3, 7, 9, 12], [5, 9, 4, 6], [7, 8, 5, 10]]
result = solve(3, 4, matrix)
print("Maximum consecutive elements with GCD > 1:", result)
# Let's verify by checking the fourth column manually
fourth_column = [12, 6, 10]
column_gcd = gcd(gcd(fourth_column[0], fourth_column[1]), fourth_column[2])
print(f"Fourth column: {fourth_column}")
print(f"GCD of fourth column: {column_gcd}")
Maximum consecutive elements with GCD > 1: 3 Fourth column: [12, 6, 10] GCD of fourth column: 2
How It Works
The algorithm works by:
-
3D Matrix Storage:
mat[i][j][k]stores the GCD of elements from row i to row j in column k - Dynamic GCD Calculation: For consecutive rows, it calculates GCD incrementally
- Consecutive Counting: Tracks the length of consecutive elements whose GCD is greater than 1
- Maximum Tracking: Keeps track of the maximum consecutive length found
Key Points
- The algorithm checks both horizontal and vertical consecutive sequences
- GCD is calculated incrementally to optimize performance
- The solution handles edge cases where single elements have GCD > 1
- Time complexity is O(n² × m × log(max_element)) due to GCD calculations
Conclusion
This solution efficiently finds the maximum number of consecutive elements in a matrix whose GCD is greater than 1. The algorithm uses dynamic programming with GCD calculations to check all possible consecutive subsequences in both horizontal and vertical directions.
