Minimum decrements required to make all pairs of adjacent matrix elements distinct
Given a matrix mat[][] of dimension N * M, the task is to count the minimum number of decrements of distinct array elements required such that no two adjacent matrix elements are equal.
Examples:
Input: mat[][] = { {2, 3, 4}, {2, 5, 4} }
Output: 3
Explanation: Decrease the matrix elements arr[0][0], arr[0][1], and arr[0][2] by 1 each. The matrix modifies to {{1, 2, 3}, {2, 5, 4}}. Therefore, all pairs of adjacent matrix elements becomes different.
Input: mat[][] = { {1, 2, 3}, {1, 2, 3}, {1, 2, 3} }
Output: 3
Explanation: Decrease each element present in the second row of the matrix by 1. The matrix will have all adjacent element different as shown below:
1 2 3
0 1 2
1 2 3
Approach: The main idea is to solve the given problem by assuming the matrix like a Chess Grid shown below:
Follow the steps below to solve the problem:
- Traverse the matrix
- For every matrix element, following two cases arise:
- Case 1 : If (i + j) is even, mat[i][j] should be even. Otherwise, mat[i][j] should be odd.
- Case 2: If (i + j) is even, value at mat[i][j] should be even. Otherwise, value at mat[i][j] should be odd.
- Therefore, calculate the number of operations required in both cases.
- Print the minimum of the two counts obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int n = 3;
const int m = 3;
void countDecrements( long long arr[][m])
{
int count_1 = 0;
int count_2 = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if ((i + j) % 2 == arr[i][j] % 2)
count_1++;
if (1 - (i + j) % 2 == arr[i][j] % 2)
count_2++;
}
}
cout << min(count_1, count_2);
}
int main()
{
long long arr[][m]
= { { 1, 2, 3 },
{ 1, 2, 3 },
{ 1, 2, 3 } };
countDecrements(arr);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void countDecrements( long arr[][])
{
int n = arr.length;
int m = arr[ 0 ].length;
int count_1 = 0 ;
int count_2 = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
if ((i + j) % 2 == arr[i][j] % 2 )
count_1++;
if ( 1 - (i + j) % 2 == arr[i][j] % 2 )
count_2++;
}
}
System.out.println(Math.min(count_1, count_2));
}
public static void main(String[] args)
{
long arr[][] = { { 1 , 2 , 3 },
{ 1 , 2 , 3 },
{ 1 , 2 , 3 } };
countDecrements(arr);
}
}
|
Python3
n = 3
m = 3
def countDecrements(arr):
count_1 = 0
count_2 = 0
for i in range (n):
for j in range (m):
if ((i + j) % 2 = = arr[i][j] % 2 ):
count_1 + = 1
if ( 1 - (i + j) % 2 = = arr[i][j] % 2 ):
count_2 + = 1
print ( min (count_1, count_2))
arr = [[ 1 , 2 , 3 ],
[ 1 , 2 , 3 ],
[ 1 , 2 , 3 ]]
countDecrements(arr)
|
C#
using System;
class GFG{
static void countDecrements( long [,] arr)
{
int n = arr.GetLength(0);
int m = arr.GetLength(1);
int count_1 = 0;
int count_2 = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
if ((i + j) % 2 == arr[i, j] % 2)
count_1++;
if (1 - (i + j) % 2 == arr[i, j] % 2)
count_2++;
}
}
Console.WriteLine(Math.Min(count_1, count_2));
}
public static void Main()
{
long [,] arr = { { 1, 2, 3 },
{ 1, 2, 3 },
{ 1, 2, 3 } };
countDecrements(arr);
}
}
|
Javascript
<script>
const n = 3;
const m = 3;
function countDecrements(arr)
{
let count_1 = 0;
let count_2 = 0;
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
if ((i + j) % 2 == arr[i][j] % 2)
count_1++;
if (1 - (i + j) % 2 == arr[i][j] % 2)
count_2++;
}
}
document.write(Math.min(count_1, count_2));
}
let arr = [ [ 1, 2, 3 ],
[ 1, 2, 3 ],
[ 1, 2, 3 ] ];
countDecrements(arr);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
23 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...