Find if there is any subset of size K with 0 sum in an array of -1 and +1

Given an integer K and an array arr containing only 1 and -1, the task is to find if there is any subset of size K sum of whose elements is 0.

Examples:

Input: arr[] = {1, -1, 1}, K = 2
Output: Yes
{1, -1} is a valid subset



Input: arr[] = {1, 1, -1, -1, 1}, K = 5
Output: No

Approach:

  • In order for the sum to be 0, there has to be equal number of 1 and -1 in the subset.
  • If K is odd then no subset will satisfy the given condition.
  • Else if K is even then we need to choose exactly (K / 2) 1’s and (K / 2) -1’s in order to form the subset so that the sum of all of it’s elements is 0
  • So, if K is even and number of 1’s ≥ K / 2 and number of -1’s ≥ K / 2 then print Yes else print No.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find if there is a subset of size
// k with sum 0 in an array of -1 and +1
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the number of 1's in the array
int countOnes(int n, int a[])
{
    int i, count = 0;
    for (i = 0; i < n; i++)
        if (a[i] == 1)
            count++;
    return count;
}
  
bool isSubset(int arr[], int n, int k)
{
    int countPos1 = countOnes(n, arr);
    int countNeg1 = n - countPos1;
  
    // If K is even and there are
    // at least K/2 1's and -1's
    return (k % 2 == 0 && countPos1 >= k / 2 && 
                          countNeg1 >= k / 2);
}
  
// Driver Program to test above function
int main()
{
    int a[] = { 1, 1, -1, -1, 1 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 5;
    if (isSubset(a, n, k))
      cout << "Yes";
    else
      cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find if there is a subset of size
// k with sum 0 in an array of -1 and +1
  
import java.io.*;
  
class GFG {
     
  
// Function to return the number of 1's in the array
static int countOnes(int n, int a[])
{
    int i, count = 0;
    for (i = 0; i < n; i++)
        if (a[i] == 1)
            count++;
    return count;
}
  
static boolean isSubset(int arr[], int n, int k)
{
    int countPos1 = countOnes(n, arr);
    int countNeg1 = n - countPos1;
  
    // If K is even and there are
    // at least K/2 1's and -1's
    return (k % 2 == 0 && countPos1 >= k / 2 && 
                        countNeg1 >= k / 2);
}
  
// Driver Program to test above function
public static void main (String[] args) {
        int []a = { 1, 1, -1, -1, 1 };
    int n = a.length;
    int k = 5;
    if (isSubset(a, n, k))
     System.out.println( "Yes");
    else
    System.out.println( "No");
    }
}
// This code is contributed by shs

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find if there is 
# a subset of size k with sum 0 in an
# array of -1 and +1 
  
# Function to return the number of
# 1's in the array 
def countOnes(n, a): 
  
    count = 0
    for i in range(0, n): 
        if a[i] == 1
            count += 1
    return count 
  
def isSubset(arr, n, k): 
  
    countPos1 = countOnes(n, arr) 
    countNeg1 = n - countPos1 
  
    # If K is even and there are 
    # at least K/2 1's and -1's 
    return (k % 2 == 0 and countPos1 >= k // 2 and
                           countNeg1 >= k // 2
  
# Driver Code
if __name__ == "__main__"
  
    a = [1, 1, -1, -1, 1
    n = len(a) 
    k = 5
      
    if isSubset(a, n, k) == True
        print("Yes"
    else:
        print("No"
      
# This code is contributed 
# by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find if there is 
// a subset of size k with sum 0
// in an array of -1 and +1
using System;
  
class GFG
{
  
// Function to return the number
// of 1's in the array
static int countOnes(int n, int []a)
{
    int i, count = 0;
    for (i = 0; i < n; i++)
        if (a[i] == 1)
            count++;
    return count;
}
  
static bool isSubset(int []arr,
                     int n, int k)
{
    int countPos1 = countOnes(n, arr);
    int countNeg1 = n - countPos1;
  
    // If K is even and there are
    // at least K/2 1's and -1's
    return (k % 2 == 0 && countPos1 >= k / 2 && 
                          countNeg1 >= k / 2);
}
  
// Driver Code
public static void Main ()
{
    int []a = { 1, 1, -1, -1, 1 };
    int n = a.Length;
    int k = 5;
    if (isSubset(a, n, k))
        Console.WriteLine( "Yes");
    else
        Console.WriteLine( "No");
}
}
  
// This code is contributed by shs

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find if there 
// is a subset of size k with 
// sum 0 in an array of -1 and +1
  
// Function to return the number
// of 1's in the array
function countOnes($n, $a)
{
    $count = 0;
    for ($i = 0; $i < $n; $i++)
        if ($a[$i] == 1)
            $count++;
    return $count;
}
  
function isSubset($arr, $n, $k)
{
    $countPos1 = countOnes($n, $arr);
    $countNeg1 = $n - $countPos1;
  
    // If K is even and there are
    // at least K/2 1's and -1's
    return ($k % 2 == 0 && $countPos1 >= $k / 2 && 
                           $countNeg1 >= $k / 2);
}
  
// Driver Code
$a = array(1, 1, -1, -1, 1);
$n = sizeof($a);
$k = 5;
  
if (isSubset($a, $n, $k))
    echo "Yes";
else
    echo "No";
  
// This code is contributed
// by Akanksha Rai
?>

chevron_right


Output:

No

Time Complexity: O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.