Modify a Binary String by flipping characters such that any pair of indices consisting of 1s are neither co-prime nor divisible by each other


In this problem, we have given a binary string of length 4*N, and we need to flip zeros of the binary string so that any pair of indices containing ‘1’ should not be co-prime or divisible by each other.

Here, we can solve the problem by observation. The string contains 4*N characters. We can flip the N characters from the last, which are at the even index.

Problem statement – We have given an integer N and a binary string of the length 4*N containing all zeros initially. We need to flip ‘0’ to ‘1’ in such a way so that any pair of indices consisting of ‘1’ in the resultant string should not be divisible by each other and co-prime to each other. Also, it is given that index starts from 1.

Note– The co-prime number is the number that has no common factor except 1.

Sample examples

Input– str = ‘000000000000’ K = 3

Output– ‘000000010101’

Explanation– We have flippled the characters at {8, 10, 12} index as any pair is not divisible by each other. Also, any pair is not co-prime, as all have 2 as a common factor.

Input– str = ‘00000000’ K = 2

Output– ‘00000101’

Explanation– We have flipped the characters at {6, 8} indexes as they are not divisible by each other and not a co-prime.

Input– str = ‘0000’ K = 1

Output– ‘0001’

Explanation– We have flipped the characters at the {4} index.

Approach 1

In this approach, we will flip the N characters from the last, which are at an even position, so that we can get the resultant string according to the given conditions.

For example, K = 3, the initial binary string will be ‘000000000000’. When we flip 3 characters at even index from the last, the string becomes 000000010101, and indices values are {8, 10, 12}. If we flip the character at the 7th index, 7 and all others are co-prime. If we flip the character at the 6th index, 12 is divisible by 6. So, we can flip only N characters from the last, which are at an even index.

Algorithm

  • Initialize the ‘len’ variable’s value by 4*N.

  • Make total N iterations using the for loop.

  • Flip the character at len -1 index as string indexing is 0-based.

  • Decrease the value of ‘len’ by 2 as we only need to flip the character at even indices.

  • At last, return the updated string.

Example

#include <iostream>
using namespace std;
// function to modify the string str by flipping 0 to 1 at particular indices so that any 2 indices are not co-prime of each other and divisible by each other
string getstring(char str[], int N) {
   int len = 4 * N;
   // flip total N 0's to 1's at 4N, 4N-2, 4N-4, 4N-6, ... indices
   for (int i = 1; i <= N; i++) {
      str[len - 1] = '1';
      len -= 2;
   }
   return str;
}
int main() {
   int N = 4;
   char str[N * 4];
   // Initialize the string str
   for (int i = 0; i < 4 * N; i++)
      str[i] = '0';
   cout << "The string after modifying is " << getstring(str, N);
   return 0;
}

Output

The string after modifying is 0000000001010101�SK-�

Time complexity – O(N) as we make N iterations.

Space complexity – O(1) as we updated the given string without using the extra space.

We have solved the above problem based on the observation only. Sometimes, observation of different inputs and outputs is more important than the logic with the mathematical operations.

Updated on: 18-Aug-2023

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements