Skip to content

Commit 4f962b8

Browse files
author
Tushar Roy
committed
smallest integer not subset sum
1 parent 07e61fa commit 4f962b8

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# http://www.geeksforgeeks.org/find-smallest-value-represented-sum-subset-given-array/
2+
3+
def find_smallest_integer(input):
4+
result = 1
5+
for i in range(len(input)):
6+
if input[i] <= result:
7+
result += input[i]
8+
else:
9+
break
10+
return result
11+
12+
if __name__ == '__main__':
13+
input = [1, 2, 3, 8]
14+
print(find_smallest_integer(input))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.interview.array;
2+
3+
/**
4+
* Date 12/31/2015
5+
* @author Tushar Roy
6+
*
7+
* Given array in non decreasing order find smallest integer which cannot be represented by
8+
* subset sum of these integers.
9+
*
10+
* Time complexity is O(n)
11+
*
12+
* http://www.geeksforgeeks.org/find-smallest-value-represented-sum-subset-given-array/
13+
*/
14+
public class SmallestIntegerNotRepresentedBySubsetSum {
15+
16+
public int findSmallestInteger(int input[]) {
17+
int result = 1;
18+
for (int i = 0; i < input.length; i++) {
19+
if (input[i] <= result) {
20+
result += input[i];
21+
} else {
22+
break;
23+
}
24+
}
25+
return result;
26+
}
27+
28+
public static void main(String args[]) {
29+
int input[] = {1, 2, 3, 8};
30+
SmallestIntegerNotRepresentedBySubsetSum ss = new SmallestIntegerNotRepresentedBySubsetSum();
31+
System.out.print(ss.findSmallestInteger(input));
32+
}
33+
}

0 commit comments

Comments
 (0)