Category: Bit Magic

Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K

#include using namespace std;  int minMoves(vector a,             vector b,             int K){        int n = a.size(), m = a[0].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 […]

Continue Reading

Count of substrings with the frequency of at most one character as Odd

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 […]

Continue Reading

Lexicographically smallest binary string formed by flipping bits at indices not divisible K1 or K2 such that count of 1s is always greater than 0s from left

  #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() == […]

Continue Reading