K ordered lcs problem

sir i code in java ,it is showinh TLE erroe while submitting on hackerearth plz help
my code is

import java.util.;
import java.io.
;

public class KOrderedLcsHE {
static class FastReader {
BufferedReader br;
StringTokenizer st;

	public FastReader() {
		br = new BufferedReader(new InputStreamReader(System.in));
	}

	String next() {
		while (st == null || !st.hasMoreElements()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	int nextInt() {
		return Integer.parseInt(next());
	}

	long nextLong() {
		return Long.parseLong(next());
	}

	double nextDouble() {
		return Double.parseDouble(next());
	}

	String nextLine() {
		String str = "";
		try {
			str = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}
}

static int a[], b[];

static int klcsTD(int i, int j, int k, int dp[][][]) {

	if (i == a.length || j == b.length)
		return 0;
	int ans = 0;
	if (dp[i][j][k] != -1)// use -1 instead
	{
		System.out.println("hi");
		return dp[i][j][k];
	}
	if (a[i] == b[j])
		return 1 + klcsTD(i + 1, j + 1, k, dp);

	else {
		if (k > 0)
			ans = 1 + klcsTD(i + 1, j + 1, k - 1, dp);
		ans = Math.max(ans, klcsTD(i + 1, j, k, dp));
		ans = Math.max(ans, klcsTD(i, j + 1, k, dp));

		return dp[i][j][k] = ans;

	}
}

public static void main(String[] args) {
	// TODO Auto-generated method stub
	FastReader sc = new FastReader();
	int n = sc.nextInt();
	int m = sc.nextInt();
	int k = sc.nextInt();
	a = new int[n];
	b = new int[m];
	for (int i = 0; i < n; i++)
		a[i] = sc.nextInt();
	for (int i = 0; i < m; i++)
		b[i] = sc.nextInt();
	int dp[][][] = new int[n][m][k + 1];
	for (int[][] row : dp) {
		for (int[] rowColumn : row) {
			Arrays.fill(rowColumn, -1);
		}
	}

	System.out.println(klcsTD(0, 0, k, dp));

}

}

Please try to provide the question link too

Hi

are you able to submit your code on hacker blocks?

Hi

It is showing TLE because it cannot complete recursion.

check out these small changes in your code i have done:

https://ide.codingblocks.com/s/70272

Hit like if you get it!
Cheers :stuck_out_tongue:

yes all test cases are passed there bt not on hackerearth.

Hi

share link of problem on hackerearth

The link is-
https://www.hackerearth.com/submission/25477955/

Hi

my C++ code is passing all the test cases.

Sir still not got the my mistake plz help

Hi

check this code:
https://www.hackerearth.com/submission/25592067/

it is accepted in java.

( in recursion you are directly returning the value and not storing it)

changes in line number 84 and 111.

Hit like if you get it!
Cheers