Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Print missing elements that lie in range 0 – 99
In C programming, finding missing elements within a specific range is a common problem. Given an array of integers, we need to identify which numbers from 0 to 99 are missing and display them either as individual numbers or as ranges.
Syntax
bool flag[MAX] = { false };
for (i = 0; i < n; i++) {
if (array[i] >= 0 && array[i] < 100) {
flag[array[i]] = true;
}
}
Algorithm
- Create a boolean array
flag[100]initialized tofalse - Mark positions as
truefor elements present in the input array (within range 0-99) - Traverse the boolean array and identify consecutive missing numbers
- Print individual numbers or ranges based on consecutive missing elements
Example
The following program demonstrates how to find and print missing elements in the range 0-99 −
#include <stdio.h>
#include <stdbool.h>
#define MAX 100
int main() {
int array[] = {88, 105, 3, 2, 200, 0, 10};
bool flag[MAX] = { false }; // Initialize all values as false
int i, j, n;
n = sizeof(array)/sizeof(array[0]);
// Mark present elements in range 0-99
for (i = 0; i < n; i++) {
if (array[i] < 100 && array[i] >= 0) {
flag[array[i]] = true;
}
}
// Find and print missing elements
for (i = 0; i < MAX; ++i) {
if (flag[i] == false) { // Found missing element
j = i + 1;
// Find end of consecutive missing range
while (j < MAX && flag[j] == false) {
j++;
}
if (j == i + 1) {
// Single missing number
printf("%d<br>", i);
} else {
// Range of missing numbers
printf("%d-%d<br>", i, j-1);
}
i = j - 1; // Skip to end of current range
}
}
return 0;
}
1 4-9 11-87 89-99
How It Works
- The boolean array
flag[100]acts as a marker for numbers 0-99 - Elements outside the range (105, 200) are ignored during marking
- The algorithm efficiently groups consecutive missing numbers into ranges
- Time complexity is O(n + 100) where n is the array size
Conclusion
This approach uses a boolean array to efficiently track missing elements in a fixed range. It handles both individual missing numbers and consecutive ranges, providing a clear output format for the missing elements.
Advertisements
