Making all elements of matrix equal to a given element K
Last Updated :
30 Aug, 2022
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
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++
#include <iostream>
using namespace std;
void checkEqualMatrix( int arr[][3], int n,
int m, int k)
{
int c = 0, cnt1 = 0, cnt2 = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if (c % 2 == 0) {
if (arr[i][j] == k) {
cnt1++;
}
}
else {
if (arr[i][j] == k) {
cnt2++;
}
}
c = c + 1;
}
}
if (cnt1 >= 1 && cnt2 >= 1) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
int arr[3][3] = { { 1, 8, 3 },
{ 1, 2, 2 },
{ 4, 1, 9 } };
int k = 2;
checkEqualMatrix(arr, 3, 3, k);
}
|
Java
class GFG
{
static void checkEqualMatrix( int arr[][], int n,
int m, int k)
{
int c = 0 , cnt1 = 0 , cnt2 = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
if (c % 2 == 0 )
{
if (arr[i][j] == k)
{
cnt1++;
}
}
else
{
if (arr[i][j] == k)
{
cnt2++;
}
}
c = c + 1 ;
}
}
if (cnt1 >= 1 && cnt2 >= 1 )
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main (String[] args)
{
int arr[][] = { { 1 , 8 , 3 },
{ 1 , 2 , 2 },
{ 4 , 1 , 9 } };
int k = 2 ;
checkEqualMatrix(arr, 3 , 3 , k);
}
}
|
Python3
def checkEqualMatrix(arr, n, m, k) :
c = 0 ; cnt1 = 0 ; cnt2 = 0 ;
for i in range (n) :
for j in range (m) :
if (c % 2 = = 0 ) :
if (arr[i][j] = = k) :
cnt1 + = 1 ;
else :
if (arr[i][j] = = k) :
cnt2 + = 1 ;
c = c + 1 ;
if (cnt1 > = 1 and cnt2 > = 1 ) :
print ( "Yes" );
else :
print ( "No" );
if __name__ = = "__main__" :
arr = [
[ 1 , 8 , 3 ],
[ 1 , 2 , 2 ],
[ 4 , 1 , 9 ]
];
k = 2 ;
checkEqualMatrix(arr, 3 , 3 , k);
|
C#
using System;
class GFG
{
static void checkEqualMatrix( int [,]arr, int n,
int m, int k)
{
int c = 0, cnt1 = 0, cnt2 = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
if (c % 2 == 0)
{
if (arr[i,j] == k)
{
cnt1++;
}
}
else
{
if (arr[i,j] == k)
{
cnt2++;
}
}
c = c + 1;
}
}
if (cnt1 >= 1 && cnt2 >= 1)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main()
{
int [,]arr = { { 1, 8, 3 },
{ 1, 2, 2 },
{ 4, 1, 9 } };
int k = 2;
checkEqualMatrix(arr, 3, 3, k);
}
}
|
Javascript
<script>
function checkEqualMatrix(arr, n, m, k)
{
var c = 0, cnt1 = 0, cnt2 = 0;
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < m; j++) {
if (c % 2 == 0) {
if (arr[i][j] == k) {
cnt1++;
}
}
else {
if (arr[i][j] == k) {
cnt2++;
}
}
c = c + 1;
}
}
if (cnt1 >= 1 && cnt2 >= 1) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
var arr = [ [ 1, 8, 3 ],
[ 1, 2, 2 ],
[ 4, 1, 9 ] ];
var k = 2;
checkEqualMatrix(arr, 3, 3, k);
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...