Given a range of integers i.e. [L,R] asks him to find the count of such integer i where L ≤ i ≤ R and binary representation of i contains the pattern 110 as a substring.
1 ≤ T ≤ 100 1 ≤ L ≤ R ≤ 10^18
Input :
3
5 16
20 50
1 100
Output:
4
15
48
Hoping you have some knowledge of digit dp.
So tackle the problem like this:
ans = count from 0 to R - count from 0 to L - 1
now you have a simple problem of counting the numbers from 0 to X which have the pattern 110
convert X to binary…
Instead of digits from 0 to 9 you have digits 0 and 1 only.
now this becomes a standard types.
dp states :
int idx = current bit that you want to place
int prev = previous bit
int prev2 = previous of previous bit
bool found = if pattern has been previously found or not
bool flag = just to see if the current number is smaller or not
so if prev2 == 1 and prev == 1 and current number placed is 0 , increment the count by 1 and solve for the next bits.
just try to take care of some other cases.
please check this code
`#include<bits/stdc++.h>
using namespace std; #define int long long int #define ull unsigned long long #define pb push_back #define pf push_front #define endl “\n” #define fast_in_out ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
No this was not from the kickstart round but yes the second problem was based on digit DP , atleast i solved it using digit dp I dont know about others