Find the minimum difference of 1s between two vertical halves of the given matrix
Last Updated : 15 Feb, 2024
Given a binary square matrix M[][] of size NXN, where N is an even number. The task is to find the minimum difference of frequency of 1s between the first and the second vertical half of M[][]. We can apply the below operations at most once:
Choose any one row out of N rows of M[][] and reverse it in.
Examples:
Input: N = 6, M [][] = {100000, 100000, 100000, 100000, 010010, 001100}
Input Matrix
Output: 2
Explanation:
Initially in M[][]:
First half (First 3 columns): (Count of 1s) = 6
Second half (Last 3 columns): (Count of 1s) = 2
Initial difference: |6-2| = 4
Count of 1s in both halves initially
After Using operation:
Let us reverse the 1st row. Now, If we see the difference of count in both halves, It is |5-3| = 2. Because count of 1s is 5 in first half and 3 in second. So, 2 is the minimum possible difference that can be achieved. Therefore, output is 2.
Matrix after applying one operation
Input: N = 4, M[][] = {0011, 1100, 1110, 0001}
Output: 0
Explanation: Initial difference between count of 1s of both vertical halves is: |4-4| = 0. The minimum possible difference is already 0 initially. Hence, no need to use operation.
Approach: The problem can be solved using the following approach:
The problem can be solved by storing the difference of 1s between the first half and the second half for each row and storing it in an array, say diff[]. If we observe carefully, we get to know that if we swap a row whose difference between first and second half is +D, then after the swap the difference becomes -D. So, now the problem is reduced to minimize the absolute sum of all the elements in the array diff[] such that we can change the sign of a single number.
Steps to solve the problem:
Calculate the difference between the first half and the second half and store it in an array diff[].
Calculate the sum of the array diff[].
For each element, change the sign of the current element and calculate the new sum.
Update the answer if the new sum is smaller than the previous sum.
Print the final answer.
Implementation:
C++
// CPP program for the above approach#include<iostream>usingnamespacestd;// Function to print the minimum differencevoidMinimumDifference(intN,intM[][6]){// Array to store the difference of the number of 1s// between the first and the second halfintdiff[N];// Variable to store the sum of all differencesintsum=0;for(inti=0;i<N;i++){intD=0;for(intj=0;j<N;j++){if(M[i][j]==1){// If 1 lies in the first halfif(j<N/2){D+=1;}// If 1 lies in the second halfelse{D-=1;}}}diff[i]=D;sum+=D;}intans=sum;// Reverse the sign of the difference of all rows to get// the answerfor(inti=0;i<N;i++){ans=min(ans,sum-2*diff[i]);}cout<<ans<<endl;}// Driver Codeintmain(){// InputintN=6;intM[][6]={{1,0,0,0,0,0},{1,0,0,0,0,0},{1,0,0,0,0,0},{1,0,0,0,0,0},{0,1,0,0,1,0},{0,0,1,1,0,0}};// Function callMinimumDifference(N,M);return0;}// This code is contributed by Susobhan Akhuli
Java
// Java code to implement the approachimportjava.util.*;// Driver ClassclassGFG{// Driver Functionpublicstaticvoidmain(String[]args)throwsjava.lang.Exception{// InputintN=6;intM[][]={{1,0,0,0,0,0},{1,0,0,0,0,0},{1,0,0,0,0,0},{1,0,0,0,0,0},{0,1,0,0,1,0},{0,0,1,1,0,0}};// function callMinimum_difference(N,M);}// Method to print the minimum differencepublicstaticvoidMinimum_difference(intN,int[][]M){// Array to store difference of number of 1s between// the first and the second halfintdiff[]=newint[N];// Variable to store the sum of all differencesintsum=0;for(inti=0;i<N;i++){intD=0;for(intj=0;j<N;j++){if(M[i][j]==1){// If 1 lies in the first halfif(j<N/2){D+=1;}// If 1 lies in the second halfelse{D-=1;}}}diff[i]=D;sum+=D;}intans=sum;// Reverse the sign of difference of all rows to get// the answerfor(inti=0;i<N;i++){ans=Math.min(ans,sum-2*diff[i]);}System.out.println(ans);}}
Python3
# Function to print the minimum differencedefMinimumDifference(N,M):# Array to store the difference of the number of 1s# between the first and the second halfdiff=[0]*N# Variable to store the sum of all differencessum=0foriinrange(N):D=0forjinrange(N):ifM[i][j]==1:# If 1 lies in the first halfifj<N/2:D+=1# If 1 lies in the second halfelse:D-=1diff[i]=Dsum+=Dans=sum# Reverse the sign of the difference of all rows to get# the answerforiinrange(N):ans=min(ans,sum-2*diff[i])print(ans)# Driver CodeN=6M=[[1,0,0,0,0,0],[1,0,0,0,0,0],[1,0,0,0,0,0],[1,0,0,0,0,0],[0,1,0,0,1,0],[0,0,1,1,0,0]]# Function callMinimumDifference(N,M)
C#
usingSystem;classProgram{// Function to print the minimum differencestaticvoidMinimumDifference(intN,int[,]M){// Array to store the difference of the number of 1s// between the first and the second halfint[]diff=newint[N];// Variable to store the sum of all differencesintsum=0;for(inti=0;i<N;i++){intD=0;for(intj=0;j<N;j++){if(M[i,j]==1){// If 1 lies in the first halfif(j<N/2){D+=1;}// If 1 lies in the second halfelse{D-=1;}}}diff[i]=D;sum+=D;}intans=sum;// Reverse the sign of the difference of all rows to get// the answerfor(inti=0;i<N;i++){ans=Math.Min(ans,sum-2*diff[i]);}Console.WriteLine(ans);}// Driver CodestaticvoidMain(){// InputintN=6;int[,]M={{1,0,0,0,0,0},{1,0,0,0,0,0},{1,0,0,0,0,0},{1,0,0,0,0,0},{0,1,0,0,1,0},{0,0,1,1,0,0}};// Function callMinimumDifference(N,M);}}
JavaScript
// JavaScript code to implement the approach// Function to calculate the minimum differencefunctionminimumDifference(N,M){// Array to store difference of number of 1s between// the first and the second halfconstdiff=newArray(N).fill(0);// Variable to store the sum of all differencesletsum=0;for(leti=0;i<N;i++){letD=0;for(letj=0;j<N;j++){if(M[i][j]===1){// If 1 lies in the first halfif(j<N/2){D+=1;}// If 1 lies in the second halfelse{D-=1;}}}diff[i]=D;sum+=D;}letans=sum;// Reverse the sign of difference of all rows to get// the answerfor(leti=0;i<N;i++){ans=Math.min(ans,sum-2*diff[i]);}console.log(ans);}// Provided InputconstN=6;constM=[[1,0,0,0,0,0],[1,0,0,0,0,0],[1,0,0,0,0,0],[1,0,0,0,0,0],[0,1,0,0,1,0],[0,0,1,1,0,0]];// Function callminimumDifference(N,M);
Output
2
Time Complexity: O(N * N), where N is the number of rows or columns in the input grid M[][]. Auxiliary Space: O(N)