Divide array into two arrays with their sum as both odd or both even
Given an array arr[] consisting of N integers, the task is to check if it is possible to divide the entire array into two arrays such that the sum of elements in new arrays is either both odd or both even. Also, each new array must have at least one element. If it is possible, print “Yes”. Otherwise, print “No”.
Examples:
Input: arr[] = {3, 1, 1, 1, 1, 1 }
Output: Yes
Explanation: The given array can be divided into two arrays as: {3, 1, 1, 1, 1} and {1}.
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: No
Explanation: No possible distribution exists such that both new arrays have either both odd or both even sum.
Approach: To solve the problem follow the below idea:
The idea is to observe the fact that if the count of odd numbers present in the given array is even, only then, the given array can be divided into two arrays having odd sum. If the count of odd numbers is odd, then one array will have odd number of odd elements & other will have even number of odd elements, and therefore answer will be “No”. Also, N should be greater than 1.
Follow the steps below to solve the problem:
- Check if N == 1, and print “No”.
- Find the total number of odd elements present in the given array and store it in a variable say, oddCnt.
- Check if oddcnt is even and N > 1, print “Yes”, else print “No”.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void canDivideArray( int arr[], int n)
{
int oddCnt = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 1)
oddCnt++;
}
if (oddCnt % 2 == 0 && n > 1) {
cout << "Yes\n" ;
}
else {
cout << "NO\n" ;
}
}
int main()
{
int arr1[] = { 3, 1, 1, 1, 1, 1 };
int N1 = 6;
canDivideArray(arr1, N1);
int arr2[] = { 1, 2, 3, 4, 5, 6 };
int N2 = 6;
canDivideArray(arr2, N2);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void canDivideArray( int arr[], int n)
{
int oddCnt = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 1 )
oddCnt++;
}
if (oddCnt % 2 == 0 && n > 1 ) {
System.out.println( "Yes" );
}
else {
System.out.println( "NO" );
}
}
public static void main(String[] args)
{
int [] arr1 = { 3 , 1 , 1 , 1 , 1 , 1 };
int N1 = 6 ;
canDivideArray(arr1, N1);
int [] arr2 = { 1 , 2 , 3 , 4 , 5 , 6 };
int N2 = 6 ;
canDivideArray(arr2, N2);
}
}
|
Python3
def canDivideArray(arr, n):
oddCnt = 0
for i in range (n):
if arr[i] % 2 = = 1 :
oddCnt + = 1
if oddCnt % 2 = = 0 and n > 1 :
print ( "Yes" )
else :
print ( "NO" )
arr1 = [ 3 , 1 , 1 , 1 , 1 , 1 ]
N1 = 6
canDivideArray(arr1, N1)
arr2 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
N2 = 6
canDivideArray(arr2, N2)
|
C#
using System;
public class GFG {
static void canDivideArray( int [] arr, int n)
{
int oddCnt = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 1)
oddCnt++;
}
if (oddCnt % 2 == 0 && n > 1) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "NO" );
}
}
static public void Main()
{
int [] arr1 = { 3, 1, 1, 1, 1, 1 };
int N1 = 6;
canDivideArray(arr1, N1);
int [] arr2 = { 1, 2, 3, 4, 5, 6 };
int N2 = 6;
canDivideArray(arr2, N2);
}
}
|
Javascript
function canDivideArray(arr, n) {
let oddCnt = 0;
for (let i = 0; i < n; i++) {
if (arr[i] % 2 === 1)
oddCnt++;
}
if (oddCnt % 2 === 0 && n > 1) {
console.log( "Yes" );
}
else {
console.log( "NO" );
}
}
const arr1 = [3, 1, 1, 1, 1, 1];
const N1 = 6;
canDivideArray(arr1, N1);
const arr2 = [1, 2, 3, 4, 5, 6];
const N2 = 6;
canDivideArray(arr2, N2);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...