- Arrays, Bit Magic, Combinatorial, combionatrics, subsequence

Count of subsequences with sum two less than the array sum

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:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.Input: vec[] = {2, 0, 1, 2, 1}, N=5Output: 6Explanation: {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=5Output: 4Explanation: {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 using namespace std;  void countTotal(vector& vec){              int sum = accumulate(vec.begin(),                         vec.end(), 0LL);      int N = (int)vec.size();              int answer = 0;              for (int mask = 0; mask < (1