Given a matrix arr[][] of size MxN, the task is to find the number of contiguous palindromic sequences of odd length in that matrix.
Example:
Input: arr[][] = { { 2, 1, 2 },
{ 1, 1, 1 },
{ 2, 1, 2 }}
Output: 15
Explanation
Contigiuos Palindromic sequences of odd length are:
Row 1: (2), (1), (2), (2, 1, 2) => n(R1) = 4
Row 2: (1), (1), (1), (1, 1, 1) => n(R2) = 4
Row 3: (2), (1), (2), (2, 1, 2) => n(R3) = 4
Column 1: (2, 1, 2) => n(C1) = 1
Column 2: (1, 1, 1) => n(C2) = 1
Column 3: (2, 1, 2) => n(C3) = 1
Therefore,
Total count = n(R1) + n(R2) + n(R3)
+ n(C1) + n(C2) + n(C3)
= 15
Input: arr[][] = { { 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 } }
Output: 65
Approach:
- Create a variable count to store the total number of contiguous palindromic sequences in the matrix
- As each element in the matrix is a contiguous Palindromic sequence of length 1, add the total number of elements in the matrix to the count, i, e,
count += (M*N)
- Then for the sequence of length > 1,
- Iterate through each element of the matrix and count the number of palindromic sequences in each row by comparing the elements to the other elements on it’s left and right
- Similarly count the number of palindromic sequences in each column by comparing the elements to the other elements on its above and below.
- If found, increment the count of found palindromic sequences by 1.
- Print the computed count of palindromic sequences in the end
Below is the implementation of the above approach:
C++
// C++ code to Count the odd length contiguous // Palindromic sequences in the matrix #include <bits/stdc++.h> using namespace std; #define MAX 10 // Function to count the number of // contiguous palindromic sequences in the matrix int countPalindromes(int n, int m, int matrix[MAX][MAX]) { // Add the total number of elements // in the matrix to the count int count = n * m; // Length of possible sequence to be checked // for palindrome horizontally and vertically int length_of_sequence_row; int length_of_sequence_column; // Iterate through each element of the matrix // and count the number of palindromic // sequences in each row and column for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // Find the possible length of sequences // that can be a palindrome length_of_sequence_row = min(j, m - 1 - j); length_of_sequence_column = min(i, n - i - 1); // From i, check if the sequence // formed by elements to its // left and right is // palindrome or not for (int k = 1; k <= length_of_sequence_row; k++) { // if the sequence [i, j-k] to [i, j+k] // is a palindrome, // increment the count by 1 if (matrix[i][j - k] == matrix[i][j + k]) { count++; } else { break; } } // From i, check if the sequence // formed by elements to its // above and below is // palindrome or not for (int k = 1; k <= length_of_sequence_column; k++) { // if the sequence [i-k, j] to [i+k, j] // is a palindrome, // increment the count by 1 if (matrix[i - k][j] == matrix[i + k][j]) { count++; } else { break; } } } } // Return the total count // of the palindromic sequences return count; } // Driver code int main(void) { int m = 3, n = 3; int matrix[MAX][MAX] = { { 2, 1, 2 }, { 1, 1, 1 }, { 2, 1, 2 } }; cout << countPalindromes(n, m, matrix) << endl; return 0; } |
Java
// Java code to Count the odd length contiguous // Palindromic sequences in the matrix class GFG { static final int MAX = 10; // Function to count the number of // contiguous palindromic sequences in the matrix static int countPalindromes(int n, int m, int matrix[][]) { // Add the total number of elements // in the matrix to the count int count = n * m; // Length of possible sequence to be checked // for palindrome horizontally and vertically int length_of_sequence_row; int length_of_sequence_column; // Iterate through each element of the matrix // and count the number of palindromic // sequences in each row and column for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // Find the possible length of sequences // that can be a palindrome length_of_sequence_row = Math.min(j, m - 1 - j); length_of_sequence_column = Math.min(i, n - i - 1); // From i, check if the sequence // formed by elements to its // left and right is // palindrome or not for (int k = 1; k <= length_of_sequence_row; k++) { // if the sequence [i, j-k] to [i, j+k] // is a palindrome, // increment the count by 1 if (matrix[i][j - k] == matrix[i][j + k]) { count++; } else { break; } } // From i, check if the sequence // formed by elements to its // above and below is // palindrome or not for (int k = 1; k <= length_of_sequence_column; k++) { // if the sequence [i-k, j] to [i+k, j] // is a palindrome, // increment the count by 1 if (matrix[i - k][j] == matrix[i + k][j]) { count++; } else { break; } } } } // Return the total count // of the palindromic sequences return count; } // Driver code public static void main(String []args) { int m = 3, n = 3; int matrix[][] = { { 2, 1, 2 }, { 1, 1, 1 }, { 2, 1, 2 } }; System.out.print(countPalindromes(n, m, matrix) +"\n"); } } // This code is contributed by 29AjayKumar ` |
Python3
# Python code to Count the odd length contiguous # Palindromic sequences in the matrix MAX = 10; # Function to count the number of # contiguous palindromic sequences in the matrix def countPalindromes(n, m, matrix): # Add the total number of elements # in the matrix to the count count = n * m; # Length of possible sequence to be checked # for palindrome horizontally and vertically length_of_sequence_row = 0; length_of_sequence_column = 0; # Iterate through each element of the matrix # and count the number of palindromic # sequences in each row and column for i in range(n): for j in range(m): # Find the possible length of sequences # that can be a palindrome length_of_sequence_row = min(j, m - 1 - j); length_of_sequence_column = min(i, n - i - 1); # From i, check if the sequence # formed by elements to its # left and right is # palindrome or not for k in range(1, length_of_sequence_row + 1): # if the sequence [i, j-k] to [i, j+k] # is a palindrome, # increment the count by 1 if (matrix[i][j - k] == matrix[i][j + k]): count += 1; else: break; # From i, check if the sequence # formed by elements to its # above and below is # palindrome or not for k in range(1, length_of_sequence_column + 1): # if the sequence [i-k, j] to [i+k, j] # is a palindrome, # increment the count by 1 if (matrix[i - k][j] == matrix[i + k][j]): count += 1; else: break; # Return the total count # of the palindromic sequences return count; # Driver code if __name__ == '__main__': m = 3; n = 3; matrix = [ 2, 1, 2 ],[ 1, 1, 1 ],[ 2, 1, 2 ]; print(countPalindromes(n, m, matrix)); # This code is contributed by 29AjayKumar |
C#
// C# code to Count the odd length contiguous // Palindromic sequences in the matrix using System; class GFG { static int MAX = 10; // Function to count the number of // contiguous palindromic sequences in the matrix static int countPalindromes(int n, int m, int [,]matrix) { // Add the total number of elements // in the matrix to the count int count = n * m; // Length of possible sequence to be checked // for palindrome horizontally and vertically int length_of_sequence_row; int length_of_sequence_column; // Iterate through each element of the matrix // and count the number of palindromic // sequences in each row and column for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // Find the possible length of sequences // that can be a palindrome length_of_sequence_row = Math.Min(j, m - 1 - j); length_of_sequence_column = Math.Min(i, n - i - 1); // From i, check if the sequence // formed by elements to its // left and right is // palindrome or not for (int k = 1; k <= length_of_sequence_row; k++) { // if the sequence [i, j-k] to [i, j+k] // is a palindrome, // increment the count by 1 if (matrix[i, j - k] == matrix[i, j + k]) { count++; } else { break; } } // From i, check if the sequence // formed by elements to its // above and below is // palindrome or not for (int k = 1; k <= length_of_sequence_column; k++) { // if the sequence [i-k, j] to [i+k, j] // is a palindrome, // increment the count by 1 if (matrix[i - k, j] == matrix[i + k, j]) { count++; } else { break; } } } } // Return the total count // of the palindromic sequences return count; } // Driver code public static void Main() { int m = 3, n = 3; int [,]matrix = { { 2, 1, 2 }, { 1, 1, 1 }, { 2, 1, 2 } }; Console.WriteLine(countPalindromes(n, m, matrix) ); } } // This code is contributed by AnkitRai01 |
15
Time Complexity: O(n*m*max(n, m))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if all the palindromic sub-strings are of odd length
- Sum of all odd length palindromic numbers within the range [L, R]
- Count sequences of length K having each term divisible by its preceding term
- Count sequences of given length having non-negative prefix sums that can be generated by given values
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Print all sequences of given length
- Print all increasing sequences of length k from first n natural numbers
- Number of N length sequences whose product is M
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count all Prime Length Palindromic Substrings
- Count of non-palindromic strings of length M using given N characters
- Count of unique palindromic strings of length X from given string
- Length of the largest subarray with contiguous elements | Set 1
- Queries to find maximum sum contiguous subarrays of given length in a rotating array
- Count of palindromic plus paths in a given Matrix
- Count of sub-sequences which satisfy the given condition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

