Find if there is any subset of size K with 0 sum in an array of -1 and +1
Last Updated :
16 Sep, 2023
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++
#include <bits/stdc++.h>
using namespace std;
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;
return (k % 2 == 0 && countPos1 >= k / 2 &&
countNeg1 >= k / 2);
}
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;
}
|
Java
import java.io.*;
class GFG {
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;
return (k % 2 == 0 && countPos1 >= k / 2 &&
countNeg1 >= k / 2 );
}
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" );
}
}
|
Python3
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
return (k % 2 = = 0 and countPos1 > = k / / 2 and
countNeg1 > = k / / 2 )
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" )
|
C#
using System;
class GFG
{
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;
return (k % 2 == 0 && countPos1 >= k / 2 &&
countNeg1 >= k / 2);
}
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" );
}
}
|
Javascript
<script>
function countOnes(n, a)
{
var i, count = 0;
for (i = 0; i < n; i++)
if (a[i] == 1)
count++;
return count;
}
function isSubset(arr, n, k)
{
var countPos1 = countOnes(n, arr);
var countNeg1 = n - countPos1;
return (k % 2 == 0 && countPos1 >= k / 2 &&
countNeg1 >= k / 2);
}
var a = [1, 1, -1, -1, 1];
var n = a.length;
var k = 5;
if (isSubset(a, n, k))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
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 ;
return ( $k % 2 == 0 && $countPos1 >= $k / 2 &&
$countNeg1 >= $k / 2);
}
$a = array (1, 1, -1, -1, 1);
$n = sizeof( $a );
$k = 5;
if (isSubset( $a , $n , $k ))
echo "Yes" ;
else
echo "No" ;
?>
|
Time Complexity: O(n)
Approach:
- Approach to solve this problem could be to use a sliding window technique and keep track of the sum of the elements in the window.
- We can start by initializing two pointers, left and right, and a variable sum to 0.
- Then we can move the right pointer to the right until the window size is K.
- If the sum of the elements in the window is 0, we can return true as we have found a subset of size K with sum 0.
- Otherwise, we can move the left pointer to the right and subtract the element at the left pointer from the sum, and continue this process until we have found a subset of size K with sum 0 or until the window size is less than K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isSubset( int arr[], int n, int k) {
int left = 0, right = 0, sum = 0;
while (right < n) {
sum += arr[right];
if (right - left + 1 == k) {
if (sum == 0) {
return true ;
}
sum -= arr[left];
left++;
}
right++;
}
return false ;
}
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;
}
|
Java
public class Main {
public static boolean isSubset( int [] arr, int n, int k) {
int left = 0 , right = 0 , sum = 0 ;
while (right < n) {
sum += arr[right];
if (right - left + 1 == k) {
if (sum == 0 ) {
return true ;
}
sum -= arr[left];
left++;
}
right++;
}
return false ;
}
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" );
}
}
}
|
Python3
def isSubset(arr, n, k):
left, right, total_sum = 0 , 0 , 0
while right < n:
total_sum + = arr[right]
if right - left + 1 = = k:
if total_sum = = 0 :
return True
total_sum - = arr[left]
left + = 1
right + = 1
return False
def main():
a = [ 1 , 1 , - 1 , - 1 , 1 ]
n = len (a)
k = 5
if isSubset(a, n, k):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
main()
|
C#
using System;
public class GFG {
public static bool IsSubset( int [] arr, int n, int k)
{
int left = 0, right = 0, sum = 0;
while (right < n) {
sum += arr[right];
if (right - left + 1
== k)
{
if (sum == 0)
{
return true ;
}
sum -= arr[left];
left++;
}
right++;
}
return false ;
}
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)) {
Console.WriteLine(
"Yes" );
}
else {
Console.WriteLine(
"No" );
}
}
}
|
Javascript
function isSubset(arr, n, k) {
let left = 0;
let right = 0;
let sum = 0;
while (right < n) {
sum += arr[right];
if (right - left + 1 === k) {
if (sum === 0) {
return true ;
}
sum -= arr[left];
left++;
}
right++;
}
return false ;
}
function main() {
const a = [1, 1, -1, -1, 1];
const n = a.length;
const k = 5;
if (isSubset(a, n, k)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
}
main();
|
Time Complexity: O(N), where N is the size of the input array, as we iterate over the array at most twice.
Space Complexity: O(1), as we only use a constant amount of extra space to store the pointers and the sum variable.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...