Saturday, 4 Dec 2021
Category: Bit Magic

Given an array vec[] of size N of non-negative integers. The task is to count the number of subsequences with the sum equal to S – 2 where S is the sum of all the elements of the array.Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA […]

Given a binary array arr[] and an integer K, the task is to count the maximum number of 0’s that can be flipped to 1’s such that there are at least K 0’s between two 1’s.Example:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course […]

Given two integers N and K, the task is to find the count of binary strings of at most N length that can be formed such that the count of consecutive 1‘s is always a multiple of K.Example:Input: N = 3, K = 2Output: 6Explanation: Binary strings of atmost N length containing consecutive 1’s as […]

#include using namespace std;  int minMoves(vector a,             vector b,             int K){        int n = a.size(), m = a.size();            int cntOperations = 0;          for (int i = 0; i < n; i++) {          for (int j = 0; j < m; j++) {                                        if (a[i][j] != b[i][j]) {                                                    cntOperations++;                                                                      if (i + K - 1 >= n                    || j + K – 1 >= m) {                    return -1;                }                                                  for […]

#include using namespace std;const long long M = 1e9 + 7;  vector directions    = { { 0, 1 }, { -1, 0 }, { 0, -1 },        { 1, 0 }, { 1, 1 }, { -1, -1 },        { -1, 1 }, { 1, -1 } };  bool isInside(int i, int j, int N, int M){    if (i >= 0 […]

Given a string S of N characters, the task is to calculate the total number of non-empty substrings such that at most one character occurs an odd number of times.Example: Input: S = “aba”Output: 4Explanation: The valid substrings are “a”, “b”, “a”, and “aba”. Therefore, the total number of required substrings are 4.Input: “aabb”Output: 9Explanation: The […]

Given a binary string S of size N, the task is to check if the count of 1s can be made greater than the count of 0s by changing the 0s adjacent to 1s to any other characters. If it is possible, then print Yes. Otherwise, print No.Note: Any index having 1 can be chosen […]

Given two positive integers X and K, the task is to find the K-th smallest positive integer Y, such that X + Y = X | Y, where | denotes the bitwise OR operation.Example:Input: X = 5, K = 1Output: 2Explanation: The first number is 2 as (2 + 5 = 2 | 5 )Input: […]

#include using namespace std;  void generateString(int k1, int k2, string s){            int C1s = 0, C0s = 0;    int flag = 0;    vector pos;          for (int i = 0; i < s.length(); i++) {          if (s[i] == '0') {            C0s++;                                      if ((i + 1) % k1 != 0                && (i + 1) % k2 != 0) {                pos.push_back(i);            }        }          else {            C1s++;        }          if (C0s >= C1s) {                                                  if (pos.size() == […]

Given two positive integers X and K, the task is to find the Kth smallest positive integer (Y) such that the sum of X and Y is equal to Bitwise OR of X and Y, i.e. (X + Y = X | Y)Examples:Input: X = 5, K = 1Output: 2Explanation:Consider the smallest number as 2. […]