给定一个维度为N × M的矩阵mat[][] ,任务是最小化矩阵元素的增量数量,以使相邻矩阵元素的总和为偶数。
注意:对于任何矩阵元素mat[i][j],考虑mat[i – 1][j]、mat[i+1][j]、mat[i][j – 1]和mat[i][ j + 1]作为其相邻元素。
例子:
Input: mat[][] = {{1, 5, 6}, {4, 7, 8}, {2, 2, 3}}
Output: 4
Explanation:
Increase cell mat[0][0] by 1, mat[1][1] by 1, mat[0][1] by 1 and mat[2][2] by 1.
Therefore, total number of increments required is 4. Therefore, modified matrix is {{2, 6, 6}, {4, 8, 8}, {2, 2, 4}} having sum of all adjacent elements even.
Input: mat[][] = {{1, 5, 5}, {5, 5, 5}, {5, 1, 1}}
Output: 0
办法:解决给定的问题的想法是基于这样的事实,两个元素的总和,即使只有当两个数字是偶数或奇数。因此,对于相邻元素之和为偶数的矩阵,所有矩阵元素应具有相同的奇偶性,即全部为奇数或全部为偶数。
因此,所需的最小增量数是给定矩阵中偶数和奇数元素计数中的最小值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
const int MAX = 500;
// Function to find the minimum number
// of increments required to make sum of
// al adjacent matrix elements even
int minOperations(int mat[][MAX], int N)
{
// Stores the count of odd elements
int oddCount = 0;
// Stores the count of even elements
int evenCount = 0;
// Iterate the matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// If element is odd
if (mat[i][j] & 1) {
// Increment odd count
oddCount++;
}
// Otherwise
else {
// Increment even count
evenCount++;
}
}
}
// Print the minimum of both counts
cout << min(oddCount, evenCount);
}
// Driver Code
int main()
{
int mat[][MAX]
= { { 1, 5, 6 }, { 4, 7, 8 }, { 2, 2, 3 } };
int N = sizeof(mat) / sizeof(mat[0]);
minOperations(mat, N);
return 0;
}
Java
// Java program for the above approach
class GFG
{
static int MAX = 500;
// Function to find the minimum number
// of increments required to make sum of
// al adjacent matrix elements even
static void minOperations(int mat[][], int N)
{
// Stores the count of odd elements
int oddCount = 0;
// Stores the count of even elements
int evenCount = 0;
// Iterate the matrix
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
// If element is odd
if (mat[i][j] % 2== 1) {
// Increment odd count
oddCount++;
}
// Otherwise
else {
// Increment even count
evenCount++;
}
}
}
// Print the minimum of both counts
System.out.print(Math.min(oddCount, evenCount));
}
// Driver Code
public static void main(String[] args)
{
int mat[][]
= { { 1, 5, 6 }, { 4, 7, 8 }, { 2, 2, 3 } };
int N = mat.length;
minOperations(mat, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program for the above approach
MAX = 500
# Function to find the minimum number
# of increments required to make sum of
# al adjacent matrix elements even
def minOperations(mat, N):
# Stores the count of odd elements
oddCount = 0
# Stores the count of even elements
evenCount = 0
# Iterate the matrix
for i in range(N):
for j in range(N):
# If element is odd
if (mat[i][j] & 1):
# Increment odd count
oddCount += 1
# Otherwise
else:
# Increment even count
evenCount += 1
# Print the minimum of both counts
print(min(oddCount, evenCount))
# Driver Code
if __name__ == '__main__':
mat = [[1, 5, 6], [4, 7, 8], [2, 2, 3]]
N = len(mat)
minOperations(mat, N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG
{
const int MAX = 500;
// Function to find the minimum number
// of increments required to make sum of
// al adjacent matrix elements even
static void minOperations(int[, ] mat, int N)
{
// Stores the count of odd elements
int oddCount = 0;
// Stores the count of even elements
int evenCount = 0;
// Iterate the matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// If element is odd
if ((mat[i, j] & 1) != 0) {
// Increment odd count
oddCount++;
}
// Otherwise
else {
// Increment even count
evenCount++;
}
}
}
// Print the minimum of both counts
Console.Write(Math.Min(oddCount, evenCount));
}
// Driver Code
public static void Main()
{
int[, ] mat
= { { 1, 5, 6 }, { 4, 7, 8 }, { 2, 2, 3 } };
int N = mat.GetLength(0);
minOperations(mat, N);
}
}
// This code is contributed by chitranayal.
Javascript
4
时间复杂度: O(N 2 )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live