Minimum increments required to make the sum of all adjacent matrix elements even
Last Updated :
26 Apr, 2021
Given a matrix mat[][] of dimensions N × M, the task is to minimize the number of increments of matrix elements required to make the sum of adjacent matrix elements even.
Note: For any matrix element mat[i][j], consider mat[i – 1][j], mat[i+1][j], mat[i][j – 1] and mat[i][j + 1] as its adjacent elements.
Examples:
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
Approach: The idea to solve the given problem is based on the fact that the sum of two elements is even only if both the numbers are even or odd. Therefore, for the matrix to have the sum of adjacent elements even, all matrix elements should have the same parity, i.e. either all odd or all even.
Therefore, the minimum number of increments required is the minimum of the count of even and odd elements in the given matrix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 500;
int minOperations( int mat[][MAX], int N)
{
int oddCount = 0;
int evenCount = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (mat[i][j] & 1) {
oddCount++;
}
else {
evenCount++;
}
}
}
cout << min(oddCount, evenCount);
}
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
class GFG
{
static int MAX = 500 ;
static void minOperations( int mat[][], int N)
{
int oddCount = 0 ;
int evenCount = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
if (mat[i][j] % 2 == 1 ) {
oddCount++;
}
else {
evenCount++;
}
}
}
System.out.print(Math.min(oddCount, evenCount));
}
public static void main(String[] args)
{
int mat[][]
= { { 1 , 5 , 6 }, { 4 , 7 , 8 }, { 2 , 2 , 3 } };
int N = mat.length;
minOperations(mat, N);
}
}
|
Python3
MAX = 500
def minOperations(mat, N):
oddCount = 0
evenCount = 0
for i in range (N):
for j in range (N):
if (mat[i][j] & 1 ):
oddCount + = 1
else :
evenCount + = 1
print ( min (oddCount, evenCount))
if __name__ = = '__main__' :
mat = [[ 1 , 5 , 6 ], [ 4 , 7 , 8 ], [ 2 , 2 , 3 ]]
N = len (mat)
minOperations(mat, N)
|
C#
using System;
class GFG
{
const int MAX = 500;
static void minOperations( int [, ] mat, int N)
{
int oddCount = 0;
int evenCount = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if ((mat[i, j] & 1) != 0) {
oddCount++;
}
else {
evenCount++;
}
}
}
Console.Write(Math.Min(oddCount, evenCount));
}
public static void Main()
{
int [, ] mat
= { { 1, 5, 6 }, { 4, 7, 8 }, { 2, 2, 3 } };
int N = mat.GetLength(0);
minOperations(mat, N);
}
}
|
Javascript
<script>
let MAX = 500;
function minOperations(mat, N)
{
let oddCount = 0;
let evenCount = 0;
for (let i = 0; i < N; i++)
{
for (let j = 0; j < N; j++)
{
if (mat[i][j] % 2== 1) {
oddCount++;
}
else {
evenCount++;
}
}
}
document.write(Math.min(oddCount, evenCount));
}
let mat= [ [ 1, 5, 6 ], [ 4, 7, 8 ], [ 2, 2, 3 ] ];
let N = mat.length;
minOperations(mat, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...