Given a string S of zeroes and ones. Given range (L,R), you have to find the 1st and last occuring 1 in the range and tell the number of zeros between the first and last ones including L and R index.
1<=L,R<=10^5.
Eg:
0010011001
L,R=1,8
Output: 2
I did it by maintaining a array of total 0s till a particular index(lets say temp array) .
and also a vector for all 1s to shift my L,R to required index(1s ) in (log n) time. Then I first shifted my L,R accordingly and then just saw difference of value in temp array of L and R.
Hence Creating temp and 1s vector ->O(n)
each query ->O(log n)
I maintained 3 array one prefix-sum array for 0s, next_pos_of_1 array, prev_pos_of_1 array and answered queries in O(1).
.
Can any one help with the Game question where you have to divide the bunch in equal parts
Just a observation , If B has no factor between [C,B) ==>ans=0(can’t divide equally by 1st player).
if it has a factor ->. if (A is odd){return 1;} else {return 0;}
as can be proved easily try otherwise I will tell
@anonym_chef So, u find the sum between next_pos_of_1(from L) and prev_pos_of_1(from R) and than u do (R-L-prefix_sum) ? U did prefix sum using fenwick tree? then how u can answer queries in O(1), it will take logn @knightknight
For eg, a,b,c=5,92,10 then ever if u finding prime factor, u can’t divide becaz after dividing quotent is less to C. factors of 92 = 46,23,4,2 but how can u divide ? @knightknight