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++
#include <bits/stdc++.h>
using namespace std;
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" ;
}
int main()
{
string S = "1122" ;
int N = S.length();
cout << check(S, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
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" ;
}
public static void main(String[] args)
{
String S = "1122" ;
int N = S.length();
System.out.println(check(S, N));
}
}
|
Python3
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"
S = "1122"
N = len (S)
print (check(S,N))
|
C#
using System;
public class GFG {
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()
{
String S = "1122" ;
int N = S.Length;
Console.WriteLine(check(S, N));
}
}
|
Javascript
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" ;
}
let S = "1122" ;
let N = S.length;
console.log(check(S, N));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Articles:
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Jan, 2023
Like Article
Save Article