Tilling Problem 2

import java.util.*;
public class Main {
	public static int mod = 1000000007;
	public static int f(int n, int m) {
		// base case
		if (n == 1 || n == 0) return 1;
		// recursion
		int x = 0, y = 0;
		if (n >= m) x = f(n - m, m);
		y = f(n - 1, m);
		return (x % mod + y % mod) % mod;
	}
    public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while (t != 0) {
			t -= 1;
			int n = sc.nextInt();
			int m = sc.nextInt();
			System.out.println(f(n, m));
		}
    }
}

What is worng with my approach except TLE since n is very large.
It’s giving me Run time error i don’t know why.

hello @vinayveeren

her ein place of f(n>=m) it shuld be if(n>=m)

also pls share ur complete code,

@vinayveeren

pls go to this link -> https://ide.codingblocks.com/
paste ur code in the editor ,press ctrl + s and then save
a url will be generated in ur search bar, share that url with me

its correct code, just optimise it wil work

1 Like

It’s not giving me TLE when i submit the code. It’s giving Run time error,

probably becuase of too many recusive calls, it exceeds the stack memory limit.

once try optimising it and see if it is giving same verdict or different

1 Like

Okay. Thank you @aman212yadav

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.