[Geeks for Geeks] Missing Intervals

Missing Intervals

Given a sorted array A[] of N distinct integers from 0 to 99999. Find all the integer intervals missing from the given list.

Note: There is always atleast 1 number missing.

  • Time : O(n)
  • Space : O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
vector<int> printMissingIntervals(int a[], int n) {
vector<int> res;
int last = -1;
for (int i = 0; i < n; i++) {
if (a[i] != last + 1) {
res.push_back(last + 1);
res.push_back(a[i] - 1);
}
last = a[i];
}
if(last != 99999) {
res.push_back(last + 1);
res.push_back(99999);
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/27/PS/GeeksforGeeks/missing-intervals/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.