Last Updated: 21 Jan, 2022

Minimum Elements

Moderate
Asked in companies
ShareChatIBMCogoport

Problem statement

You are given an array of ‘N’ distinct integers and an integer ‘X’ representing the target sum. You have to tell the minimum number of elements you have to take to reach the target sum ‘X’.

Note:
You have an infinite number of elements of each type.
For example
If N=3 and X=7 and array elements are [1,2,3]. 
Way 1 - You can take 4 elements  [2, 2, 2, 1] as 2 + 2 + 2 + 1 = 7.
Way 2 - You can take 3 elements  [3, 3, 1] as 3 + 3 + 1 = 7.
Here, you can see in Way 2 we have used 3 coins to reach the target sum of 7.
Hence the output is 3.
Input Format :
The first line of the input contains an integer, 'T’, denoting the number of test cases.

The first line of each test case will contain two space-separated integers ‘N’ and ‘X’, denoting the size of the array and the target sum.

The second line of each test case contains ‘N’ space-separated integers denoting elements of the array.
Output Format :
For each test case, print the minimum number of coins needed to reach the target sum ‘X’, if it's not possible to reach to target then print "-1".

Print a separate line for each test case.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 15
1 <= nums[i] <= (2^31) - 1
1 <= X <= 10000

All the elements of the “nums” array will be unique.
Time limit: 1 sec

Approaches

01 Approach

The basic idea is to go over recursively to find the way such that the sum of chosen elements is ‘X’. For every element, we have two choices

 

1. Include the element in our set of chosen elements.

2. Don’t include the element in our set of chosen elements.

 

When we include an element in the set then we decrease the ‘X’ by the value of the element at the end. At any point, if we have X=0 then we can say that we have found the set of integers such that the sum of the set is equal to ‘X’.

 

Let us define a function “minimum( sum, num)”, where “sum” is the remaining sum that we have to make, “num” is the array that is given to us. Each time we have ‘n’ elements from which we have to take one.

If the function “minimum( sum, num)” returns INT_MAX which means that we cannot make the target sum ‘X’. 

 

Here is the algorithm:

  • minimum function:
    • If sum is less than 0
      • return INT_MAX
    • If sum is 0
      • Return 0
    • Declare an integer variable “ans” and initialize it with INT_MAX.
    • Iterate over n(say ‘j’)
      • Update ans as ans=min(ans ,1+ minimum(sum-num[j],  num))
    • Return ans
  • given function:
    • ans= numberOfWays( X, n, num).
    • If ans == INT_MAX
      • Return -1

                   Return ans

02 Approach

The basic idea is to store the results of the recursive calls to avoid repetition. We can memorize the results of our function calls in an array (say, ‘DP’).

 

So, let's first define dp[i]:

Dp[i] is the minimum number of elements that we need to make a sum equal to ‘i’.

 

Let us define a function “minimum( sum, num,dp)”, where “sum” is the remaining sum that we have to make, num is the array which is given to us and “dp” array is used to memorize the results. 

 

Here is the algorithm :

  • minimum function:
    • If sum is less than 0
      • return INT_MAX
    • If sum is 0
      • Return 0
    • If dp[sum] != -1
      • Return dp[sum]
    • Declare an integer variable “ans” and initialize it with INT_MAX.
    • Iterate over n(say ‘j’)
      • Update ans as ans=min(ans ,1+ minimum(sum-num[j],  num, dp))
    • Update dp[sum] as ans.
    • Return ans.

 

  • given function:
    • Declare an array “dp” of size ‘X’ + 1 and initialize it with -1.
    • ans= minimum( X, num, dp).
    • If ans == INT_MAX
      • Return -1

                   Return ans

03 Approach

The idea is similar to the previous approach. In this approach, we use an iterative way to store the minimum number of elements that are required to make the target sum ‘X’.

 

dp[i]= the minimum number of elements to make sum ‘i’.

 

If we already have calculated the minimum number of elements to make sum [0,1,..,i-1] then we can iterate over all the elements of the array and try to put an ith element of the array as the last element to make sum equal to ‘i’.

 

Here is the algorithm :

  • Declare an integer variable ‘n’ and initialize it as the size of the array.
  • Declare an array “dp” of size “X” + 1 and initialize it with INT_MAX.
  • Set dp[0] = 0
  • Run a loop from ‘i’ =1 to ‘i’ = X
    • Run another loop for iterating over all the elements of the array, say iterator ‘j’
      • If i-num[j] >= 0 && dp[i - num[j]] != INT_MAX
      • Update dp[i] as min(dp[i], 1+dp[i- num[j]])
  • Return dp[X]