Category: Advanced Data Structure

Length of Longest Increasing Subsequences (LIS) using Segment Tree

#include using namespace std;  #define M 100000  vector tree(4 * M + 1);  void update_tree(int start, int end,                 int update_idx, int length_t,                 int count_c, int idx){            if (start == end        && start == update_idx) {        tree[idx].first            = max(tree[idx].first, length_t);        tree[idx].second = count_c;        return;    }          if (update_idx < start        || end < update_idx) {        return;    }          int mid = (start + end) / 2;      update_tree(start, mid, update_idx,                length_t, count_c,                2 * idx);    update_tree(mid + 1, end, update_idx,                length_t, […]

Continue Reading

Search a string in the dictionary with a given prefix and suffix for Q queries

  #include using namespace std;  struct Trie {    Trie* arr[27] = { NULL };              int idx;};  Trie* root = new Trie();  void insert(string word, int i){            Trie* temp = root;              for (char ch : word) {                          if (temp->arr[ch – ‘a’] == NULL) {            Trie* t = new Trie();            temp->arr[ch – ‘a’] = t;        }          temp = temp->arr[ch – ‘a’];        temp->idx = i;    }}  int search(string word){    Trie* temp = root;              for (char ch […]

Continue Reading

Program to implement Hash Table using Open Addressing

#include #include   struct HashNode {    int key;    int value;};  const int capacity = 20;int size = 0;  struct HashNode** arr;struct HashNode* dummy;  void insert(int key, int V){      struct HashNode* temp        = (struct HashNode*)malloc(sizeof(struct HashNode));    temp->key = key;    temp->value = V;              int hashIndex = key % capacity;          while (arr[hashIndex] != NULL           && arr[hashIndex]->key != key           && arr[hashIndex]->key != -1) {        hashIndex++;        hashIndex %= capacity;    }              if (arr[hashIndex] == NULL        || arr[hashIndex]->key == -1)        size++;      arr[hashIndex] = […]

Continue Reading

Queries to calculate sum of squares of array elements over range of indices [L, R] with updates

   #include using namespace std;    vector seg(400000, 0),     dseg(400000, 0),     a(100000);    vector segSum(400000, 0);    void build(int ind, int low, int high) {          if (low == high) {                     seg[ind] = a[low] * a[low];         segSum[ind] = a[low];         return;     }             int mid = (low + high) / 2;         […]

Continue Reading

Queries to count array elements from a given range having a single set bit

#include using namespace std; bool check(int N) {     if (N == 0)         return 0;     return !(N & (N – 1)); } void build_seg_tree(int ss, int se, int si,                     int tree[], int arr[]) {          if (ss == se) {                  tree[si] = check(arr[ss]);         return;     }          int mid = (ss + se) / 2;      […]

Continue Reading