Category: TrueGeek-2021

Minimum characters to be replaced in Ternary string to remove all palindromic substrings for Q queries

  #include #define SIZE 100005using namespace std;  void preprocess(string& s, string& t,                int prefix[][SIZE],                int n, int i){        prefix[i][0] = (s[0] != t[0]);      for (int j = 1; j < n; j++) {                                  prefix[i][j]            = prefix[i][j - 1]              + (s[j] != t[j % 3]);    }    return;}  void minChangesNonPalindrome(    string str, int N, int Q,    vector queries){          int prefix[6][SIZE];                  vector sequences        = { "012", "021", "102",            "120", "201", "210" };      for (int i = 0; […]

Continue Reading

Introduction to Universal Hashing in Data Structure

Hashing is a great practical tool, with an interesting and subtle theory too. In addition to its use as a dictionary data structure, hashing also comes up in many different areas, including cryptography and complexity theory. This article discusses an important notion: Universal Hashing (also known as universal hash function families).Universal Hashing refers to selecting a […]

Continue Reading

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

Minimize count of steps to make all Matrix elements as 0 by replacing neighbours of K with 0

#include using namespace std;  void traverse(int M, int N,              vector& matrix,              int row, int col){          if (row >= 0 && row < M        && col >= 0 && col < N) {        if (matrix[row][col] == 0) {            return;        }                          matrix[row][col] = 0;                  traverse(M, N, matrix, row, col - 1);                  traverse(M, N, matrix, row, col + 1);                  traverse(M, N, matrix, row - 1, col);                  traverse(M, N, matrix, row + […]

Continue Reading

Maximum length of string formed by concatenation having even frequency of each character

  import java.io.*;import java.util.*;  public class index {    static int max = 0;    static String ans1 = “”;          static void calculate(String ans)    {          int dp[] = new int[26];        for (int i = 0; i < ans.length(); ++i) {                                      dp[ans.charAt(i) - 'A']++;        }                  for (int i = 0; i < dp.length; ++i) {            if (dp[i] % 2 == 1) {                return;            }        }        if (max < ans.length()) {                                      max = ans.length();            ans1 = ans;        }    }                  static […]

Continue Reading