forked from Silver-Taurus/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-8-zero-matrix.cpp
119 lines (102 loc) · 2.68 KB
/
1-8-zero-matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Cracking the coding interview 1.8
* Write a space efficient algorithm, such that if an element in MxN is 0, the entire row and column containing it are 0.
*
* Approach:
* We can use a boolean matrix of MxN or a bit vector to mark row and columns to be nullified in first iteration, but it wont be space efficient.
* More space efficient would be to first check first row and column and if any of them contains zero, mark them to be nullified using two boolearn vars
* let's say firstRow and firstCol, and then iterate through rest of the matrix and store information in first row column elements, only when that row
* and column is to be marked for nullified, this way we will only store values in elements of first row and column which are marked for zeroed any ways.
*/
#include <iostream>
void nullifyRow( int ** matrix, int N, int row ) {
for ( int j = 0; j < N; ++j ) {
matrix[row][j] = 0;
}
}
void nullifyCol( int ** matrix, int M, int col ) {
for ( int i = 0; i < M; ++i ) {
matrix[i][col] = 0;
}
}
void nullifyMatrix( int ** matrix, int M, int N ) {
bool firstRow = false;
bool firstCol = false;
//first row
for( int i = 0; i < N; ++i ) {
if ( matrix[0][i] == 0 ) {
firstRow = true;
break;
}
}
//first col
for ( int i = 0; i < M; ++i ) {
if ( matrix[i][0] == 0 ) {
firstCol = true;
break;
}
}
//rest of the matrix
for( int i = 1; i < M; ++i ) {
for ( int j = 1; j < N; ++j ) {
if ( matrix[i][j] == 0 ) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
//now we know which row and column to be nullify using information stored in previous step.
//rows first
for ( int i = 1; i < M; ++i ) {
if ( matrix[i][0] == 0 ) {
nullifyRow(matrix, N, i);
}
}
//cols now
for ( int j = 1; j < N; ++j ) {
if ( matrix[0][j] == 0 ) {
nullifyCol(matrix, M, j);
}
}
//now first row
if ( firstRow ) {
nullifyRow(matrix, N, 0);
}
//now first col
if ( firstCol ) {
nullifyCol(matrix, M, 0);
}
}
void printMatrix( int ** matrix, int M, int N ) {
for ( int i = 0; i < M; ++i ) {
for ( int j = 0; j < N; ++j ) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int main()
{
int M, N;
std::cout << "Enter number of rows:";
std::cin >> M;
std::cout << "Enter number of cols:";
std::cin >> N;
int ** matrix = new int*[M];
for ( int i =0; i < M; ++i ) {
matrix[i] = new int[N];
}
std::cout << "Provide M x N matrix \n";
for ( int i = 0; i < M; ++i ) {
for ( int j = 0; j < N; ++j ) {
std::cin >> matrix[i][j];
}
}
std::cout << "Matrix Before:\n";
printMatrix(matrix, M, N);
nullifyMatrix(matrix, M, N);
std::cout << "Matrix After:\n";
printMatrix(matrix, M, N);
return 0;
}