Check if String can be divided into two Subsequences so that product of sum is odd


In this problem, we will check if it is possible to divide the given numeric string into two disjoint subsequences such that sum(sub1) * sum(sub2) becomes odd.

We need to divide the string into two subsequences such that the sum of the digits of both becomes odd to get the odd multiplication result.

Problem statement − We have given a string num_string containing the numeric characters. We need to check whether we can divide the string into two subsequences such that the multiplication of the sum of both subsequences becomes odd. Also, it is given that every character of the string should be in any of the subsequences.

Sample Examples

Input

num_str = "3232";

Output

‘Yes’

Explanation

We can divide the string into subsequences: ' 32’ and ‘32’ or ‘3’ and ‘232.’

Input

 num_str = ‘2424’

Output

‘No’

Explanation

We can’t divide the string into two subsequences such that we can get odd multiplication of their digit’s sum.

Input

num_str = "9546732";

Output

‘Yes’

Explanation

We can divide the string into the ‘946’ and ‘5732’ subsequences.

Approach 1

We should start thinking about how to get the odd multiplication of two numbers to solve the problem. The answer is when both multipliers are odd, and if any multiplier is even, we can’t get an odd multiplication result.

So, we need to check whether we can divide the string into two subsequences such that the sum of their digits is odd, and it is only possible when we have the total number of odd digits in the string is even.

For example, if we have 5 odd digits in the string. So, we can put {0, 5}, {1, 4}, {2, 3}, {3, 2}, {4, 1}, and {5, 0} odd digits in the first and second subsequence. We can observe that in every pair, any subsequence contains an even number of odd digits, and when we take the sum of the even number of odd digits, it becomes even.

Here, we will find digits to include in the first subsequence, and the remaining digits will be included in the second subsequence.

Algorithm

  • Step 1 − Initialize the ‘sub1’ with 0 to count the number of odd digits in the given string.

  • Step 2 − Start traversing the string. If the digit is not divisible by 2, increase the value of the ‘sub1’ by 1.

  • Step 3 − If ‘sub1’ is not divisible by 2 or its value is 0, return false.

  • Step 4 − At last, return true.

Example

Following are the programs to the above algorithm −

#include <stdio.h>
#include <string.h>

int isSplitPossible(const char* num_str) {
   int sub1 = 0;
   int num_len = strlen(num_str);

   // Find the total number of odd digits
   for (int p = 0; p < num_len; p++) {
      if ((num_str[p] - '0') % 2 != 0) {
         sub1++;
      }
   }

   // Return false if sub1 is odd or 0
   if (sub1 % 2 != 0 || sub1 == 0) {
      return 0;
   }
   return 1;
}
int main() {
   const char* num_str = "3232";

   if (isSplitPossible(num_str)) {
      printf("Yes, it is possible to split the string into two substrings.\n");
   } else {
      printf("No, it is not possible to split the string into two substrings.\n");
   }
   return 0;
}

Output

Yes, it is possible to split the string into two substrings.
#include <bits/stdc++.h>
using namespace std;
bool isSplitPossible(string num_str, int num_len) {
   int sub1 = 0;
   
   // Find total number of odd digits
   for (int p = 0; p < num_len; p++) {
      if ((num_str[p] - '0') % 2 != 0) {
         sub1++;
      }
   }
   
   // Return false, if sub1 is odd or 0
   if (sub1 % 2 != 0 || sub1 == 0)
   return false;
   return true;
}
int main() {
   string num_str = "3232";

   int num_len = num_str.length();
   if (isSplitPossible(num_str, num_len)) {
      cout << "Yes, it is possible to split the string into two substrings.";
   } else {
      cout << "No, it is not possible to split the string into two substrings.";
   }
   return 0;
}

Output

Yes, it is possible to split the string into two substrings.
public class SplitString {
   public static boolean isSplitPossible(String numStr) {
      int sub1 = 0;

      // Find total number of odd digits
      for (int p = 0; p < numStr.length(); p++) {
         if ((numStr.charAt(p) - '0') % 2 != 0) {
            sub1++;
         }
      }

      // Return false if sub1 is odd or 0
      if (sub1 % 2 != 0 || sub1 == 0) {
         return false;
      }
      return true;
   }

   public static void main(String[] args) {
      String numStr = "3232";

      if (isSplitPossible(numStr)) {
         System.out.println("Yes, it is possible to split the string into two substrings.");
      } else {
         System.out.println("No, it is not possible to split the string into two substrings.");
      }
   }
}

Output

Yes, it is possible to split the string into two substrings.
def is_split_possible(num_str):
   sub1 = 0

   # Find total number of odd digits
   for digit in num_str:
      if int(digit) % 2 != 0:
         sub1 += 1

   # Return false if sub1 is odd or 0
   if sub1 % 2 != 0 or sub1 == 0:
      return False
   return True

# Main function
if __name__ == "__main__":
   num_str = "3232"

   if is_split_possible(num_str):
      print("Yes, it is possible to split the string into two substrings.")
   else:
      print("No, it is not possible to split the string into two substrings.")

Output

Yes, it is possible to split the string into two substrings.

Time complexity − O(N) for traversing the string.

Space complexity − O(1), as we don’t use any extra space.

In this kind of problem, we need to think about the cases when we get the desired result, as we started to think here to get odd multiplication. After that, we can move to the next step to get the result in the current step; as we have thought about, we need a total even number of odd digits in the subsequence.

Updated on: 16-Oct-2023

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements