Check whether a binary matrix symbolizes character X

Updated

Given a binary matrix of N x N and N ≥ 3. We need to check whether it symbolizes character ‘X’ or not.

For better understanding see the following examples

Examples :

Input : {{0, 1, 1, 1, 0},
{1, 0, 1, 0, 1},
{1, 1, 0, 1, 1},
{1, 0, 1, 0, 1},
{0, 1, 1, 1, 0}}
Output : YES
Explanation :
All diagonal elements are equal.

Input : {{0, 1, 1, 1, 0},
{1, 0, 1, 0, 1},
{1, 1, 1, 1, 1},
{1, 0, 1, 0, 1},
{0, 1, 1, 1, 0}}
Output : NO
Explanation :
All diagonal elements are not equal.

Input : {{0, 1, 0, 1, 0},
{1, 0, 1, 0, 1},
{1, 1, 0, 1, 1},
{1, 0, 1, 0, 1},
{0, 1, 1, 1, 0}}
Output : NO
Explanation :
All diagonal elements are equal but non-diagonal elements are having
more than one type of character.

Approach :

  1. Declare three sets s, s1, s2. Where set ‘s’ will store all the non-diagonal elements, set ‘s1’ will store all the ‘left to right diagonal’ elements and set ‘s2’ will store all the ‘right to left diagonal’ elements.
  2. Output will be false in the following case :

=> If s1 is not equal to s2 or if the size of one of them exceeds one.

=> if the size of set ‘s’ exceeds one.

Note : We use SET data structure because as it allows no duplicates and if there are any different values present then the size of s/s1/s2 increases and it return false.

Implementation :

//C++ program to check whether 
//given matrix symbolizes X
#include <iostream>
#include <vector>
#include <set>
using namespace std;

bool CheckForX(vector<vector<int>> arr)
{
    int n = arr.size();
    
    // Declaring three sets s, s1, s2 of
    // char data type
    set<int> s, s1, s2;
  
    // Middle element of square matrix
    int mid = ((n+1)/2)-1;
  
    // Traversing the Matrix elements
    // one by one
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            
            // If it is a mid element (i.e., both diagonals contain this element)
            // insert it to both s1 & s2
            if(i == mid && j == mid) {
                s1.insert(arr[i][j]);
                s2.insert(arr[i][j]);
            }
          
            // if it is a element of left to right diagonal
            // then insert it to s1
            else if(i == j) { 
                s1.insert(arr[i][j]);
            }
          
            // if it is a element of right to left diagonal
            // then insert it to s2
            else if(i == n-j-1) {
                s2.insert(arr[i][j]);
            }
            
            // non diagonal elements are
            // inserted in s
            else {
                s.insert(arr[i][j]);
            }
        }
    } 
    // checking the four conditions 
    // as discussed above
    if(s1 != s2 || s1.size() > 1 || s2.size() > 1 || s.size() > 1) {
        return false;
    }
    else {
        return true;
    }
    
}

int main() 
{
    // declaring a 5 X 5 square matrix
    vector<vector<int>> arr =  {{0, 1, 1, 1, 0},
								{1, 0, 1, 0, 1},
                                {1, 1, 0, 1, 1},
                                {1, 0, 1, 0, 1},
                                {0, 1, 1, 1, 0}};
    if(CheckForX(arr)) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }

}
Output : YES

 Time Complexity : O(n2)

Comment
RKplus Published 3 Dec 2026 · 3 min read

Related tutorials