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

Continue Readingconst MAX = 100005; function isPerfect(N) { let sum = 1; for (let i = 2; i * 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 ReadingQueries to check if array elements from indices [L, R] forms an Arithmetic Progression or notGiven an array arr[] consisting of N integers and an array Q[][2] consisting of M queries of the form {L, R}, the task for each query is to check if array elements over the range [L, R] forms an Arithmetic […]

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

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