find the smallest integer which cannot be represented as the sum of any of the subset of the given array.
💻 coding

find the smallest integer which cannot be represented as the sum of any of the subset of the given array.

1 min read 125 words
1 min read
ShareWhatsAppPost on X
  • 1The problem involves finding the smallest positive integer not representable as the sum of any subset of a sorted array.
  • 2The solution requires an O(n) time complexity algorithm to efficiently determine the result.
  • 3In the provided example, the smallest integer that cannot be represented is 10, calculated from the array {1, 1, 3, 4}.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The problem involves finding the smallest positive integer not representable as the sum of any subset of a sorted array."

find the smallest integer which cannot be represented as the sum of any of the subset of the given array.

We have been given a sorted array in increasing order of only positive values, now it is required to find the smallest positive integer value which cannot be represented as the sum of elements of any subset of given set.

The requirement is to solve the problem in time complexity is O(n).

public class SmallestIntegerThatIsNotSumInArray {

	private static int FindSmallestInteger(int[] arr) {
		int res = 1;
		for(int i=0;i< arr.length && arr[i]<= res;i++) {
			res += arr[i];
		}
		return res;
	}
	
	public static void main(String[] args) {
		
		int arr2[] = {1, 1, 3, 4};
		
		long startTime = System.nanoTime();
		
		System.out.println(FindSmallestInteger(arr2));
		
		long endTime = System.nanoTime();
		long totalTime = endTime - startTime;
		System.out.println("Total Time (nanoseconds) : " + (totalTime));
	}
}

output :

10
Total Time (nanoseconds) : 288471

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 15 July 2018 · 1 min read · 125 words

Part of AskGif Blog · coding

You might also like