Design a Data Structure that performs add in O(n) and getMinimum & deleteMinimum in O(1) Last Updated : 11 Apr, 2024 Comments Improve Suggest changes Like Article Like Report Design a data structure which can do following operations add() in O(n)getMinimum() in O(1)deleteMinimum() in O(1)Source : MakeMyTrip Interview. Maintain a linkedlist with elements in increasing order.Move head to next position in case of delete Min operation.Return First element in case of get Min Operation.Implementation: C++ #include <iostream> // Node class class Node { public: int data; Node* next; Node(int d) { data = d; next = nullptr; } }; // Main class class MinDS { Node* start; public: MinDS() { start = nullptr; } // Function to add an element void addElement(int d) { Node* tmp = new Node(d); // If the linked list is empty if (start == nullptr) { start = tmp; return; } // If the new element is smaller than the head if (d < start->data) { tmp->next = start; start = tmp; return; } // If the new element needs to be inserted in the middle Node* prev = start; Node* ptr = start->next; while (ptr != nullptr) { if (d < ptr->data) { tmp->next = ptr; prev->next = tmp; return; } else { prev = ptr; ptr = ptr->next; } } prev->next = tmp; } // Function to get the minimum element int getMin() { return start->data; } // Function to delete the minimum element int delMin() { int min = start->data; start = start->next; return min; } // Function to print elements void print() { Node* ptr = start; std::cout << "Elements: "; while (ptr != nullptr) { std::cout << ptr->data << ", "; ptr = ptr->next; } std::cout << "\n"; } }; int main() { MinDS x; x.addElement(10); x.addElement(20); x.addElement(5); x.addElement(15); x.print(); std::cout << "Get Min: " << x.getMin() << std::endl; std::cout << "Del Min: " << x.delMin() << std::endl; x.print(); std::cout << "Min: " << x.getMin() << std::endl; return 0; } Java // Java code for linked list to // perform required operations import java.util.*; // Node class class Node { int data; Node next; Node(int d) { data = d; next = null; } } // main class class MinDS { Node start; public MinDS() { start = null; } // Function to add element void addElement(int d) { Node tmp = new Node(d); // If linked list is empty if (start == null) { start = tmp; return; } // If head itself is greater if (d < start.data) { tmp.next = start; start = tmp; return; } // If need to insert somewhere in middle Node prev = start; Node ptr = start.next; while (ptr != null) { if (d < ptr.data) { tmp.next = ptr; prev.next = tmp; return; } else { prev = ptr; ptr = ptr.next; } } prev.next = tmp; } // Function to get minimum int getMin() { return start.data; } // Function to delete minimum int delMin() { int min = start.data; start = start.next; return min; } // Function to print elements void print() { Node ptr = start; System.out.print("Elements: "); while (ptr != null) { System.out.print(ptr.data + ", "); ptr = ptr.next; } System.out.println("\n"); } // Driver code public static void main(String[] args) { MinDS x = new MinDS(); x.addElement(10); x.addElement(20); x.addElement(5); x.addElement(15); x.print(); System.out.println("Get Min: " + x.getMin()); System.out.println("Del Min: " + x.delMin()); x.print(); System.out.println("Min: " + x.getMin()); } } Python3 # Python code for linked list to perform required operations # Node class class Node: def __init__(self, d): self.data = d self.next = None # Main class class MinDS: def __init__(self): self.start = None # Function to add element def addElement(self, d): tmp = Node(d) # If linked list is empty if self.start is None: self.start = tmp return # If head itself is greater if d < self.start.data: tmp.next = self.start self.start = tmp return # If need to insert somewhere in the middle prev = self.start ptr = self.start.next while ptr is not None: if d < ptr.data: tmp.next = ptr prev.next = tmp return else: prev = ptr ptr = ptr.next prev.next = tmp # Function to get minimum def getMin(self): return self.start.data # Function to delete minimum def delMin(self): min_val = self.start.data self.start = self.start.next return min_val # Function to print elements def printList(self): ptr = self.start print("Elements: ", end="") while ptr is not None: print(ptr.data, end=", ") ptr = ptr.next print("\n") # Driver code if __name__ == "__main__": x = MinDS() x.addElement(10) x.addElement(20) x.addElement(5) x.addElement(15) x.printList() print("Get Min:", x.getMin()) print("Del Min:", x.delMin()) x.printList() print("Min:", x.getMin()) C# using System; // Node class public class Node { public int data; public Node next; // Constructor public Node(int d) { data = d; next = null; } } // Main class public class MinDS { private Node start; // Constructor public MinDS() { start = null; } // Function to add an element public void AddElement(int d) { Node tmp = new Node(d); // If the linked list is empty if (start == null) { start = tmp; return; } // If the new element is smaller than the head if (d < start.data) { tmp.next = start; start = tmp; return; } // If the new element needs to be inserted in the middle Node prev = start; Node ptr = start.next; while (ptr != null) { if (d < ptr.data) { tmp.next = ptr; prev.next = tmp; return; } else { prev = ptr; ptr = ptr.next; } } prev.next = tmp; } // Function to get the minimum element public int GetMin() { if (start == null) { throw new InvalidOperationException("List is empty"); } return start.data; } // Function to delete the minimum element public int DelMin() { if (start == null) { throw new InvalidOperationException("List is empty"); } int min = start.data; start = start.next; return min; } // Function to print elements public void Print() { Node ptr = start; Console.Write("Elements: "); while (ptr != null) { Console.Write(ptr.data + ", "); ptr = ptr.next; } Console.WriteLine(); } } class Program { static void Main(string[] args) { MinDS x = new MinDS(); x.AddElement(10); x.AddElement(20); x.AddElement(5); x.AddElement(15); x.Print(); Console.WriteLine("Get Min: " + x.GetMin()); Console.WriteLine("Del Min: " + x.DelMin()); x.Print(); Console.WriteLine("Min: " + x.GetMin()); } } JavaScript // Node class class Node { constructor(d) { this.data = d; this.next = null; } } // Main class class MinDS { constructor() { this.start = null; } // Function to add element addElement(d) { let tmp = new Node(d); // If linked list is empty if (this.start === null) { this.start = tmp; return; } // If head itself is greater if (d < this.start.data) { tmp.next = this.start; this.start = tmp; return; } // If need to insert somewhere in middle let prev = this.start; let ptr = this.start.next; while (ptr !== null) { if (d < ptr.data) { tmp.next = ptr; prev.next = tmp; return; } else { prev = ptr; ptr = ptr.next; } } prev.next = tmp; } // Function to get minimum getMin() { return this.start.data; } // Function to delete minimum delMin() { let min = this.start.data; this.start = this.start.next; return min; } // Function to print elements print() { let ptr = this.start; let result = "Elements: "; while (ptr !== null) { result += ptr.data + ", "; ptr = ptr.next; } console.log(result + "\n"); } // Driver code static main() { let x = new MinDS(); x.addElement(10); x.addElement(20); x.addElement(5); x.addElement(15); x.print(); console.log("Get Min: " + x.getMin()); console.log("Del Min: " + x.delMin()); x.print(); console.log("Min: " + x.getMin()); } } // Call the main function MinDS.main(); OutputElements: 5, 10, 15, 20, Get Min: 5 Del Min: 5 Elements: 10, 15, 20, Min: 10 Comment More infoAdvertise with us Next Article Design a Data Structure that performs add in O(n) and getMinimum & deleteMinimum in O(1) A amansharma26 Follow Improve Article Tags : Linked List Interview Experiences DSA Experiences Practice Tags : Linked List Similar Reads Design a data structure that supports insert, delete, search and getRandom in constant time Design a data structure that supports the following operations in O(1) time.insert(x): Inserts an item x to the data structure if not already present.remove(x): Removes item x from the data structure if present. search(x): Searches an item x in the data structure.getRandom(): Returns a random elemen 5 min read A data structure for n elements and O(1) operations Propose a data structure for the following:Â The data structure would hold elements from 0 to n-1. There is no order on the elements (no ascending/descending order requirement)Â The complexity of the operations should be as follows:Â Insertion of an element â O(1)Â Deletion of an element â O(1)Â Findi 4 min read Design a data structure that supports insert, delete, getRandom in O(1) with duplicates Design a Data Structure that can support the following operations in O(1) Time Complexity. insert(x): Inserts x in the data structure. Returns True if x was not present and False if it was already present.remove(x): Removes x from the data structure, if present.getRandom(): Returns any value present 9 min read Minimum number of increment/decrement operations such that array contains all elements from 1 to N Given an array of N elements, the task is to convert it into a permutation (Each number from 1 to N occurs exactly once) by using the following operations a minimum number of times: Increment any number.Decrement any number. Examples: Input: arr[] = {1, 1, 4} Output: 2 The array can be converted int 4 min read Design an efficient data structure for given operations To design an efficient data structure for a specific set of operations, it's important to consider the time and space complexity of different data structures and choose the one that is best suited for the specific requirements. For example, if you need to perform operations such as inserting element 15+ min read Like