Largest Square House in java

You are given matrix 1's represent trees and 0's represent empty space, now you have to build a house in space such that it should form grid/square, then return the dimension of grid/square.



import java.util.*;
/**
* This program is about find a house, where we can build among the trees.
* here trees are represented as 1's and empty spaces as 0's. You need to
* find a grid based on empty spaces.
* @author Cerebri
*
*/
public class LargestSquareHouse {
public static String largestsquarehouse(int[][] input1)
{
String value=null;
int x=0;int r=0,c=0,count=0;
int rows=input1.length;int columns=input1[0].length;
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
if(x==input1[i][j]){
count++; //counts only zeros.
}
else{
count=0; //reset counter if there is 1 in between;
}
}
if(count>1){
r++; //it calculates row counts after successful zero's
c=count; //it calculates column counts based on zero's
}
count=0; //resetting the counter to check in next row of the matrix.
}
value=r+"$"+c;
return value;
}
public static void main(String [] args){
Scanner in = new Scanner(System.in);
String output;
int ip1_rows = 0;
int ip1_cols = 0;
ip1_rows = Integer.parseInt(in.nextLine().trim());
ip1_cols = Integer.parseInt(in.nextLine().trim());
int[][] ip1 = new int[ip1_rows][ip1_cols];
for(int ip1_i=0; ip1_i<ip1_rows; ip1_i++) {
for(int ip1_j=0; ip1_j<ip1_cols; ip1_j++) {
ip1[ip1_i][ip1_j] = in.nextInt();
}
}
output = largestsquarehouse(ip1);
System.out.println(String.valueOf(output));
}
}

Input
4
5
1 1 1 0 0
1 1 1 0 0
0 1 0 1 0
0 0 1 1 1

Output: 2$2.

Input:
4
5
1 1 0 0 0
1 1 0 0 0
0 1 0 1 0
0 0 1 1 1

Output: 2$3.

first value indicates row and after $ indicates column.

Comments

Popular posts from this blog

Reasoning-Number Series

Profit and Loss

Reasoning-Letter Series