Grand Temple - Run Error

My sample input is giving correct answer but on submitting all test cases are giving run error. Can you please help me with this.

import java.util.*;
public class Main {
public static void main(String args[]) {

	Scanner sc=new Scanner (System.in);

	int n=sc.nextInt();
	int[][] cor=new int[2][n];

	int max=0;
	for (int i=0; i<n; i++) {
		cor[0][i]=sc.nextInt();
		max=Math.max(max,cor[0][i]);

		cor[1][i]=sc.nextInt();
		max=Math.max(max,cor[1][i]);
	}

	char[][] board=new char[max][max];
	for (int i=0; i<max; i++) {
		for (int j=0; j<max; j++) {
			board[i][j]='-';
		}
	}

	for (int i=0; i<n; i++) {

		for (int j=0; j<max; j++) {
			board[cor[0][i]-1][j]='X';
		}

		for (int j=0; j<max; j++) {
			board[j][cor[1][i]-1]='X';
		}
	}

	// for (int i=0; i<max; i++) {
	// 	for (int j=0; j<max; j++) {
	// 		System.out.print (board[i][j]+" ");
	// 	}
	// 	System.out.println ();
	// }

	System.out.println (maxArea (board, 0, 0, new boolean[max][max]));

}

static int maxArea (char[][] board, int i, int j, boolean[][] visited) {

	if (board[i][j]=='X') {

		int a=0, b=0, c=0;

		if (i+1<board.length)
		a=maxArea (board, i+1, j, visited);

		if (j+1<board.length)
		b=maxArea (board, i, j+1, visited);

		if (i+1<board.length && j+1<board.length)
		c=maxArea (board, i+1, j+1, visited);

		return Math.max (a, Math.max (b,c));
	}

	int x=0, y=0, z=0, w=0;

	if (isSafe (board, i+1, j, visited)) {
		visited[i+1][j]=true;
		x= 1 + maxArea (board, i+1, j, visited);
		visited[i+1][j]=false;
	}

	if (isSafe (board, i-1, j, visited)) {
		visited[i-1][j]=true;
		y= 1 + maxArea (board, i-1, j, visited);
		visited[i-1][j]=false;
	}

	if (isSafe (board, i, j+1, visited)) {
		visited[i][j+1]=true;
		z= 1 + maxArea (board, i, j+1, visited);
		visited[i][j+1]=false;
	}

	if (isSafe (board, i, j-1, visited)) {
		visited[i][j-1]=true;
		w= 1 + maxArea (board, i, j-1, visited);
		visited[i][j-1]=false;
	}

	


	return Math.max (Math.max(x,y), Math.max(z,w));
}

static boolean isSafe (char[][] board, int i, int j, boolean[][] visited){

	if (i<0 || j<0 || i==board.length ||j==board.length || visited[i][j] || board[i][j]=='X') {
		return false;
	}

	return true;
}

}

your code will give out of memry error due to exceeding the heap space
In this question you are given that there are some watchtowers at different coordinates (x,y). Now 2 rivers flow horizontally and vertically and they intersect at this tower.

Here you can see that there are 3 towers at (8,6), (3,8) and (11,2) and for each of them 2 rivers (1 horizontal and the other vertical and flowing and intersecting at the respective (x,y)).
Given this arrangement the rest of the space left in the grid(the white portion in image) is land.
Now the king wants to build a temple and he wants to do that on the largest area available. Now you are required to find out and print the area of the largest land which is available and in which the king can build a temple.
Hints :
This is a pretty simple problem. All we need to do is just store all the X and Y coordinates and then sort them. Then we will calculate maximum ΔX and ΔY where ΔX = (Xi -Xi-1) & ΔY = (Yi -Yi-1). Then the area will be (ΔY-1)*(ΔX-1)

Dry run the sample test case.you will get this logic
All we need to do is just store all the X and Y coordinates and then sort them. Then we will calculate maximum ΔX and ΔY where ΔX = (Xi -Xi-1) & ΔY = (Yi -Yi-1). Then the area will be (ΔY-1)*(ΔX-1).

We have to find the maximum area of land(so we exclude the rivers).
Clearly the maximum area would be between the intersection points of (2,4) and (5,2) for the given sample testcase.

Area = | 5 - 2 - 1 | * | 2 - 4 - 1| = 2 * 1 = 2

We implement the formula ,
Area = abs( y2 - y1 - 1) * abs( x2 - x1 - 1)//take abs as area cant be negative

We add an extra -1 in our calculation since we should consider the river area. If we simply implement (y2 - y1) or (x2 - x1) then we would end up counting 1 edge of the vertical river and 1 edge of the horizontal river. We should not include that area as the temple cannot be built over the river edge.

Hope this helps :smile:

This was very helpful. Thank you so much.

Since I have used another approach so I want to understand my mistake in that code too, it would be very helpful if you can provide me some test cases for which I can run my code where I have used recursion and detect the mistakes. :smile:

try test case like
https://cb-s3-bucket-ind-1.s3.ap-south-1.amazonaws.com/testCases/28558265-f5bd-4095-94a5-831a2d0aa6c9
your output shd be
https://cb-s3-bucket-ind-1.s3.ap-south-1.amazonaws.com/testCases/179d52e3-87be-421c-a9fc-3986cf7a7704