Amazon Online Assessment (OA) 2021 - Find All Combination of Numbers that Sum to a Target

Given four arrays of integers and an integer limit. We need to pick 1 number from each of the four arrays such that the sum of the numbers we pick is smaller or equal to limit. Find the number of ways we can pick.

Example

a = [2, 3, 5]
b = [5]
c = [2, 3, 10]
d = [1, 2]
limit = 11

We can pick the numbers in the following four ways: [2, 5, 2, 1], [2, 5, 3, 1], [2, 5, 2, 2], [2, 5, 3, 2]. So return 4.

Try it yourself

Solution