![]() |
VOOZH | about |
Given an integer K and an array A[] of size M, check if it is possible to obtain the integer K by taking the sum of one or more elements from the array A, with each element used at most once.
Examples:
Input: A[] = {1, 2, 3}, K = 6
Output: True
Explanation: 1 + 2 + 3 = 6Input: A[] = {15, 12, 13, 10}, K = 20
Output: False
Approach: To solve the problem follow the below idea:
The given problem can be solved using the property of recursion, that every array element has 2 choices for the answer:
- Either the array element can be included in the current sum to contribute towards K.
- Or the array element can be skipped to find the total sum as K.
Steps to solve the problem:
Below is the implementation of the above approach:
true
Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(N), Recursive Stack Space