Binary Substitution solution codechef-

Binary Substitution solution codechef – Chef has binary string S of length N.

Chef can perform the following operation on the string:

  • Choose a contiguous subarray S_L, S_{L + 1}, \ldots , S_R such that the count of set bits in the subarray is equal to the count of unset bits in the subarray.
  • Replace the chosen subarray with either a set bit or an unset bit.This LeetCode contest is sponsored by Motorola.The top 500 contestants would be shortlisted as Employee Referrals if they apply for the job opportunities with Motorola Mobility, a Lenovo Company.Register for the contest in advance! You can fill out the contact information at the registration step.

    ⭐ Bonus Prizes⭐

    • Contestants ranked 1st will win a Motorola Ace 5G
    • Contestants ranked 2nd will win a Lenovo Smart Clock with wireless charger
    • Contestants ranked 3rd will win a Motorola Bluetooth Speaker Tumbler
    • Contestants ranked 4th ~ 10th will win Amazon $40 eGift card
    • Contestants ranked 11th ~ 20th will win Starbucks $25 eGift card
    • Contestants ranked 66th, 166th, 266th, 666th, 1666th will win a LeetCode BigO NoteBook

Binary Substitution solution codechef

Chef wants to reduce the string to minimum possible length using minimum number of given operations. Help Chef by telling him the minimum length and also the operations required to obtain that. If there are multiple ways to obtain the answer, print any.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • The first line of each test case contains a single integer N, denoting the length of the binary string.
  • The second line of each test case contains a binary string S.

Binary Substitution solution codechef

For each test case, output K+1 lines:

  • The first line should contain two space-separated integers M and K, denoting the minimum length that can be obtained and the minimum number of operations required to obtain it respectively.
  • Then, K lines follow, where the i^{th} line denotes the i^{th} operation:
    • Each operation is denoted using three space separated integers LR, and B.
      The integers L and R denote the chosen substring such that (1\le L\lt R \le |S|) and the substring S[L, R] has equal count of set and unset bits. Note that, |S| denotes the length of the current string.
      The integer B denotes the bit with which the substring is replaced.

Binary Substitution solution codechef

  • 1 \leq T \leq 100
  • 1 \leq N \leq 1000

Sample 1:

Input

Output

3
4
1100
1
1
5
11000
1 1
1 4 0
1 0
1 2
1 4 1
1 2 1

Binary Substitution solution codechef

Test case 1: We can reduce the string to a string of length 1. We require only 1 operation to do so:

  • Choose L = 1, R = 4, and B = 0. We chose the substring S[1,4] which contains 2 set bits and 2 unset bits. We can replace the chosen substring with bit 0.

Test case 2: The given string is of length 1. Thus, we cannot reduce it any further.

Test case 3: We can reduce the string to a string of length 1. We require 2 operations to do so:

  • Operation 1: Choose L = 1, R = 4, and B = 1. We chose the substring S[1,4] which contains 2 set bits and 2 unset bits. We can replace the chosen substring with bit 1. Thus, the string after this operation is S = 10.

Operation 2: Choose L = 1, R = 2, and B = 1. We chose the substring S[1,2] which contains 1 set bit and 1 unset bit. We can replace the chosen substring with bit 1. Thus, the string after this operation is S = 1.

Solution – CLICK HERE

 

Leave a Comment