How to give condition for n if i try to solve with recursion first

import java.util.*;
public class Main {
public static void main(String args[]) {
// Your Code Here
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int w = scan.nextInt(); //Weight we want
int[] price = new int[w];
int n = price.length; // to traverse array
for(int i = 0; i< price.length ; i++){
price[i] = i+1;
}
System.out.println(n + " "+ w);
System.out.println(minPrice(n,price, w));
}
public static int minPrice(int n, int[] price, int w){
if(n==0){
if(w==0){
return 0;
}
}
if(w==0 ){
return 0;
}

	int incl = Integer.MAX_VALUE;
	int excl = Integer.MAX_VALUE;
	if(price[n-1]<=w && n>0){
		 incl = price[n-1] + minPrice(n,price,w-price[n-1]);
		
	}
	//exclude
		 excl = minPrice(n-1,price,w);
		if(incl< excl && w==0){
			 
			return incl;
		}
		if(excl<incl){
			System.out.println(incl + "**"+ excl + w);
			return excl;
		}
	
	return -1;
}

}

import java.util.*; public class Main { public static void main(String args[]) { // Your Code Here Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int w = scan.nextInt(); //Weight we want int[] price = new int[w]; int n = price.length; // to traverse array for(int i = 0; i< price.length ; i++){ price[i] = scan.nextInt(); } System.out.println(minPrice(n,price, w)); } public static int minPrice(int n, int[] price, int w){ if(n==0 && w!=0){ return -1; } if(w==0){ return 0; } int incl = Integer.MAX_VALUE; int excl = Integer.MAX_VALUE; if(n-1<=w && price[n-1]!=-1){ incl = price[n-1] + minPrice(n,price,w-(n)); } //exclude excl = minPrice(n-1,price,w); if(incl!=-1 && excl!=-1){ return(java.lang.Math.min(incl,excl)); }else if(incl==-1 && excl!=-1){ return excl; }else if(excl==-1&&incl!=-1){ return incl; }else{ return -1; } } }

Recursively it will not work as constraints are high. Its is same as 0/1 knapsack ,you have to just think for minimizing. Recursively it will a lots of call as for every element you have to check whether to take or not .
You can check my solution for better understanding , its same as filling the 2D Dp for a general knapsack .

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.