Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given a string S of length N, the task is to check whether it is possible to partition the string S into two disjoint non-empty subsequences S1 and S2 such that sum(S1) × sum(S2) is odd and every character of S must be in either S2 and S1.

Examples:

Input: S = “1122”
Output: Yes

Explanation: We partition the string into two subsequences .Let S1 be the underlined elements and S2 be the other ones. sum(S1) × sum(S2) = 3 × 3 = 9.

Input: S = “135”
Output: No

Approach: The problem can be solved based on the following observation:

To make the product of two numbers odd, both numbers should be odd. So, the sum of S1 and S2 must be odd. This is only possible when each of them has odd number of odd integers. If the string has even number (except 0) of odd digits then only such a partition is possible. 

This satisfies that the sum of S will also be even and there will be atleast 2 odd numbers.

Follow the steps mentioned below to implement the idea:

  • Traverse through the string S.
  • Check if the sum is even and the number of odd digits is at least 2.
    • If the condition is satisfied, the partition is possible.
    • Otherwise, the partition is not possible.

Below is the implementation of the above approach.

C++

// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;

// Function to Check whether it is
// possible to partition a string S
// into two non-empty sub-sequences
// S1 and S2.
string check(string S, int n)
{
    int arr[n];
    for (int i = 0; i < n; i++) {
        arr[i] = S.at(i) - '0';
    }
    int S1 = 0;
    for (int i = 0; i < n; i++)
        if ((arr[i] & 1) != 0)
            S1++;

    if ((S1 & 1) != 0 || S1 == 0)
        return "No";
    return "Yes";
}

// Driver Code
int main()
{

    string S = "1122";

    int N = S.length();

    // Function Call
    cout << check(S, N);

    return 0;
}

// This code is contributed by aarohirai2616.

Java

// Java code to implement the approach

import java.io.*;
import java.util.*;

public class GFG {

    // Function to Check whether it is
    // possible to partition a string S
    // into two non-empty sub-sequences
    // S1 and S2.
    public static String check(String S, int n)
    {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = S.charAt(i) - '0';
        }
        int S1 = 0;
        for (int i = 0; i < n; i++)
            if ((arr[i] & 1) != 0)
                S1++;

        if ((S1 & 1) != 0 || S1 == 0)
            return "No";
        return "Yes";
    }

    // Driver Code
    public static void main(String[] args)
    {
        String S = "1122";
        int N = S.length();

        // Function Call
        System.out.println(check(S, N));
    }
}

Python3

# Python code to implement the approach

# Function to Check whether it is
# possible to partition a string S
# into two non-empty sub-sequences
# S1 and S2.
def check(S,n):
    arr=[0]*n
    for i in range(n):
        arr[i]=ord(S[i])-ord('0')
        
    S1=0
    for i in range(n):
        if((arr[i]&1)!=0):
            S1=S1+1
            
    if((S1&1)!=0 or S1==0):
        return "No"
    return "Yes"
    
# Driver Code
S = "1122"
N=len(S)

#Function Call
print(check(S,N))

# This code is contributed by Pushpesh Raj.

C#

// C# code to implement the approach

using System;

public class GFG {

    // Function to Check whether it is
    // possible to partition a string S
    // into two non-empty sub-sequences
    // S1 and S2.
    public static String check(String S, int n)
    {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = S[i] - '0';
        }
        int S1 = 0;
        for (int i = 0; i < n; i++)
            if ((arr[i] & 1) != 0)
                S1++;

        if ((S1 & 1) != 0 || S1 == 0)
            return "No";
        return "Yes";
    }

    static public void Main()
    {

        // Code
        String S = "1122";
        int N = S.Length;

        // Function Call
        Console.WriteLine(check(S, N));
    }
}

// This code is contributed by lokesh

Javascript

// js code

// Function to Check whether it is
// possible to partition a string S
// into two non-empty sub-sequences
// S1 and S2.
function check(S, n) {
  let arr = new Array(n);
  for (let i = 0; i < n; i++) {
    arr[i] = parseInt(S[i]);
  }
  let S1 = 0;
  for (let i = 0; i < n; i++) {
    if ((arr[i] & 1) !== 0) {
      S1++;
    }
  }

  if ((S1 & 1) !== 0 || S1 === 0) {
    return "No";
  }
  return "Yes";
}

// Test case
let S = "1122";
let N = S.length;

// Function Call
console.log(check(S, N));

// This code is contributed by ksam24000
Output

Yes

Time Complexity: O(N)
Auxiliary Space: O(N)

Related Articles:

My Personal Notes arrow_drop_up
Last Updated : 15 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials