Count of subsequences with sum two less than the array sum
Last Updated :
12 Dec, 2021
Given an array vec[] of size N of non-negative integers. The task is to count the number of subsequences with the sum equal to S – 2 where S is the sum of all the elements of the array.
Examples:
Input: vec[] = {2, 0, 1, 2, 1}, N=5
Output: 6
Explanation: {2, 0, 1, 1}, {2, 1, 1}, {2, 0, 2}, {2, 2}, {0, 1, 2, 1}, {1, 2, 1}
Input: vec[] = {2, 0, 2, 3, 1}, N=5
Output: 4
Explanation: {2, 0, 3, 1}, {2, 3, 1}, {0, 2, 3, 1}, {2, 3, 1}
Naive Approach: The idea is to generate all subsequences and check the sum of each and every individual subsequence equals S-2 or not.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void countTotal(vector< int >& vec)
{
int sum = accumulate(vec.begin(),
vec.end(), 0LL);
int N = ( int )vec.size();
int answer = 0;
for ( int mask = 0; mask < (1 << N); mask++) {
int curr = 0;
for ( int i = 0; i < N; i++) {
if ((mask & (1 << i)) != 0) {
curr += vec[i];
}
}
if (curr == sum - 2)
answer++;
}
cout << answer;
}
int main()
{
vector< int > vec = { 2, 0, 1, 2, 1 };
countTotal(vec);
return 0;
}
|
Java
public class GFG {
static int accumulate( int [] vec){
int sum1 = 0 ;
for ( int i = 0 ; i < vec.length; i++)
sum1 += vec[i];
return sum1;
}
static void countTotal( int []vec)
{
int sum = accumulate(vec);
int N = vec.length;
int answer = 0 ;
for ( int mask = 0 ; mask < ( 1 << N); mask++) {
int curr = 0 ;
for ( int i = 0 ; i < N; i++) {
if ((mask & ( 1 << i)) != 0 ) {
curr += vec[i];
}
}
if (curr == sum - 2 )
answer++;
}
System.out.print(answer);
}
public static void main (String[] args)
{
int []vec = { 2 , 0 , 1 , 2 , 1 };
countTotal(vec);
}
}
|
Python3
def countTotal(vec) :
Sum = sum (vec)
N = len (vec);
answer = 0 ;
for mask in range (( 1 << N)) :
curr = 0 ;
for i in range (N) :
if ((mask & ( 1 << i)) ! = 0 ) :
curr + = vec[i];
if (curr = = Sum - 2 ) :
answer + = 1 ;
print (answer);
if __name__ = = "__main__" :
vec = [ 2 , 0 , 1 , 2 , 1 ];
countTotal(vec);
|
C#
using System;
public class GFG {
static int accumulate( int [] vec)
{
int sum1 = 0;
for ( int i = 0; i < vec.Length; i++)
sum1 += vec[i];
return sum1;
}
static void countTotal( int [] vec)
{
int sum = accumulate(vec);
int N = vec.Length;
int answer = 0;
for ( int mask = 0; mask < (1 << N); mask++) {
int curr = 0;
for ( int i = 0; i < N; i++) {
if ((mask & (1 << i)) != 0) {
curr += vec[i];
}
}
if (curr == sum - 2)
answer++;
}
Console.WriteLine(answer);
}
public static void Main( string [] args)
{
int [] vec = { 2, 0, 1, 2, 1 };
countTotal(vec);
}
}
|
Javascript
<script>
function countTotal(vec) {
let sum = vec.reduce((acc, curr) => acc + curr, 0)
let N = vec.length;
let answer = 0;
for (let mask = 0; mask < (1 << N); mask++) {
let curr = 0;
for (let i = 0; i < N; i++) {
if ((mask & (1 << i)) != 0) {
curr += vec[i];
}
}
if (curr == sum - 2)
answer++;
}
document.write(answer);
}
let vec = [2, 0, 1, 2, 1];
countTotal(vec);
</script>
|
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use the Combinatorics that apart from 0’s, 1’s, and 2’s, all the other elements in our array will be part of the desired subsequences. Let’s call them extra elements. Then, count the occurrences of 0’s, 1’s, and 2’s in the array. Let’s say the count of 0’s is x, count of 1’s be y, count of 2’s be z.
- Let’s count the number of desired subsequences if all 2’s and extra elements are in the subsequence. Now there can be exactly y – 2 elements out of y. Note that there is no restriction for taking 0’s as it contributes nothing to our subsequence sum.
- Hence, the total count of such subsequences = count1 = 2x × yCy – 2 = 2x × yC2 ( Since, nC0 + nC1 + . . . + nCn = 2n).
- Let’s count the number of subsequences if all the 1’s are in our subsequence. Now there can be exactly z – 1 elements out of z.
- Hence, the total count of such subsequences = count2 = 2x × zCz – 1 = 2x × zC1
- Total count of subsequences whose sum is equal to S – 2, count = count1 + count2 = 2x × ( yC2 + zC1 )
Follow the steps below to solve the problem:
- Initialize the variable sum as the sum of the array.
- Initialize the variable answer as 0 to store the answer.
- Initialize the variables countOfZero, countOfOne and countOfTwo to store the count of 0, 1 and 2.
- Traverse the array vec[] using the iterator x and perform the following tasks:
- Count the occurrences of 0’s, 1’s, and 2’s.
- Initialize the variables value1 as 2countOfZero.
- Initialize the variable value2 as (countOfOne * (countOfOne – 1)) / 2.
- Initialize the variable value3 as countOfTwo.
- Set the value of answer as value1 * ( value2 + value).
- After performing the above steps, print the value of answer as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void countTotal(vector< int >& vec)
{
int sum = accumulate(vec.begin(),
vec.end(), 0LL);
int N = ( int )vec.size();
int answer = 0;
int countOfZero = 0,
countOfOne = 0,
countOfTwo = 0;
for ( auto x : vec) {
if (x == 0)
countOfZero++;
else if (x == 1)
countOfOne++;
else if (x == 2)
countOfTwo++;
}
int value1 = pow (2, countOfZero);
int value2
= (countOfOne
* (countOfOne - 1))
/ 2;
int value3 = countOfTwo;
answer = value1 * (value2 + value3);
cout << answer;
}
int main()
{
vector< int > vec = { 2, 0, 1, 2, 1 };
countTotal(vec);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void countTotal( int [] arr)
{
int N = arr.length;
int answer = 0 ;
int countOfZero = 0 , countOfOne = 0 , countOfTwo = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] == 0 )
countOfZero++;
else if (arr[i] == 1 )
countOfOne++;
else if (arr[i] == 2 )
countOfTwo++;
}
int value1 = ( 1 << countOfZero);
int value2 = (countOfOne * (countOfOne - 1 )) / 2 ;
int value3 = countOfTwo;
answer = value1 * (value2 + value3);
System.out.println(answer);
}
public static void main(String[] args)
{
int [] arr = { 2 , 0 , 1 , 2 , 1 };
countTotal(arr);
}
}
|
Python3
def countTotal(vec) :
sum1 = sum (vec);
N = len (vec);
answer = 0 ;
countOfZero = 0 ; countOfOne = 0 ; countOfTwo = 0 ;
for x in vec :
if (x = = 0 ) :
countOfZero + = 1 ;
elif (x = = 1 ) :
countOfOne + = 1 ;
elif (x = = 2 ) :
countOfTwo + = 1 ;
value1 = 2 * * countOfZero;
value2 = (countOfOne * (countOfOne - 1 )) / / 2 ;
value3 = countOfTwo;
answer = value1 * (value2 + value3);
print (answer);
if __name__ = = "__main__" :
vec = [ 2 , 0 , 1 , 2 , 1 ];
countTotal(vec);
|
C#
using System;
public class GFG {
static void countTotal( int [] arr)
{
int N = arr.Length;
int answer = 0;
int countOfZero = 0, countOfOne = 0, countOfTwo = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == 0)
countOfZero++;
else if (arr[i] == 1)
countOfOne++;
else if (arr[i] == 2)
countOfTwo++;
}
int value1 = (1 << countOfZero);
int value2 = (countOfOne * (countOfOne - 1)) / 2;
int value3 = countOfTwo;
answer = value1 * (value2 + value3);
Console.WriteLine(answer);
}
public static void Main( string [] args)
{
int [] arr = { 2, 0, 1, 2, 1 };
countTotal(arr);
}
}
|
Javascript
<script>
function countTotal(vec)
{
let sum = vec.reduce( function (accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
let N = vec.length;
let answer = 0;
let countOfZero = 0,
countOfOne = 0,
countOfTwo = 0;
for (let x of vec) {
if (x == 0)
countOfZero++;
else if (x == 1)
countOfOne++;
else if (x == 2)
countOfTwo++;
}
let value1 = Math.pow(2, countOfZero);
let value2
= (countOfOne
* (countOfOne - 1))
/ 2;
let value3 = countOfTwo;
answer = value1 * (value2 + value3);
document.write(answer);
}
let vec = [2, 0, 1, 2, 1];
countTotal(vec);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...