Can someone please help to resolve this question?
I got this in a online techical round in a shared document (not google).
Return a List of integers representing the weights of passengers whose combined weight will leave exactly 300 lbs on the vehicle.
Example- Input:
Max Capacity - 500lb
Weights - [130, 70, 140. 100]
Output - [1, 2]
A transport vehicle with max 500lb capacity, list of passengers weighing [130, 70, 140. 100] ,shd return [130, 70] to leave exactly 300lb for vehicle safety. Is it a Knapsack problem or two sum? I wasn't able to figure out.
//java code
public static ArrayList weights(int capacity, ArrayList passangerweights) {
resultList = null;
int rem_space = 300;
List<Integer> passangerList = passangerweights;
int[] passangerArray = new int[passangerList.size()];Integer[] arr = new Integer[passangerList.size()];
arr = passangerList.toArray(arr);
//looping through passanger list
for (int psg : passangerList) {
for (int i = 0; i < arr.length-1; i++) {
if (capacity - (arr[i] + arr[i + 1]) == rem_space) {
resultList.add(i, arr[i]);;
} else {
continue;
}
}
}
return (ArrayList<Integer>) resultList;
}