Print exchange order and swap count to make count of 1s same rowwise in Binary Matrix
Last Updated :
06 Feb, 2023
Given a n*m binary matrix, the task is to make an equal number of 1’s in each row using the operation as follows: Choose any two rows from (1 to n) and a column from (1 to m) and swap the elements present at these two positions. If it is possible to make equal ones in each row then return the number of steps required and the positions chosen(1-Based indexing) else return -1.
Examples:
Input: n = 3, m = 4, arr[] = {{1, 1, 1, 0}, {0, 0, 1, 0}, {1, 0, 0, 1}};
Output: 1
2 1 1
Explanation: It’s enough to do a single operation: to swap the first element in the second and the first rows. The array will become {{0, 1, 1, 0}, {1, 0, 1, 0}, {1, 0, 0, 1}}, each row contains exactly two 1s.
Input: n = 2, m = 2, arr[] = {{0, 0}, {0, 1}}
Output: -1
Explanation: It is impossible to make equal ones in all the rows.
Approach: The above problem can be solved through the idea:
As we need to make the number of ones in each row of the given array the same, we should calculate the number of ones in the whole array. If the total number of ones in the array is divisible by n (number of rows) then it is possible to make equal ones otherwise it is impossible to make equal ones hence returning -1. If it is possible then count the number of ones in each row if it is greater than (number of ones/n) then reduce and perform the operation. Else if the number of ones is exactly equal to the (total ones/n) then do nothing.
Follow the below steps to implement the idea:
- First, count the total number of ones as total_ones in the arr.
- If total_ones is multiple of n then it is possible to make equal ones otherwise return -1.
- Create a vector ans to store the row numbers and column numbers on which operations are performed.
- Make two vectors iterate the first column to find which rows operation is needed. If the count of ones in any row is greater than total_ones/n then push it in the first vector if less than the push it into the second vector. After the complete iteration pushes the row numbers and column numbers in the ans.
- For each column do the above operation.
Below is the implementation of the above approach.
C++14
#include <bits/stdc++.h>
using namespace std;
void calculate( int n, int m, vector<vector< int > >& Arr)
{
vector< int > sum(n, 0);
for ( int i = 0; i < n; ++i) {
for ( int j = 0; j < m; ++j) {
sum[i] += Arr[i][j];
}
}
int total_ones = 0;
for ( int i = 0; i < n; ++i)
total_ones += sum[i];
if (total_ones % n) {
cout << "-1\n" ;
return ;
}
total_ones /= n;
vector<vector< int > > ans;
for ( int j = 0; j < m; ++j) {
vector< int > V1, V2;
for ( int i = 0; i < n; ++i) {
if (sum[i] > total_ones && Arr[i][j])
V1.push_back(i);
if (sum[i] < total_ones && !Arr[i][j])
V2.push_back(i);
}
for ( int i = 0; i < min(V2.size(), V1.size());
++i) {
++sum[V2[i]], --sum[V1[i]];
ans.push_back({ V2[i], V1[i], j });
}
}
cout << ans.size() << endl;
for ( auto i : ans)
cout << i[0] + 1 << " " << i[1] + 1 << " "
<< i[2] + 1 << " " ;
}
int main()
{
int n = 3, m = 4;
vector<vector< int > > Arr = { { 1, 1, 1, 0 },
{ 0, 0, 1, 0 },
{ 1, 0, 0, 1 } };
calculate(n, m, Arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void calculate( int n, int m, int [][] Arr)
{
int [] sum = new int [n];
for ( int i = 0 ; i < n; ++i) {
for ( int j = 0 ; j < m; ++j) {
sum[i] += Arr[i][j];
}
}
int total_ones = 0 ;
for ( int i = 0 ; i < n; ++i)
total_ones += sum[i];
if (total_ones % n != 0 ) {
System.out.println( "-1" );
return ;
}
total_ones /= n;
List<List<Integer> > ans = new ArrayList<>();
for ( int j = 0 ; j < m; ++j) {
List<Integer> V1 = new ArrayList<>();
List<Integer> V2 = new ArrayList<>();
for ( int i = 0 ; i < n; ++i) {
if (sum[i] > total_ones && Arr[i][j] == 1 ) {
V1.add(i);
}
if (sum[i] < total_ones && Arr[i][j] == 0 ) {
V2.add(i);
}
}
for ( int i = 0 ;
i < Math.min(V2.size(), V1.size()); ++i) {
++sum[V2.get(i)];
--sum[V1.get(i)];
List<Integer> current = new ArrayList<>();
current.add(V2.get(i));
current.add(V1.get(i));
current.add(j);
ans.add(current);
}
}
System.out.println(ans.size());
for (List<Integer> i : ans) {
System.out.print((i.get( 0 ) + 1 ) + " "
+ (i.get( 1 ) + 1 ) + " "
+ (i.get( 2 ) + 1 ) + " " );
}
}
public static void main(String[] args)
{
int n = 3 , m = 4 ;
int [][] Arr = { { 1 , 1 , 1 , 0 },
{ 0 , 0 , 1 , 0 },
{ 1 , 0 , 0 , 1 } };
calculate(n, m, Arr);
}
}
|
Python3
from typing import List , Tuple
def calculate(n: int , m: int , Arr: List [ List [ int ]]) - > None :
sum_ = [ 0 ] * n
for i in range (n):
for j in range (m):
sum_[i] + = Arr[i][j]
total_ones = sum (sum_)
if total_ones % n ! = 0 :
print ( "-1" )
return
total_ones = total_ones / / n
ans = []
for j in range (m):
V1, V2 = [], []
for i in range (n):
if sum_[i] > total_ones and Arr[i][j]:
V1.append(i)
if sum_[i] < total_ones and not Arr[i][j]:
V2.append(i)
for i in range ( min ( len (V2), len (V1))):
sum_[V2[i]] + = 1
sum_[V1[i]] - = 1
ans.append((V2[i], V1[i], j))
print ( len (ans))
for i in ans:
print (i[ 0 ] + 1 , i[ 1 ] + 1 , i[ 2 ] + 1 )
n = 3
m = 4
Arr = [
[ 1 , 1 , 1 , 0 ],
[ 0 , 0 , 1 , 0 ],
[ 1 , 0 , 0 , 1 ]
]
calculate(n, m, Arr)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void Calculate( int n, int m, int [, ] Arr)
{
int [] sum = new int [n];
for ( int i = 0; i < n; ++i) {
for ( int j = 0; j < m; ++j) {
sum[i] += Arr[i, j];
}
}
int total_ones = 0;
for ( int i = 0; i < n; ++i)
total_ones += sum[i];
if (total_ones % n != 0) {
Console.WriteLine( "-1" );
return ;
}
total_ones /= n;
List<List< int > > ans = new List<List< int > >();
for ( int j = 0; j < m; ++j) {
List< int > V1 = new List< int >();
List< int > V2 = new List< int >();
for ( int i = 0; i < n; ++i) {
if (sum[i] > total_ones && Arr[i, j] == 1) {
V1.Add(i);
}
if (sum[i] < total_ones && Arr[i, j] == 0) {
V2.Add(i);
}
}
for ( int i = 0;
i < Math.Min(V2.Count, V1.Count); ++i) {
++sum[V2[i]];
--sum[V1[i]];
List< int > current = new List< int >();
current.Add(V2[i]);
current.Add(V1[i]);
current.Add(j);
ans.Add(current);
}
}
Console.WriteLine(ans.Count);
foreach (List< int > i in ans)
{
Console.Write((i[0] + 1) + " " + (i[1] + 1)
+ " " + (i[2] + 1) + " " );
}
}
static public void Main()
{
int n = 3, m = 4;
int [, ] Arr = { { 1, 1, 1, 0 },
{ 0, 0, 1, 0 },
{ 1, 0, 0, 1 } };
Calculate(n, m, Arr);
}
}
|
Javascript
function calculate(n, m, Arr) {
let sum = Array(n).fill(0);
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
sum[i] += Arr[i][j];
}
}
let total_ones = 0;
for (let i = 0; i < n; ++i)
total_ones += sum[i];
if (total_ones % n) {
console.log( "-1" );
return ;
}
total_ones /= n;
let ans = [];
for (let j = 0; j < m; ++j) {
let V1 = [], V2 = [];
for (let i = 0; i < n; ++i) {
if (sum[i] > total_ones && Arr[i][j])
V1.push(i);
if (sum[i] < total_ones && !Arr[i][j])
V2.push(i);
}
for (let i = 0; i < Math.min(V2.length, V1.length); ++i) {
++sum[V2[i]], --sum[V1[i]];
ans.push([V2[i], V1[i], j]);
}
}
console.log(ans.length);
for (let i of ans)
console.log((i[0] + 1) + " " + (i[1] + 1) + " " + (i[2] + 1));
}
let n = 3, m = 4;
let Arr = [
[1, 1, 1, 0],
[0, 0, 1, 0],
[1, 0, 0, 1]
];
calculate(n, m, Arr);
|
Time Complexity: O(N*M)
Auxiliary Space: O(2*N)
Share your thoughts in the comments
Please Login to comment...