Category: array-range-queries

Count of non decreasing Arrays with ith element in range [A[i], B[i]]

#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

Continue Reading

Count of even sum triplets in the array for Q range queries

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

Continue Reading

Queries to calculate average of an array after removing K smallest and largest elements with updates

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

Continue Reading

Queries to calculate Bitwise AND of an array with updates

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

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 calculate GCD of an array after multiplying first or last K elements by X

#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