Skip to content

Commit

Permalink
Merge pull request #1373 from pranavbafna586/main
Browse files Browse the repository at this point in the history
Added Set Matrix Zero
  • Loading branch information
pankaj-bind authored Oct 29, 2024
2 parents 85b21b8 + a66db2c commit 70dd9e2
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
66 changes: 66 additions & 0 deletions 2D Arrays/SetMatrixZero/Program.c
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;
}
50 changes: 50 additions & 0 deletions 2D Arrays/SetMatrixZero/README.md
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.




0 comments on commit 70dd9e2

Please sign in to comment.