使矩阵的所有元素等于给定元素 K
给定一个二维数组arr[][] ,任务是检查是否有可能使数组的所有元素等于给定的数字k如果在一次操作中可以选择任何元素以及周围的对角线元素可以等于它。
例子:
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
方法:
- 矩阵可以被认为是一个带有黑色和白色盒子的棋盘。
- 如果选择黑盒中的任何元素等于给定的数字,则可以使用给定的操作使黑盒的所有元素都等于它,
- 同样,可以检查白框。所以需要至少有一个元素等于黑色和白色框中的给定元素。
- 所以我们需要使用计数器迭代所有元素。如果计数器的值为奇数,则可将其视为黑盒,对于偶数值,可将其视为白盒。
下面是上述方法的实现。
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[][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
// 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
Javascript
输出:
Yes
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。