#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 ReadingLength of all prefixes that are also the suffixes of given string Given a string S consisting of N characters, the task is to find the length of all prefixes of the given string S that are also suffixes of the same string S.Examples:Input: S = “ababababab”Output: 2 4 6 8Explanation: The prefixes of S that […]

Continue Reading#include using namespace std; int countSubstrings(string S, string S1, string S2){ int N = S.length(); int N1 = S1.length(); int N2 = S2.length(); int count = 0, totalcount = 0; for (int i = 0; i < N; i++) { string prefix = S.substr(i, N1); string suffix = S.substr(i, N2); if (S1 == prefix) count++; if (S2 == suffix) totalcount += count; } return totalcount;} void checkSubstrings(string S, string S1, string S2){ int […]

Continue Reading#include using namespace std; int GetDiff(int A[], int N) { int SuffMaxArr[N]; SuffMaxArr[N – 1] = A[N – 1]; for (int i = N – 2; i >= 0; –i) { SuffMaxArr[i] = max(SuffMaxArr[i + 1], A[i + 1]); } int MaximumSum = INT_MIN; for (int i […]

Continue Reading#include using namespace std; void findGCDQueries(int arr[], int N, int Queries[][3], int Q) { int prefix[N], suffix[N]; prefix[0] = arr[0]; suffix[N – 1] = arr[N – 1]; for (int i = 1; i < N; i++) { prefix[i] = __gcd(prefix[i - 1], arr[i]); } for (int i = N - 2; […]

Continue Reading