My code is working for some scenarios, but it is not working for the following inputs
- Target = 10
- No of elements = 4
- elements are = 2,3,3,4.
It returns no elements available. Could anyone please help here?
arr = []
target = int(input("Enter the target: "))
n = int(input("Enter the number of elements: "))
for i in range(n):
arr.append(int(input()))
sorted_arr = sorted(arr)
selected = []
sum = 0
for num in sorted_arr:
if sum+num <= target:
sum += num
selected.append(num)
su = 0
for nu in selected:
su += nu
if su < target:
selected.clear()
if len(selected) == 0:
print("No elements available")
else:
print(*selected)