category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

B. And It’s Non-Zero, bitwise, prefix sums

brute force

brute force ```cpp int main() { int t = 1; cin >> t; for (int i = 1; i <= t; i++) { int l, r; cin >> l >> r; int ans = r - l; for (int place = 0; place <= 18; place++) { int count = 0; int temp = 1 << place; for (int i = l; i <= r; i++) { count += ((i & temp) == 0); } ans = min(ans, count); } cout << ans << '\n'; } } ```
correct one ```cpp int main() { vector<vector> dp(32, vector(2e5, 0)); for (int place = 0; place <= 18; place++) { int temp = 1 << place; for (int i = 1; i <= 2e5; i++) { dp[place][i] = ((i & temp) == 0); } } for (int place = 0; place <= 18; place++) { for (int i = 2; i <= 2e5; i++) { dp[place][i] += dp[place][i - 1]; } } int t = 1; cin >> t; for (int i = 1; i <= t; i++) { int l, r; cin >> l >> r; int ans = r - l; for (int i = 0; i <= 18; i++) { ans = min(ans, (dp[i][r] - dp[i][l - 1])); } cout << ans << '\n'; } } ``` </details>