# Making all elements of matrix equal to a given element K

Given a 2-d array arr[][], the task is to check whether it is possible to make all elements of the array to equal to a given number k if, in one operation, any element can be chosen and the surrounding diagonal elements can be made equal to it.

Examples:

```Input:
arr[][] = 1 8 3
1 2 2
4 1 9
k = 2
Output: Yes
Explanation:
In first operation choose element at (2, 2)
New array = 2 8 2
1 2 2
2 1 2
In second operation choose element at (2, 3)
New array = 2 2 2
1 2 2
2 2 2
In third operation choose element at (1, 2)
New array = 2 2 2
2 2 2
2 2 2

Input:
arr[][] = 3 1 2 3
2 1 8 6
9 7 9 9
k = 4
Output:
No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• The matrix can be considered as a chessboard with black and white boxes.
• If any element in the black box is chosen which is equal to the given number, then all elements of black boxes can be made equal to it using the given operation,
• Similarly, it can be checked for the white boxes. So there need to be at least one element equal to the given element in both black and white boxes.
• So we need to iterate over all elements using a counter. If the value of the counter is odd, it can be considered a black box and for even values, it can be considered a white box.

Below is the implementation of the above approach.

## C++

 `// C++ implementation of the above approach. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if all ` `// elements can be equal or not ` `void` `checkEqualMatrix(``int` `arr[], ``int` `n, ` `                      ``int` `m, ``int` `k) ` `{ ` `    ``int` `c = 0, cnt1 = 0, cnt2 = 0; ` ` `  `    ``// Iterate over the matrix ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``for` `(``int` `j = 0; j < m; j++) { ` `            ``if` `(c % 2 == 0) { ` ` `  `                ``// Update the counter for odd values ` `                ``// if array element at that position is k ` `                ``if` `(arr[i][j] == k) { ` `                    ``cnt1++; ` `                ``} ` `            ``} ` `            ``else` `{ ` ` `  `                ``// Update the counter for even values ` `                ``// if array element at that position is k ` `                ``if` `(arr[i][j] == k) { ` `                    ``cnt2++; ` `                ``} ` `            ``} ` `            ``c = c + 1; ` `        ``} ` `    ``} ` `    ``// To check if there is at least one ` `    ``// element at both even and odd indices. ` `    ``if` `(cnt1 >= 1 && cnt2 >= 1) { ` `        ``cout << ``"Yes"``; ` `    ``} ` `    ``else` `{ ` `        ``cout << ``"No"``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr = { { 1, 8, 3 }, ` `                      ``{ 1, 2, 2 }, ` `                      ``{ 4, 1, 9 } }; ` `    ``int` `k = 2; ` `    ``// Function calling ` `    ``checkEqualMatrix(arr, 3, 3, k); ` `} `

## Java

 `// Java implementation of the above approach.  ` `class` `GFG  ` `{ ` `     `  `    ``// Function to check if all  ` `    ``// elements can be equal or not  ` `    ``static` `void` `checkEqualMatrix(``int` `arr[][], ``int` `n,  ` `                              ``int` `m, ``int` `k)  ` `    ``{  ` `        ``int` `c = ``0``, cnt1 = ``0``, cnt2 = ``0``;  ` `     `  `        ``// Iterate over the matrix  ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{  ` `            ``for` `(``int` `j = ``0``; j < m; j++)  ` `            ``{  ` `                ``if` `(c % ``2` `== ``0``) ` `                ``{  ` `     `  `                    ``// Update the counter for odd values  ` `                    ``// if array element at that position is k  ` `                    ``if` `(arr[i][j] == k) ` `                    ``{  ` `                        ``cnt1++;  ` `                    ``}  ` `                ``}  ` `                ``else`  `                ``{  ` `     `  `                    ``// Update the counter for even values  ` `                    ``// if array element at that position is k  ` `                    ``if` `(arr[i][j] == k)  ` `                    ``{  ` `                        ``cnt2++;  ` `                    ``}  ` `                ``}  ` `                ``c = c + ``1``;  ` `            ``}  ` `        ``}  ` `         `  `        ``// To check if there is at least one  ` `        ``// element at both even and odd indices.  ` `        ``if` `(cnt1 >= ``1` `&& cnt2 >= ``1``) ` `        ``{  ` `            ``System.out.println(``"Yes"``);  ` `        ``}  ` `        ``else` `        ``{  ` `            ``System.out.println(``"No"``);  ` `        ``}  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `arr[][] = { { ``1``, ``8``, ``3` `},  ` `                        ``{ ``1``, ``2``, ``2` `},  ` `                        ``{ ``4``, ``1``, ``9` `} };  ` `        ``int` `k = ``2``;  ` `         `  `        ``// Function calling  ` `        ``checkEqualMatrix(arr, ``3``, ``3``, k);  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the above approach.  ` ` `  `# Function to check if all  ` `# elements can be equal or not  ` `def` `checkEqualMatrix(arr, n, m, k) : ` ` `  `    ``c ``=` `0``; cnt1 ``=` `0``; cnt2 ``=` `0``;  ` ` `  `    ``# Iterate over the matrix  ` `    ``for` `i ``in` `range``(n) : ` `        ``for` `j ``in` `range``(m) : ` `            ``if` `(c ``%` `2` `=``=` `0``) : ` `                 `  `                ``# Update the counter for odd values  ` `                ``# if array element at that position is k  ` `                ``if` `(arr[i][j] ``=``=` `k) : ` `                    ``cnt1 ``+``=` `1``;  ` `             `  `            ``else` `:  ` ` `  `                ``# Update the counter for even values  ` `                ``# if array element at that position is k  ` `                ``if` `(arr[i][j] ``=``=` `k) : ` `                    ``cnt2 ``+``=` `1``;  ` ` `  `            ``c ``=` `c ``+` `1``;  ` ` `  `    ``# To check if there is at least one  ` `    ``# element at both even and odd indices.  ` `    ``if` `(cnt1 >``=` `1` `and` `cnt2 >``=` `1``) : ` `        ``print``(``"Yes"``);  ` `    ``else` `:  ` `        ``print``(``"No"``);  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``arr ``=` `[  ` `            ``[ ``1``, ``8``, ``3` `],  ` `            ``[ ``1``, ``2``, ``2` `],  ` `            ``[ ``4``, ``1``, ``9` `]  ` `            ``];  ` `             `  `    ``k ``=` `2``;  ` `     `  `    ``# Function calling  ` `    ``checkEqualMatrix(arr, ``3``, ``3``, k);  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the above approach.  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` `     `  `    ``// Function to check if all  ` `    ``// elements can be equal or not  ` `    ``static` `void` `checkEqualMatrix(``int` `[,]arr, ``int` `n,  ` `                                        ``int` `m, ``int` `k)  ` `    ``{  ` `        ``int` `c = 0, cnt1 = 0, cnt2 = 0;  ` `     `  `        ``// Iterate over the matrix  ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{  ` `            ``for` `(``int` `j = 0; j < m; j++)  ` `            ``{  ` `                ``if` `(c % 2 == 0)  ` `                ``{  ` `     `  `                    ``// Update the counter for odd values  ` `                    ``// if array element at that position is k  ` `                    ``if` `(arr[i,j] == k)  ` `                    ``{  ` `                        ``cnt1++;  ` `                    ``}  ` `                ``}  ` `                ``else` `                ``{  ` `     `  `                    ``// Update the counter for even values  ` `                    ``// if array element at that position is k  ` `                    ``if` `(arr[i,j] == k)  ` `                    ``{  ` `                        ``cnt2++;  ` `                    ``}  ` `                ``}  ` `                ``c = c + 1;  ` `            ``}  ` `        ``}  ` `         `  `        ``// To check if there is at least one  ` `        ``// element at both even and odd indices.  ` `        ``if` `(cnt1 >= 1 && cnt2 >= 1)  ` `        ``{  ` `            ``Console.WriteLine(``"Yes"``);  ` `        ``}  ` `        ``else` `        ``{  ` `            ``Console.WriteLine(``"No"``);  ` `        ``}  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `[,]arr = { { 1, 8, 3 },  ` `                        ``{ 1, 2, 2 },  ` `                        ``{ 4, 1, 9 } };  ` `        ``int` `k = 2;  ` `         `  `        ``// Function calling  ` `        ``checkEqualMatrix(arr, 3, 3, k);  ` `    ``}  ` `}  ` ` `  `// This code is contributed by AnkitRai01  `

Output:

```Yes
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : AnkitRai01