Find Smallest Integer

Moderate
0/80
Average time to solve is 10m
profile
Contributed by
44 upvotes
Asked in companies
SamsungPaytm (One97 Communications Limited)Uber

Problem statement

You are given an array 'ARR' consisting of 'N' positive numbers and sorted in non-decreasing order, and your task is to find the smallest positive integer value that cannot be represented as a sum of elements of any proper subset of the given array.

An array 'B' is a subset of another array 'A' if each element of 'B' is present in 'A'.

For example:
For the given input array [1, 1, 3],
1 can be represented as the sum of elements of the subset [1],
2 can be represented as the sum of elements of a subset [1, 1],
3 can be represented as the sum of elements of a subset [3],
4 can be represented as the sum of elements of a subset [1, 3],
5 can be represented as the sum of elements of a subset [1, 1, 3]
So, the smallest positive integer value that cannot be represented as a sum of elements of any subset of a given array is 6.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.

The first line of each test case contains an integer ‘N’ representing the size of the input array.

The second line of each test case contains elements of the array separated by a single space.
Output Format:
For each test case, the only line of output prints a single integer representing the smallest positive integer value that cannot be represented as a sum of any subset of the given array.

Output for each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of.
Constraints:
1 <= T <= 100
1 <= N <= 10^4
0 <= arr[i] <= 10^9

Where 'T' represents the number of test cases, 'N' represents the size of the array, and 'arr[i]' represents the elements of the array.  
Time Limit: 1 sec.
Sample Input 1:
2
4
1 2 3 4
2
1 3
Sample Output 1:
11
2
Explanation of Sample Input 1:
For the first test case, the smallest positive integer value that cannot be represented as a sum of elements of any subset of a given array is 11, as the integer from 1 to 10 can be represented as the sum of elements of any subset of the given array.

For the second test case, the possible values of integers that can be represented as the sum of elements of any subset of the given array are [1, 3, 4], the smallest missing positive integer from which is 2.
Sample Input 2:
2
4
1 1 1 1
6
1 2 6 10 11 15
Sample Output 2:
5
4
Hint

Generate all possible subsets and store the corresponding sum of elements for each subset in a HashSet.

Approaches (2)
Brute force Approach

The brute force approach for this problem is explained below:

  1. We can generate all possible 2 ^ N subsets where ‘N’ is the number of elements in the array.
  2. Compute the sum of each subset.
  3. Insert those sums into a HashSet and then
  4. Run a loop i: 1 -> Integer.MAX_VALUE to check if ‘i’ is present in the hashSet. The first element that is missing from the hashSet is our answer.

 

We will use a recursive approach to find a subset, where we backtrack each solution after appending the subset to the resultset. The thought is that if we have N number of elements inside an array, we have exactly two choices for each of them; either we include that element in our subset or we do not include it. So, the take or not take strategy builds a pseudo-binary tree of choices returning only the leaves for each combination resulting in the powerset.

 

Here are the steps to generate subsets:

  1. Check for the base case, i.e., if startIndex > endIndex, then add the current subset-sum into the HashSet.
  2. Recursively call a subset including arr[l] i.e. subsetSums(arr, l + 1, r, arr[l] + sum).
  3. Recursively call a subset excluding arr[l] i.e. subsetSums(arr, l + 1, r, sum).
Time Complexity

O(2 ^ N), where ‘N’ is the size of the given array.

 

Generating subsets is an NP-Complete problem, and so there is no polynomial-time solution for it. Therefore, the time complexity of this approach is O(2 ^ N).

Space Complexity

O(N), where ‘N’ is the size of the given array.

 

Since we are using a HashSet to store the sum of each subset of the given input array thus, the space complexity will be O(N).

Code Solution
(100% EXP penalty)
Find Smallest Integer
Full screen
Console