-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
87 lines (45 loc) · 1.36 KB
/
Copy pathTest.java
File metadata and controls
87 lines (45 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package revision;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Test {
// • dishes = [["Salad", "Tomato", "Cucumber", "Salad", "Sauce"],
// • ["Pizza", "Tomato", "Sausage", "Sauce", "Dough"],
// • ["Quesadilla", "Chicken", "Cheese", "Sauce"],
// • ["Sandwich", "Salad", "Bread", "Tomato", "Cheese"]]
public static int helper(int arr[],int x,int l,int h){
//out of bound
if(l>h){
return 0;
}
//not possible
if(x<0){
return -1;
}
if (x == 0) {
return 1;
}
int waysleft = 0;
int waysright=0;
if(arr[l]<=x){
waysleft+= helper(arr,x-arr[l],l+1,h); //o
}
else if(arr[h]<=x){
waysright+=helper(arr,x-arr[h],l,h-1); //1
}
System.out.println("left:"+waysleft+"righ:"+waysright);
return Math.min(waysleft,waysright);
}
public static int ways(int arr[], int x) {
return helper(arr, x, 0, arr.length - 1);
}
public static void main(String[] args) {
int arr[] = {1, 1, 4, 2, 3};
System.out.println(ways(arr,5));
}
}