while(arr[i]!=l)
and while(arr[i]!=r)
are wrong. The indicators of the left and right sides, Li
and Ri
, are indices or counts, not the values in the array. You should use a simple for
loop:
for (i = l; i <= r; ++i)
pro = pro * arr[i-1] % m;
Note that the index is i-1
because Li
and Ri
are given using one-based indexing, but C uses zero-based indexing for its arrays.