Monday, 29 Nov 2021
Category: array-range-queries

#include using namespace std;  int totalValidArrays(int a[], int b[],                     int N){        int dp[N + 1][b[N – 1] + 1];          int pref[N + 1][b[N – 1] + 1];          memset(dp, 0, sizeof(dp)),        memset(pref, 0, sizeof(pref));          dp[0][0] = 1;          for (int i = 0; i

#include using namespace std;  void countTriplets(int size, int queries,                   int arr[], int Q[][2]){          int arr_even[size + 1], arr_odd[size + 1];          int even = 0, odd = 0;    arr_even[0] = 0;    arr_odd[0] = 0;          for (int i = 0; i < size; i++) {                  if (arr[i] % 2) {            odd++;        }                  else {            even++;        }                          arr_even[i + 1] = even;        arr_odd[i + 1] = odd;    }          for (int i = 0; i < […]

Minimum absolute value of (K – arr[i]) for all possible values of K over the range [0, N – 1]Given a positive integer N and a sorted array arr[] consisting of M integers, the task is to find the minimum absolute value of (K – arr[i]) for all possible values of K over the range […]

#include using namespace std;  class SpecialAverage {public:                multiset left, mid, right;      int n, k;                      long pos, sum;          vector v;                  SpecialAverage(int nvalue, int kvalue)    {        n = nvalue;        k = kvalue;        pos = 0;        sum = 0;        for (int i = 0; i < n; i++)            v.push_back(0);    }                  void add(int num)    {                        left.insert(num);                          if (left.size() > k) {                                                  int temp = *(prev(end(left)));                                      mid.insert(temp);                                      left.erase(prev(end(left)));                                      sum += temp;        }                          if (mid.size() > (n – 2 * k)) {                                                  int temp = *(prev(end(mid)));                                      right.insert(temp);                                      mid.erase(prev(end(mid)));                                      sum […]

#include using namespace std;  int prefixCount[32][10000];  void findPrefixCount(vector arr,                     int size){        for (int i = 0; i < 32; i++) {                          prefixCount[i][0]            = ((arr[0] >> i) & 1);                          for (int j = 1; j < size; j++) {                          prefixCount[i][j]                = ((arr[j] >> i) & 1);            prefixCount[i][j]                += prefixCount[i][j – 1];        }    }}  void arrayBitwiseAND(int size){        int result = 0;          for (int i = 0; i < 32; i++) {                          int temp = […]

#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;         […]

Print modified array after performing queries to add (i – L + 1) to each element present in the range [L, R] Given an array arr[] consisting of N 0s (1-based indexing) and another array query[], with each row of the form {L, R}, the task for each query (L, R) is to add a […]

#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; […]