The Palindrome Pattern
Submissions: 254   Accuracy:


  Difficulty: Easy   Marks: 2

Given a 2-D integer array A of order NXN, consisting of only zeroes and ones, find the row or column whose all elements form a palindrome. If more than one palindrome is found, the palindromes found in rows will have priority over the ones found in columns and within rows and columns, the one with lesser index gets the highest priority. Print the palindrome with the highest priority along with it's index and indication of it being a row or column.


First line of input is an integer T, denoting the number of test cases. Each test case has two lines of input. First line consists of an integer N, denoting order of matrix A. Second line consists of NXN elements of the matrix A.


For each test case, there is a single line of output comprising of two parts. First, the index of either row or column(0 based indexing) , followed by either of the two characters 'R' or 'C', representing whether the palindrome was found in a row or column. All the characters printed as output should be space separated. If no palindrome is found, print -1 as the ouput in new line.




A[i][j]=0 or 1, where i and j denote row and column indices of the matrix respectively.



1 0 0 0 1 0 1 1 0
1 0 1 0
0 0 1 0 1 1 0 1 0 1 1 1 1 1 0 1
1 0 0 0 1 1 0 1 0


1 R
0 C
2 C


In the first test case, 0-1-0 is a palindrome occuring in a row having index 1.

In the second test case, 1-1 occurs before 0-0 in the 0th column. And there is no palindrome in the two rows.

There is no palindrome in either any row or any column. Hence, -1 is printed.

In the third test case, 0-1-0 occurs in the second column.

** For More Input/Output Examples Use 'Expected Output' option **

Author: Anushree_20

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.