-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1373 from pranavbafna586/main
Added Set Matrix Zero
- Loading branch information
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <stdio.h> | ||
|
||
#define MAX 100 | ||
|
||
// Function to print the matrix | ||
void printMatrix(int matrix[][MAX], int rows, int cols) { | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
printf("%d ", matrix[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
} | ||
|
||
// Function to set the rows and columns to zero where element is zero | ||
void setMatrixZero(int matrix[][MAX], int rows, int cols) { | ||
int rowFlag[MAX] = {0}; // Array to mark rows to be set to zero | ||
int colFlag[MAX] = {0}; // Array to mark columns to be set to zero | ||
|
||
// First pass: Identify rows and columns to be zeroed | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
if (matrix[i][j] == 0) { | ||
rowFlag[i] = 1; // Mark row i for zeroing | ||
colFlag[j] = 1; // Mark column j for zeroing | ||
} | ||
} | ||
} | ||
|
||
// Second pass: Set identified rows and columns to zero | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
if (rowFlag[i] == 1 || colFlag[j] == 1) { | ||
matrix[i][j] = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
int matrix[MAX][MAX]; | ||
int rows, cols; | ||
|
||
// Taking input for the dimensions of the matrix | ||
printf("Enter the number of rows and columns: "); | ||
scanf("%d %d", &rows, &cols); | ||
|
||
// Taking input for matrix elements | ||
printf("Enter the elements of the matrix:\n"); | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
scanf("%d", &matrix[i][j]); | ||
} | ||
} | ||
|
||
printf("Original Matrix:\n"); | ||
printMatrix(matrix, rows, cols); | ||
|
||
// Setting the required rows and columns to zero | ||
setMatrixZero(matrix, rows, cols); | ||
|
||
printf("Modified Matrix:\n"); | ||
printMatrix(matrix, rows, cols); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Set Matrix Zero | ||
|
||
## Problem Statement | ||
Given a matrix, if an element in the matrix is `0`, you must set its entire row and column to `0`. The task is to modify the matrix in place to achieve this result. | ||
|
||
### Example | ||
Input: | ||
matrix = [[1,1,1], [1,0,1], [1,1,1]] | ||
|
||
Output: | ||
[[1,0,1], [0,0,0], [1,0,1]] | ||
|
||
### Explanation | ||
Since `matrix[1][1]` is `0`, the entire row and column where this element is located are set to `0`. | ||
|
||
--- | ||
|
||
## Code Explanation | ||
|
||
The C program provided below takes the matrix dimensions and elements as input and sets rows and columns to zero according to the problem statement. | ||
|
||
### Code Flow | ||
1. **Input Matrix Dimensions and Elements**: The user enters the matrix dimensions (`rows` and `cols`) and the elements. | ||
2. **Identifying Rows and Columns for Zeroing**: | ||
- Two arrays, `rowFlag` and `colFlag`, are used to keep track of which rows and columns should be set to zero. | ||
- In a first pass through the matrix, if a `0` is found at any position `matrix[i][j]`, the program marks the respective row and column in `rowFlag` and `colFlag`. | ||
3. **Setting Rows and Columns to Zero**: | ||
- In a second pass through the matrix, if a row or column is marked in the `rowFlag` or `colFlag`, all elements in that row or column are set to zero. | ||
4. **Output Modified Matrix**: The program prints the modified matrix with the correct rows and columns set to zero. | ||
|
||
### Example Usage | ||
|
||
#### Input | ||
Enter the number of rows and columns: 3 3 Enter the elements of the matrix: 1 1 1 1 0 1 1 1 1 | ||
|
||
#### Output | ||
Original Matrix: 1 1 1 1 0 1 1 1 1 | ||
|
||
Modified Matrix: 1 0 1 0 0 0 1 0 1 | ||
|
||
|
||
--- | ||
|
||
## Complexity Analysis | ||
- **Time Complexity**: `O(rows * cols)` for both passes over the matrix. | ||
- **Space Complexity**: `O(rows + cols)` for the additional `rowFlag` and `colFlag` arrays used to mark rows and columns to be zeroed. | ||
|
||
|
||
|
||
|