Skip to content

Commit d9b3484

Browse files
author
rpanjrath
committed
Arrays: Fill binary matrix with 1's
1 parent ea7e191 commit d9b3484

File tree

2 files changed

+74
-10
lines changed

2 files changed

+74
-10
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,39 @@ Arrays
2424
4. Shift array of integers circularly given input array and shift size.
2525
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/circularshiftintarray/CircularShiftArray.java
2626

27-
5. Check if a sub-array exists in parent array.
27+
5. Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 (or true)
28+
then make all the cells of ith row and jth column as 1.
29+
Link:
30+
31+
6. Check if a sub-array exists in parent array.
2832
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/findarray/FindArrayImpl.java
2933

30-
6. Find duplicates count in sorted array.
34+
7. Find duplicates count in sorted array.
3135
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/findduplicatecountsortedarray/DuplicatesCountInSortedArray.java
3236

33-
7. Write a program takes array input{1,0,2,3} and num = 3 and provides output {1,2}{0,3}{2,1}{3,0} sum equals the num.
37+
8. Write a program takes array input{1,0,2,3} and num = 3 and provides output {1,2}{0,3}{2,1}{3,0} sum equals the num.
3438
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/findpairsequaltosum/FindPairsEqualToSum.java
3539

36-
8. Given a sorted array (ascending order and distinct elements), find i such that inputArr[i] = i. Return -1 if nothing found.
40+
9. Given a sorted array (ascending order and distinct elements), find i such that inputArr[i] = i. Return -1 if nothing found.
3741
Link:
3842

39-
9. Find the contiguous subarray which has the largest sum. Kadane's algorithm.
43+
10. Find the contiguous subarray which has the largest sum. Kadane's algorithm.
4044
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/maxsubarray/FindMaxSubArray.java
4145

42-
10. Given two sorted (ascending) arrays of integers, find the minimum difference between two integers in that array.
46+
11. Given two sorted (ascending) arrays of integers, find the minimum difference between two integers in that array.
4347
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/mindiffsortedarrays/MinimumDifferenceSortedArrays.java
4448

45-
11. Print numbers with frequency.
49+
12. Print numbers with frequency.
4650
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/numberfrequency/PrintNumbersWithFrequency.java
4751

48-
12. Find Number with Highest frequency in sorted array.
52+
13. Find Number with Highest frequency in sorted array.
4953
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/numberwithhighestfreq/FindNumberWithHighestFreq.java
5054

51-
13. Given an array, write a program to generate a random permutation of array elements.
55+
14. Given an array, write a program to generate a random permutation of array elements.
5256
Also asked as “shuffle a deck of cards” or “randomize a given array”.
5357
Link:
5458

55-
14. Search for a number in rotated sorted array (Ascending or Descending).
59+
15. Search for a number in rotated sorted array (Ascending or Descending).
5660
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/searchrotatedsortedarray/SearchInRotatedSortedArray.java
5761

5862

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package arrays.fillmatrixwithones;
2+
3+
/**
4+
* Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 (or true)
5+
* then make all the cells of ith row and jth column as 1.
6+
* User: techpanja
7+
* Date: 2/8/14
8+
* Time: 6:07 PM
9+
*/
10+
public class FillBinaryMatrixWithOnes {
11+
12+
private FillBinaryMatrixWithOnes() {
13+
14+
}
15+
16+
public static int[][] fillBinaryMatrix(int[][] matrix) {
17+
int[] rowTemp = new int[matrix.length];
18+
int[] colTemp = new int[matrix[0].length];
19+
int noOfRows = rowTemp.length;
20+
int noOfCols = colTemp.length;
21+
if (noOfRows == 0 || noOfCols == 0) {
22+
return new int[][]{};
23+
}
24+
for (int i = 0; i < noOfRows; i++) {
25+
rowTemp[i] = 0;
26+
}
27+
for (int i = 0; i < noOfCols; i++) {
28+
colTemp[i] = 0;
29+
}
30+
for (int i = 0; i < noOfRows; i++) {
31+
for (int j = 0; j < noOfCols; j++) {
32+
if (matrix[i][j] == 1) {
33+
rowTemp[i] = 1;
34+
colTemp[j] = 1;
35+
}
36+
}
37+
}
38+
for (int i = 0; i < noOfRows; i++) {
39+
for (int j = 0; j < noOfCols; j++) {
40+
if (rowTemp[i] == 1 || colTemp[j] == 1) {
41+
matrix[i][j] = 1;
42+
}
43+
}
44+
}
45+
return matrix;
46+
}
47+
48+
public static void printMatrix(int[][] inputMatrix) {
49+
for (int i = 0; i < inputMatrix.length; i++) {
50+
for (int j = 0; j < inputMatrix[0].length; j++) {
51+
System.out.print(inputMatrix[i][j] + " ");
52+
}
53+
System.out.println();
54+
}
55+
}
56+
57+
public static void main(String[] args) {
58+
printMatrix(fillBinaryMatrix(new int[][]{{1, 0}, {0, 0}, {0, 0}}));
59+
}
60+
}

0 commit comments

Comments
 (0)