LeetCode Entry
2996. Smallest Missing Integer Greater Than Sequential Prefix Sum
First prefix sum not in the array
2996. Smallest Missing Integer Greater Than Sequential Prefix Sum easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1448
Problem TLDR
First prefix sum not in the array
Intuition
- brute force, only 50 elements
- max is 50, max prefix sum 50^2
- check only the current position: n[i] == n[0] + i
- use bitmask instead of a set
- don’t check sum if it is bigger than max
Approach
- kotlin: (sum…) - n.toSet() then take first
- rust: (n[0]..) gives the expected sequence
Complexity
-
Time complexity: \(O(n^2)\)
-
Space complexity: \(O(1)\)
Code
fun missingInteger(n: IntArray) =
((n.indices.takeWhile{n[it]==n[0]+it}.sumOf{n[it]}..2500)-n.toSet())[0]
pub fn missing_integer(n: Vec<i32>) -> i32 {
(n.iter().zip(n[0]..).take_while(|(a, b)| *a == b).map(|x| x.1).sum()..)
.find(|x| !n.contains(x)).unwrap()
}
Comments