Skip to content

Commit bd867fc

Browse files
authored
Merge pull request #38 from sakshi1432/patch-5
Finds missing elements in a range in array
2 parents bb7b303 + ecb657c commit bd867fc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

missing-elem-range.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// A hashing based C++ program to find missing
2+
// elements from an array
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
// Print all elements of range [low, high] that
7+
// are not present in arr[0..n-1]
8+
void printMissing(int arr[], int n, int low,
9+
int high)
10+
{
11+
// Insert all elements of arr[] in set
12+
unordered_set<int> s;
13+
for (int i=0; i<n; i++)
14+
s.insert(arr[i]);
15+
16+
// Traverse throught the range an print all
17+
// missing elements
18+
for (int x=low; x<=high; x++)
19+
if (s.find(x) == s.end())
20+
cout << x << " ";
21+
}
22+
23+
// Driver program
24+
int main()
25+
{
26+
int arr[] = {1, 3, 5, 4};
27+
int n = sizeof(arr)/sizeof(arr[0]);
28+
int low = 1, high = 10;
29+
printMissing(arr, n, low, high);
30+
return 0;
31+
}

0 commit comments

Comments
 (0)