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
A data structure for n elements and O(1) operations?
Here we will see one data structure with n elements, and O(1) operations. So the operations will take constant amount of time to execute.
The data structure will hold n elements (from 0 to n-1). The data can be in any order. The insertion, deletion and searching will take O(1) amount of time.
To solve this problem, we will use one Boolean array. This will indicate that the item is present or not at position i. If the item is present, it will hold 1, otherwise 0.
Syntax
void init(bool dataStructure[], int n); // Initialize array void insert(bool dataStructure[], int i); // Insert element at index i void delete(bool dataStructure[], int i); // Delete element at index i bool search(bool dataStructure[], int i); // Search element at index i
Algorithm
initialization(n)
begin
fill all elements of the Boolean array as 0
end
insert(i)
begin
set element at index i as 1(true)
end
delete(i)
begin
set element at index i as 0(false)
end
search(i)
begin
return item at position i
end
Example: Boolean Array Implementation
Here is a complete implementation of the O(1) data structure using a Boolean array −
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 10
bool dataStructure[MAX_SIZE];
/* Initialize the data structure */
void init(int n) {
for (int i = 0; i < n; i++) {
dataStructure[i] = false;
}
printf("Data structure initialized for %d elements
", n);
}
/* Insert element at index i */
void insert(int i) {
if (i >= 0 && i < MAX_SIZE) {
dataStructure[i] = true;
printf("Inserted element at index %d
", i);
}
}
/* Delete element at index i */
void delete(int i) {
if (i >= 0 && i < MAX_SIZE) {
dataStructure[i] = false;
printf("Deleted element at index %d
", i);
}
}
/* Search element at index i */
bool search(int i) {
if (i >= 0 && i < MAX_SIZE) {
return dataStructure[i];
}
return false;
}
/* Display current state */
void display(int n) {
printf("Current state: ");
for (int i = 0; i < n; i++) {
printf("%d ", dataStructure[i] ? 1 : 0);
}
printf("
");
}
int main() {
int n = 6;
/* Initialize data structure */
init(n);
display(n);
/* Insert some elements */
insert(0);
insert(2);
insert(4);
display(n);
/* Search for elements */
printf("Search index 2: %s
", search(2) ? "Found" : "Not Found");
printf("Search index 3: %s
", search(3) ? "Found" : "Not Found");
/* Delete an element */
delete(2);
display(n);
return 0;
}
Data structure initialized for 6 elements Current state: 0 0 0 0 0 0 Inserted element at index 0 Inserted element at index 2 Inserted element at index 4 Current state: 1 0 1 0 1 0 Search index 2: Found Search index 3: Not Found Deleted element at index 2 Current state: 1 0 0 0 1 0
Time Complexity Analysis
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Initialization | O(n) | O(n) |
| Insert | O(1) | O(1) |
| Delete | O(1) | O(1) |
| Search | O(1) | O(1) |
Key Points
- This data structure is ideal when you need to track presence/absence of elements from a fixed range [0, n-1].
- All operations except initialization run in constant time O(1).
- Space complexity is O(n) for storing the Boolean array.
- The approach works best when the range of possible values is small and known in advance.
Conclusion
The Boolean array data structure provides O(1) insert, delete, and search operations by directly mapping indices to array positions. While initialization takes O(n) time, all subsequent operations are constant time, making it efficient for scenarios requiring frequent lookups on a fixed range of values.
