Minimum decrements required to make all pairs of adjacent matrix elements distinct | C ++ program for the above approach ; Function to count minimum number of operations required ; Case 1 : ; Case 2 : ; Print the minimum number of operations required ; Driver Code ; The given matrix ; Function Call to count the minimum number of decrements required
#include <bits/stdc++.h> NEW_LINE 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 ; }