Minimum steps to determine the subsequence with max 1s based on given conditions


The aim of this article is to implement a program to find minimum steps to determine the subsequence with max 1s based on given conditions.

So as we all know, a one-dimensional array containing characters that is terminated by a null can be used to define a string.

Given is a string Str of length K, wherein K is always even, and contains the characters "0," "1," and "?" Divide the string to two separate strings, let's call them Str1 and Str2, each of which is going to include the characters at the even values of Str and the odd values of Str. The goal is to determine the fewest steps necessary to make a prediction about which of the two strings, Str1 or Str2, will have the greatest number of 1s. Select one character for Str1 or Str2 in a single step. Choose '0' when the character is a zero, '1' if it is a one, and '?' if it is a character that is either a one or a zero.

Problem Statement

Implement a program to find minimum steps to determine the subsequence with max 1s based on given conditions

Sample Example 1

Input: Str = “?10?0?”
Output: 4

Explanation

  • Step 1 − Here Str[0] is "?"

So select "0" as the character for Str1.
Which implies Str1=”0″, Str2=”″.
  • Step 2 − Here Str[1] is "1"

Select "1" as the character for Str2.
Which implies Str1=”0″, Str2=”1″.
  • Step 3 − Here Str[2] is "0"

Select "0" as the character for Str1.
Which implies Str1=”00″, Str2=”1″.
  • Step 4 − Here Str[3] is "?"

Select "1" as the character for Str2.
Which implies Str1=”00″, Str2=”11″.

No matter what number is selected for the remaining indices, Str2 is going to have more 1s after Step 4.

Sample Example 2

Input: Str = “1?0??0110”
Output: 4

Explanation

  • Step 1 − Here Str[0] is "1"

So select "1" as the character for Str1.
Which implies Str1=”1″, Str2=”″.
  • Step 2 − Here Str[1] is "?"

Select "1" as the character for Str2.
Which implies Str1=”1″, Str2=”1″.
  • Step 3 − Here Str[2] is "0"

Select "0" as the character for Str1.
Which implies Str1=”10″, Str2=”1″.
  • Step 4 − Here Str[3] is "?"

Select "1" as the character for Str2.
Which implies Str1=”10″, Str2=”11″.
  • Step 5 − Here Str[4] is "?"

Select "0" as the character for Str1.
Which implies Str1=”100″, Str2=”11″.
  • Step 6 − Here Str[5] is "0"

Select "0" as the character for Str2.
Which implies Str1=”100″, Str2=”111″.
  • Step 7 − Here Str[6] is "1"

Select "1" as the character for Str1.
Which implies Str1=”1001″, Str2=”111″.

No matter what number is selected for the remaining indices, Str2 is going to have more 1s after Step 7.

Solution Approach

In Order to to find minimum steps to determine the subsequence with max 1s based on given conditions, we take the following methodology.

The approach to solve this problem and to find minimum steps to determine the subsequence with max 1s based on given conditions is given below.

The goal is to recursively address the issue and arrive at a solution after considering every alternative.

The term Recursion is nothing but the process of a function calling itself whether directly that is,without an intermediary or indirectly. This equivalent function is acknowledged to be a recursive function. Also a recursive algorithm could be made use in tackling various issues with quite relative ease.

Algorithm

The algorithm to find minimum steps to determine the subsequence with max 1s based on given conditions given below

  • Step 1 − Start

  • Step 2 − Define a recursive function.

  • Step 3 − Define string Str ,integer i, integers count1 and count2 which is used in order to store the number of ones till i in Str1 and Str2 respectively.

  • Step 4 − Define integers n1 and n2 to store the available places in Str1 and Str2

  • Step 5 − If i equals m, then Str1 and Str2 are both completely filled, and the answer can now be anticipated with certainty. therefore give a 0 back.

  • Step 6 − Return 0 if the count1 exceeds the product of n2 and count2, as Str1 will now have more ones than Str2 even after all the ones in Str2 have been chosen.

  • For the reason outlined above, return 0 if count2 exceeds the product of n1 and count1.

  • Step 7 − Verify whether i equals even or odd after testing for basic instances. If i is even, Str1 will choose this index; if not, Str2.

  • Since the number of accessible places within the string will shrink by a single position after filling, decrease n1 or n2 depending on which string is presently being filled.

  • Step 8 − Suppose, if the present character is '?' That is, s[i] = '? ' then perform two recursive calls of selecting '1' and of picking out '0', returning the lowest value of the two after incorporating 1 to both.

  • Otherwise, place a single call and then add one to that to get the answer.

    The response to this query will be provided by the final recursive call.

  • Step 8 − Stop

Example: C++ Program

Here is the C program implementation of the above written algorithm to find minimum steps to determine the subsequence with max 1s based on given conditions

// the C++ program of the above written algorithm
#include <bits/stdc++.h>
using namespace std;

// the function in order find the minimum number of the steps recursively  needed by combining both the 2 strings
int minimumSteps(string& Str, int cnt1, int cnt2,int n1, int n2, int m,int i){

   // check whetherthe current pointer reach //the end
   if (i == m) {
      return 0;
   }
    
   // the Condition which indicates here that one string does more ones than the other regardless of which number is opted  for theindexes which is remaining
   if (cnt1 > (n2 + cnt2)
         || cnt2 > (n1 + cnt1)) {
      return 0;
   }
   int ch1 = 0;
   int ch2 = 0;
    
   // on condition that i is found to be even, then choose the character for Str
   if (i % 2 == 0) {
      if (Str[i] == '?') {
         return min( 1 + minimumSteps(Str, i + 1,  cnt1 + 1, cnt2, n1 - 1, n2, m), 1 + minimumSteps( Str, i + 1, cnt1, cnt2, n1 - 1, n2, m));
      } else if (Str[i] == '1') {
         ch1 = 1 + minimumSteps(Str, i + 1, cnt1 + 1, cnt2, n1 - 1, n2, m);
         return ch1;
      } else {
         ch2 = 1 + minimumSteps(Str, i + 1, cnt1, cnt2, n1 - 1, n2, m);
         return ch2;
      }
   }
   else {
      if (Str[i] == '?') {
         return min(1 + minimumSteps(Str, i + 1, cnt1, cnt2 + 1, n1, n2 - 1, m),1 + minimumSteps(Str, i + 1,cnt1, cnt2, n1, n2 - 1, m));
      } else if (Str[i] == '1') {
         ch1 = 1+ minimumSteps(Str, i + 1, cnt1, cnt2 + 1, n1, n2 - 1, m);
         return ch1;
      } else {
         ch2 = 1+ minimumSteps( Str, i + 1, cnt1, cnt2, n1, n2 - 1, m);
         return ch2;
      }
   }
}
int main(){
   string str = "?10?0?01";
   int M = str.size();
   cout << minimumSteps(str, 0, 0, 0, M / 2, M / 2, M);
   return 0;
}

Output

1

Conclusion

Likewise we can find the minimum steps to determine the subsequence with max 1s based on given conditions

The challenge of obtaining the minimum steps to determine the subsequence with max 1s based on given conditions is resolved in this article.

Here C++ programming code as well as the algorithm to find the minimum steps to determine the subsequence with max 1s based on given conditions are provided.

Updated on: 10-Aug-2023

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements