Large values in segment tree for product range queries - algorithm

I wrote code for firing product range queries on an array.
Note: This question not a duplicate to Multiplication in a range. My problem is something different
I wrote the code for this,
// Program to show segment tree operations like construction, query and update
#include <stdio.h>
#include <math.h>
#define R 1000000000
typedef unsigned long long ull;
int getMid(int s, int e) { return s + (e -s)/2; }
ull getProdUtil(ull *st, int ss, int se, int qs, int qe, int index)
{
if (qs <= ss && qe >= se)
return st[index];
if (se < qs || ss > qe)
return 1;
int mid = getMid(ss, se);
return (getProdUtil(st, ss, mid, qs, qe, 2*index+1) %R *
getProdUtil(st, mid+1, se, qs, qe, 2*index+2) % R )%R;
}
ull getProd(ull *st, int n, int qs, int qe)
{
if (qs < 0 || qe > n-1 || qs > qe)
{
return 0;
}
return getProdUtil(st, 0, n-1, qs, qe, 0) % R;
}
ull constructSTUtil(ull arr[], int ss, int se, ull *st, int si)
{
if (ss == se)
{
st[si] = arr[ss];
return arr[ss];
}
int mid = getMid(ss, se);
ull l = (ull)constructSTUtil(arr, ss, mid, st, si*2+1)%R;
ull r = (ull)constructSTUtil(arr, mid+1, se, st, si*2+2)%R;
st[si] = (l * r) % R;
return st[si];
}
void init_array(ull *st, ull size){
for(ull i=0;i< size;i++){
st[i] = 1;
}
}
ull *constructST(ull arr[], int n)
{
int x = (int)(ceil(log2(n))); //Height of segment tree
int max_size = 2*(int)pow(2, x) - 1; //Maximum size of segment tree
ull *st = new ull[max_size];
init_array(st, max_size);
constructSTUtil(arr, 0, n-1, st, 0);
return st;
}
void print_array(ull *array,int size){
printf("\n");
for(int i=0; i< size;i++){
printf("<%d,%llu> ", i+1,array[i]);
}
}
int main()
{
int n;
scanf("%d",&n);
ull arr[n];
for(int i=0;i<n;i++){
scanf("%llu",&arr[i]);
}
ull *st = constructST(arr, n);
int t;
scanf("%d", &t);
while(t--){
int l,m,r;
scanf("%d %d %d", &l,&r,&m);
ull result = getProd(st, n, l-1, r-1);
printf("%lld\n",result % m);
//int sizes = 100;
//printf("%llu\n",st[0]);
//print_array(st, sizes);
}
return 0;
}
The constraints are,
1 ≤ N ≤ 100,000 (size of array)
1 ≤ Ai ≤ 100 (array elements)
1 ≤ T ≤ 100,000 (number of test cases)
1 ≤ Li ≤ Ri ≤ N (Left and right ranges in query)
1 ≤ Mi ≤ 10<sup>9</sup> (Modulus for each test case. Each test case has to be modulu of this number)
Im pretty sure that R , that I have chosen is failing some test case. I tried R with 1018 as well. But still same problem. Dont know why this is happening?
My question is , is it the problem of R I have chosen or is it the problem of different M being passed in each test case.
Note: Truly not expecting a solution, just expecting a clue
Regards

Related

Using heap sort, append an array elements

I have given an array int A[] = {12,10,9,2,11,8,14,3,5};
In this array, 1st 4 elements(from index 0 to index 3) follow max heap condition. But last 5 elements(index 4 to index 8) don't follow max heap condition. So, I have to write a code so that the whole array follow max heap condition.
I have given a function call max_heap_append(A,3,8); and I have to use it in my code to write the program. It is an assignment so I have to follow the instruction.
I have written this code bellow but when I run the program, nothing happens.
#include <stdio.h>
#include <stdlib.h>
void swap(int * a, int * b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void heapify( int A[], int q, int i)
{
int largest = i;
int l = 2 * i + 1 ;
int r = 2 * i + 2;
if( l < q && A[l] > A[largest])
{
largest = l;
}
if( r < q && A[r] > A[largest])
{
largest = r;
}
if( largest != i)
{
swap( &A[i] , &A[largest]);
heapify(A, q, largest);
}
}
void max_heap_append(int A[], int p , int q)
{
int i;
for( i = q / 2 -1; i >= 0; i--)
{
heapify( A , q , i);
}
// sort the heap
for( i = q; i>= 0; i--)
{
swap(&A[0] , &A[i]);
heapify(A, i, 0);
}
}
void printA(int A[], int q)
{
int i;
for( i = 0; i <= q; i++)
{
printf("%d", A[i]);
}
printf("%d\n");
}
int main()
{
int A[] = {12,10,9,2,11,8,14,3};
max_heap_append(A,3,8);
printf("Sorted: ");
printA(A, 8);
return 0;
}
Its not followed heapify from 0 to 3 index.. so u need to heapify all. there is some mistake. if your array size is 8 then u can not excess a[8], you can access a[0] to a[7]. so you need to iterate from 0 to 7.
Try with this:
#include <stdio.h>
#include <stdlib.h>
void swap(int * a, int * b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void heapify( int A[], int q, int i)
{
int largest = i;
int l = 2 * i + 1 ;
int r = 2 * i + 2;
if( l < q && A[l] > A[largest])
{
largest = l;
}
if( r < q && A[r] > A[largest])
{
largest = r;
}
if( largest != i)
{
swap( &A[i] , &A[largest]);
heapify(A, q, largest);
}
}
void max_heap_append(int A[], int p , int q)
{
int i;
for( i = q-1; i >= 0; i--)
{
heapify( A , q , i);
}
// sort the heap
for( i = q-1; i>= 0; i--)
{
swap(&A[0] , &A[i]);
heapify(A, i, 0);
}
}
void printA(int A[], int q)
{
int i;
for( i = 0; i < q; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
int main()
{
int A[] = {12,10,9,2,11,8,14,3};
max_heap_append(A,3,8);
printf("Sorted: ");
printA(A, 8);
return 0;
}
You have several problems in your code
printA
One is/can be indicated by the compiler, in printA :
printf("%d\n");
‘%d’ expects a matching ‘int’ argument, but there no no argument
It is easy to guess you just wanted to print a newline, so that line can be replaced by
putchar('\n');
Still in printA you print the numbers without a separator, the result is not usable, for instance do
printf("%d ", A[i]);
When I look at the call of printA in main the parameter n is the number of elements in A, so the end test of the for is invalid because you try to print a value out of the array, the loop must be :
for( i = 0; i < q; i++)
max_heap_append
in the second for the index i can value 0, in that case you swap the first element of the array with itself, that has no sense and the same for the call of heapify with the 2 last arguments valuing 0
When you call that function in main the parameter q receive the number of elements in the array, which is also the first value of i still in that second for and &A[i] is out of the array. You need to replace that line by
for( i = q-1; i> 0; i--)
If I do all these changes :
Compilation and execution :
bruno#bruno-XPS-8300:/tmp$ gcc -g -Wall h.c
bruno#bruno-XPS-8300:/tmp$ ./a.out
Sorted: 2 3 8 9 10 11 12 14
bruno#bruno-XPS-8300:/tmp$

Same code but Strange behaviour

This is my code on implementation of merge sort , it is showing no error at all
and giving no output either :
#include<iostream>
using namespace std;
void merge(int arr[],int l,int m,int r)
{
int i,j,k;
int n1 = m-l+1;
int n2 = r-m;
int L[n1],R[n2];
for(i=0;i<n1;i++)
{
L[i]=arr[i+l];
}
for(j=0;j<n2;j++)
{
R[j]=arr[m+1+j];
}
i=0;
j=0;
k=1;
while(i<n1 && j<n2)
{
if(L[i]<=R[j])
{
arr[k]=L[i];
i++;
}
else
{
arr[k]=R[j];
j++;
}
k++;
}
while(i<n1)
{
arr[k]=L[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if(l < r)
{
int m = l+(r-1)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[],int size)
{
int i;
for( i=0;i<size;i++)
cout<<A[i]<<" ";
}
int main()
{
int arr[]={12,65,34,78,90,65,34};
int arr_size = sizeof(arr)/sizeof(arr[0]);
cout<<"Given array is \n";
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
cout<<"\nSorted array is \n";
printArray(arr, arr_size);
return 0;
}
now here is the piece of code , which is the problem :
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
if i replace this mergeSort() code with the above program's mergeSort(),
the program gives output.
but have a look , both mergeSort() codes are same (in program code as well as mentioned mergeSort() code)
link to original code ===> http://cpp.sh/6zdip
link to the code after replacing mergeSort ====> http://cpp.sh/7lfo
this is most strange problem ,i have ever faced.
It's not the same code.
on your original code: int m = l+(r-1)/2;
on the working code: int m = l+(r-l)/2;
the letter L minuscule can look like '1', but it's not.
Now, try one thing: make the spacements of your code consistent (by running clang-format on both codes) then use a diff application to verify the differences on your code and the running one.
It was pretty easy to spot when I did this:
> int m = l + (r - 1) / 2;
> ---
> int m = l + (r - l) / 2;

Mergesort implementation is slow

I'am doing a report about different sorting algorithms in C++. What baffles me is that my mergesort seems to be slower than heapsort in both of the languages. What I've seen is that heapsort is supposed to be slower.
My mergesort sorts an unsorted array with size 100000 at a speed of 19.8 ms meanwhile heapsort sorts it at 9.7 ms. The code for my mergesort function in C++ is as follows:
void merge(int *array, int low, int mid, int high) {
int i, j, k;
int lowLength = mid - low + 1;
int highLength = high - mid;
int *lowArray = new int[lowLength];
int *highArray = new int[highLength];
for (i = 0; i < lowLength; i++)
lowArray[i] = array[low + i];
for (j = 0; j < highLength; j++)
highArray[j] = array[mid + 1 + j];
i = 0;
j = 0;
k = low;
while (i < lowLength && j < highLength) {
if (lowArray[i] <= highArray[j]) {
array[k] = lowArray[i];
i++;
} else {
array[k] = highArray[j];
j++;
}
k++;
}
while (i < lowLength) {
array[k] = lowArray[i];
i++;
k++;
}
while (j < highLength) {
array[k] = highArray[j];
j++;
k++;
}
}
void mergeSort(int *array, int low, int high) {
if (low < high) {
int mid = low + (high - low) / 2;
mergeSort(array, low, mid);
mergeSort(array, mid + 1, high);
merge(array, low, mid, high);
}
}
The example merge sort is doing allocation and copying of data in merge(), and both can be eliminated with a more efficient merge sort. A single allocation for the temp array can be done in a helper / entry function, and the copy is avoided by changing the direction of merge depending on level of recursion either by using two mutually recursive functions (as in example below) or with a boolean parameter.
Here is an example of a C++ top down merge sort that is reasonably optimized. A bottom up merge sort would be slightly faster, and on a system with 16 registers, a 4 way bottom merge sort a bit faster still, about as fast or faster than quick sort.
// prototypes
void TopDownSplitMergeAtoA(int a[], int b[], size_t ll, size_t ee);
void TopDownSplitMergeAtoB(int a[], int b[], size_t ll, size_t ee);
void TopDownMerge(int a[], int b[], size_t ll, size_t rr, size_t ee);
void MergeSort(int a[], size_t n) // entry function
{
if(n < 2) // if size < 2 return
return;
int *b = new int[n];
TopDownSplitMergeAtoA(a, b, 0, n);
delete[] b;
}
void TopDownSplitMergeAtoA(int a[], int b[], size_t ll, size_t ee)
{
if((ee - ll) == 1) // if size == 1 return
return;
size_t rr = (ll + ee)>>1; // midpoint, start of right half
TopDownSplitMergeAtoB(a, b, ll, rr);
TopDownSplitMergeAtoB(a, b, rr, ee);
TopDownMerge(b, a, ll, rr, ee); // merge b to a
}
void TopDownSplitMergeAtoB(int a[], int b[], size_t ll, size_t ee)
{
if((ee - ll) == 1){ // if size == 1 copy a to b
b[ll] = a[ll];
return;
}
size_t rr = (ll + ee)>>1; // midpoint, start of right half
TopDownSplitMergeAtoA(a, b, ll, rr);
TopDownSplitMergeAtoA(a, b, rr, ee);
TopDownMerge(a, b, ll, rr, ee); // merge a to b
}
void TopDownMerge(int a[], int b[], size_t ll, size_t rr, size_t ee)
{
size_t o = ll; // b[] index
size_t l = ll; // a[] left index
size_t r = rr; // a[] right index
while(1){ // merge data
if(a[l] <= a[r]){ // if a[l] <= a[r]
b[o++] = a[l++]; // copy a[l]
if(l < rr) // if not end of left run
continue; // continue (back to while)
while(r < ee) // else copy rest of right run
b[o++] = a[r++];
break; // and return
} else { // else a[l] > a[r]
b[o++] = a[r++]; // copy a[r]
if(r < ee) // if not end of right run
continue; // continue (back to while)
while(l < rr) // else copy rest of left run
b[o++] = a[l++];
break; // and return
}
}
}

Count the number of occurences of a key in a sorted array recursively

I was trying to solve this problem recursively http://www.geeksforgeeks.org/count-number-of-occurrences-in-a-sorted-array/.
The code I have till now uses a stupid little hack with static variable. Although this works, it would fail if you call the function repeatedly with different keys(as the static variable would still remember the previous set value).
int FindCount(const vector< int > &A, int l, int r, int B)
{
static int count =0;
// cout<<l<<' '<<r<<endl;
if(l <= r)
{
int mid = (l+r)/2;
// cout<<mid<<endl;
if(A[mid] == B)
{
count++;
FindCount(A, l, mid-1, B);
FindCount(A, mid+1, r, B);
}
else if (A[mid] < B)
{
FindCount(A, mid+1, r, B);
}
else
{
FindCount(A, l, mid-1, B);
}
}
return count;
}
I can figure out how it should work but have a hard time converting that into code. It should be something like this, once you find the particular key then return 1 and the continue to recusively search the left and right of the key.
Could you help me do this recusively without the use of static variable with a cleaner code :)
int FindCount(const vector< int > &A, int l, int r, int B)
{
int count = 0;
if(l <= r)
{
int mid = (l+r)/2;
if(A[mid] == B)
{
count++;
count += FindCount(A, l, mid-1, B);
count += FindCount(A, mid+1, r, B);
}
else if (A[mid] < B)
{
count = FindCount(A, mid+1, r, B);
}
else
{
count = FindCount(A, l, mid-1, B);
}
}
return count;
}
This should work, although it is still a O(n) algorithm, not very efficient.
You yet cast away the return value of all invocations but that at the bottom of the recursion stack (a stack grows upwards); instead of the static count you can just add the return value of the recursions to an automatic local variable count.
The code contains a serious bug: you should use size_t and not int. The result could overflow. Indexers and counts should be size_t - which is a unsigned 32-bit integer on 32-bit platforms and a unsigned 64-bit integer on 64-bit platforms.
u_seem_surprised has a perfectly valid answer. Another way to solve this problem is to use lambdas and capture the count variable:
#include <vector>
#include <functional>
size_t FindCount(const std::vector<int> &A, size_t l, size_t r, int B)
{
using namespace std;
size_t count = 0;
function<void(const vector<int>&, size_t, size_t, int)> impl;
impl = [&count, &impl](const vector<int> &A, size_t l, size_t r, int B)
{
if (l <= r)
{
auto mid = (l + r) / 2;
if (A[mid] == B)
{
count++;
impl(A, l, mid - 1, B);
impl(A, mid + 1, r, B);
}
else if (A[mid] < B)
{
impl(A, mid + 1, r, B);
}
else
{
impl(A, l, mid - 1, B);
}
}
};
impl(A, l, r, B);
return count;
}

Can I partition the array in K sizes?

I am trying to implement the algorithm from this question: Need idea for solving this algorithm puzzle, but i am missing some edge case which is causing my code to go in infinite loop. I can fix it by doing some cosmetic change but it shows that i didn't understand the algorithm.
Can someone help me out, what i am missing?
#include <stdio.h>
#define max(a, b) (((a)>(b))?(a):(b));
int get_max(int *a, int i, int size)
{
if (i >= size)
return 0;
return max(a[i], get_max(a, i+1, size));
}
int get_sum(int *a, int i, int size)
{
if (i >= size)
return 0;
return a[i] + get_sum(a, i+1, size);
}
int get_partition(int *a, int size, int bound) {
int running_sum = 0;
int partitions = 0, i;
for (i=0;i<size;i++) {
if (a[i] + running_sum <= bound) {
running_sum += a[i];
} else {
running_sum = 0;
running_sum += a[i];
partitions++;
}
}
return partitions;
}
int foo(int *a, int size, int k)
{
int lower = get_max(a, 0, size);
int higher = get_sum(a, 0, size);
int partition;
while (lower < higher) {
int bound = (lower + (higher))/2;
partition = get_partition(a, size, bound);
printf("partition %d bound %d lower %d higher %d\n", partition, bound, lower, higher);
if (partition >= k)
lower = bound;
else
higher = bound;
}
return partition;
}
#define SIZE(a) sizeof(a)/sizeof(a[0])
int main(void) {
int a[] = {2, 3, 4, 5, 6};
printf("%d\n", foo(a, SIZE(a), 3));
return 0;
}
Output:
partition 1 bound 13 lower 6 higher 20
partition 2 bound 9 lower 6 higher 13
partition 3 bound 7 lower 6 higher 9
partition 3 bound 8 lower 7 higher 9
partition 3 bound 8 lower 8 higher 9
...last line keeps repeating.
You have couple of mistakes:
during the binary search, your while test should be while (lower+1 < higher) { and not while (lower < higher) {. You are entering infinite loop when lower = 8, higher = 9. At this stage, your bound would be (lower+higher)/2=8and you would update lower = bound which would not change anything.
at the end of foo you should return higher (not partitions) since your binary search invariant is that for having bound <= lower you can partition the array in more than k parts and forbound >= higher you can partition it in k or less.
your calculation of get_partition is wrong. You don't take into the account the last partition group since you only update partitions when you overflow running_sum. After the for-cycle you should have the statement :
if (running_sum > 0)
partitions++;
Putting it all together:
#include <stdio.h>
#define max(a, b) (((a)>(b))?(a):(b));
int get_max(int *a, int i, int size)
{
if (i >= size)
return 0;
return max(a[i], get_max(a, i+1, size));
}
int get_sum(int *a, int i, int size)
{
if (i >= size)
return 0;
return a[i] + get_sum(a, i+1, size);
}
int get_partition(int *a, int size, int bound) {
int running_sum = 0;
int partitions = 0, i;
for (i=0;i<size;i++) {
if (a[i] + running_sum <= bound) {
running_sum += a[i];
} else {
running_sum = 0;
running_sum += a[i];
partitions++;
}
}
if (running_sum > 0)
partitions++;
return partitions;
}
int foo(int *a, int size, int k)
{
int lower = get_max(a, 0, size);
int higher = get_sum(a, 0, size);
int partition;
while (lower+1 < higher) {
int bound = (lower + (higher))/2;
partition = get_partition(a, size, bound);
printf("partition %d bound %d lower %d higher %d\n", partition, bound, lower, higher);
if (partition > k)
lower = bound;
else
higher = bound;
}
printf("partition %dlower %d higher %d\n", partition, lower, higher);
return higher;
}
#define SIZE(a) sizeof(a)/sizeof(a[0])
int main(void) {
int a[] = {2, 3, 4, 5, 6};
printf("%d\n", foo(a, SIZE(a), 3));
return 0;
}

Resources