Advertisements
Two cells is a matrix will be called connected if they are adjacent on either x-axis or y-axis (but not diagonally) For. eg., a[0,0] will be connected to both cells a[0,1] and a[1,0]. But it will not connected to cell a[1,1]
For this problem, lets say, cells denoted by integer <= 0 represent Water and cells denoted by non-zero positive integer represent Land. An Island is a connected land cells. Weight of an island is the sum of all integers in that island.
1 0 0 0 1 0 1 2 2 1 0 2 0 0 0 1
In above example matrix, there are two islands { a[0,0], a[0,1], a[0,2], a[1,2] } with weight of 5 units { a[2,1], a[3,1], a[3,2], a[3,3] }, with weight of 6 units
heaviest island is { a[2,1], a[3,1], a[3,2], a[3,3] } elements with weight 6
Problem: Implement Java code which takes 2 dimensional integer array as input and prints out heaviest island.