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 2Input:
arr[][] = 3 1 2 3
2 1 8 6
9 7 9 9
k = 4
Output: No
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++ implementation of the above approach. #include <iostream> using namespace std;
// Function to check if all // elements can be equal or not void checkEqualMatrix( int arr[][3], 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[3][3] = { { 1, 8, 3 },
{ 1, 2, 2 },
{ 4, 1, 9 } };
int k = 2;
// Function calling
checkEqualMatrix(arr, 3, 3, k);
} |
// 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 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# 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 |
<script> // Javascript implementation of the above approach. // Function to check if all // elements can be equal or not function checkEqualMatrix(arr, n, m, k)
{ var c = 0, cnt1 = 0, cnt2 = 0;
// Iterate over the matrix
for ( var i = 0; i < n; i++) {
for ( var 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) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
} // Driver code var arr = [ [ 1, 8, 3 ],
[ 1, 2, 2 ],
[ 4, 1, 9 ] ];
var k = 2;
// Function calling checkEqualMatrix(arr, 3, 3, k); // This code is contributed by importantly. </script> |
Yes
Time Complexity: O(N*M) as nested loops are required in the algorithm the overall time complexity is O(N*M).
Auxiliary Space: O(1) as constant extra space is being used.