Sunday, 28 Nov 2021
Category: binary-string

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 maxLength(string str, int n,              char c, int k){        int ans = -1;          int cnt = 0;          int left = 0;      for (int right = 0; right < n; right++) {          if (str[right] == c) {            cnt++;        }                  while (cnt > k) {            if (str[left] == c) {                cnt–;            }                                      left++;        }                          ans = max(ans, right – left + 1);    }      return ans;}  int maxConsecutiveSegment(string S, int K){    int N = […]

Given two positive integers L and R and a binary string S of size N, the task is to check if the end of the string is reached from the index 0 by a series of jumps of indices, say i such that S[i] is 0 jumps are allowed in the range [L, R]. If […]

const INT_MAX = 2147483647;                    const minZeroeCount = (S, K, Q) => {                let N = S.length;                        if (K == 0 || Q == 0) {            let cnt0 = 0;            for (let i = 0; i < N; ++i) {                if (S[i] == '0') cnt0++;            }            return cnt0;        }                        if (Q >= Math.ceil(N / K)) {            return 0;        }                                const dp = new Array(N + 1).fill(INT_MAX).map(() => new Array(Q […]

#include using namespace std;void findTotalDistance(string S, int N){            vector prefixDistance(N);    vector suffixDistance(N);            int cnt = 0;        bool haveOne = false;    for (int i = 0; i < N; ++i) {                if (S[i] == '1') {                        haveOne = true;                        cnt = 0;                        prefixDistance[i] = 0;        }                else if (haveOne) {                        cnt++;                        prefixDistance[i] = cnt;        }                        else            prefixDistance[i] = INT_MAX;    }        haveOne = false;    for (int i = N - 1; i >= 0; –i) {                if […]

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

#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 a binary string S consisting of N characters, the task is to find the minimum number of flips required such that there don’t exist three consecutive same characters.Examples:Input: S = “1100011”Output: 1Explanation:Flip the character at index 3 modifies the string S “1101011” that have no three consecutive same characters. Therefore, the minimum number of […]

Given a binary string str[] of size N and an integer M. This binary string can be modified by flipping all the 0’s to 1 which have exactly one 1 as a neighbour. The task is to find the final state of the binary string after M such iterations.Note: 2≤N≤103, 1≤M≤109Examples:Input: str=”01100″, M=1Output: 11110Explanation: After […]

#include using namespace std;  void merge(vector& v, int left,           int mid, int right, int& inversions){    vector temp(right – left + 1);      int i = left;    int j = mid + 1;    int k = 0;    int cnt = 0;      while (i