Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Number of N length sequences whose product is M
Next article icon

Count of all subsequence whose product is a Composite number

Last Updated : 05 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[], the task is to find the number of non-empty subsequences from the given array such that the product of subsequence is a composite number.
Example: 
 

Input: arr[] = {2, 3, 4} 
Output: 5 
Explanation: 
There are 5 subsequences whose product is composite number {4}, {2, 3}, {2, 4}, {3, 4}, {2, 3, 4}.
Input: arr[] = {2, 1, 2} 
Output: 2 
Explanation: 
There is 2 subsequences whose product is composite number {2, 2}, {2, 1, 2} 
 

 

Approach: The approach used to find the count of such subsequences is similar to the approach used in this article. Also, the approach can slightly tweaked to get the count of subsequences whose product is a Prime number. 
To solve the problem mentioned above, we have to find the total number of non-empty subsequences and subtract the subsequence whose product is not a composite number. The 3 possible cases where the product is not a composite number are: 
 

  • Any nonempty combination of 1 that is 
     

pow(2, count of “1”) – 1
 

  • Any subsequence of length 1 which consists of a prime number that is basically the 
     

count of prime numbers

  • Combination of non-empty 1 with a prime number that is 
     

(pow(2, number of 1 ) – 1) * (count of prime numbers)

Below is the implementation of above approach: 
 

C++




// C++ implementation to count all
// subsequence whose product
// is Composite number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether a
// number is prime or not
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
 
            return false;
 
    return true;
}
 
// Function to find number of subsequences
// whose product is a composite number
int countSubsequences(int arr[], int n)
{
    // Find total non empty subsequence
    int totalSubsequence = pow(2, n) - 1;
 
    int countPrime = 0, countOnes = 0;
 
    // Find count of prime number and ones
    for (int i = 0; i < n; i++) {
        if (arr[i] == 1)
            countOnes++;
        else if (isPrime(arr[i]))
            countPrime++;
    }
 
    int compositeSubsequence;
 
    // Calculate the non empty one subsequence
    int onesSequence = pow(2, countOnes) - 1;
 
    // Find count of composite subsequence
    compositeSubsequence
        = totalSubsequence - countPrime
          - onesSequence
          - onesSequence * countPrime;
 
    return compositeSubsequence;
}
 
// Driver code
int main()
{
 
    int arr[] = { 2, 1, 2 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << countSubsequences(arr, n);
 
    return 0;
}
 
 

Java




// Java implementation to count all
// subsequence whose product
// is Composite number
import java.util.*;
class GFG{
 
// Function to check whether a
// number is prime or not
static boolean isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
 
            return false;
 
    return true;
}
 
// Function to find number of subsequences
// whose product is a composite number
static int countSubsequences(int arr[], int n)
{
    // Find total non empty subsequence
    int totalSubsequence = (int)(Math.pow(2, n) - 1);
 
    int countPrime = 0, countOnes = 0;
 
    // Find count of prime number and ones
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == 1)
            countOnes++;
        else if (isPrime(arr[i]))
            countPrime++;
    }
 
    int compositeSubsequence;
 
    // Calculate the non empty one subsequence
    int onesSequence = (int)(Math.pow(2, countOnes) - 1);
 
    // Find count of composite subsequence
    compositeSubsequence = totalSubsequence -
                                 countPrime -
                               onesSequence -
                               onesSequence *
                               countPrime;
 
    return compositeSubsequence;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 1, 2 };
 
    int n = arr.length;
 
    System.out.print(countSubsequences(arr, n));
}
}
 
// This code is contributed by 29AjayKumar
 
 

Python3




# Python3 implementation to count
# all subsequence whose product
# is composite number
 
# Function to check whether
# a number is prime or not
def isPrime(n):
     
    if (n <= 1):
        return False;
         
    for i in range(2, n):
        if (n % i == 0):
            return False;
 
    return True;
 
# Function to find number of subsequences
# whose product is a composite number
def countSubsequences(arr, n):
     
    # Find total non empty subsequence
    totalSubsequence = (int)(pow(2, n) - 1);
     
    countPrime = 0;
    countOnes = 0;
 
    # Find count of prime number and ones
    for i in range(n):
        if (arr[i] == 1):
            countOnes += 1;
             
        elif (isPrime(arr[i])):
            countPrime += 1;
 
    compositeSubsequence = 0;
 
    # Calculate the non empty one subsequence
    onesSequence = (int)(pow(2, countOnes) - 1);
 
    # Find count of composite subsequence
    compositeSubsequence = (totalSubsequence -
                                  countPrime -
                                onesSequence -
                                onesSequence *
                                  countPrime);
 
    return compositeSubsequence;
 
# Driver code
if __name__ == '__main__':
     
    arr = [ 2, 1, 2 ];
    n = len(arr);
 
    print(countSubsequences(arr, n));
 
# This code is contributed by 29AjayKumar
 
 

C#




// C# implementation to count all
// subsequence whose product
// is Composite number
using System;
class GFG{
 
// Function to check whether a
// number is prime or not
static bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
 
            return false;
 
    return true;
}
 
// Function to find number of subsequences
// whose product is a composite number
static int countSubsequences(int []arr, int n)
{
    // Find total non empty subsequence
    int totalSubsequence = (int)(Math.Pow(2, n) - 1);
 
    int countPrime = 0, countOnes = 0;
 
    // Find count of prime number and ones
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == 1)
            countOnes++;
        else if (isPrime(arr[i]))
            countPrime++;
    }
 
    int compositeSubsequence;
 
    // Calculate the non empty one subsequence
    int onesSequence = (int)(Math.Pow(2, countOnes) - 1);
 
    // Find count of composite subsequence
    compositeSubsequence = totalSubsequence -
                                 countPrime -
                               onesSequence -
                               onesSequence *
                                 countPrime;
 
    return compositeSubsequence;
}
 
// Driver code
public static void Main()
{
    int []arr = { 2, 1, 2 };
 
    int n = arr.Length;
 
    Console.Write(countSubsequences(arr, n));
}
}
 
// This code is contributed by Nidhi_biet
 
 

Javascript




<script>
 
 
// Javascript implementation to count all
// subsequence whose product
// is Composite number
 
// Function to check whether a
// number is prime or not
function isPrime(n)
{
    if (n <= 1)
        return false;
    for (var i = 2; i < n; i++)
        if (n % i == 0)
 
            return false;
 
    return true;
}
 
// Function to find number of subsequences
// whose product is a composite number
function countSubsequences( arr, n)
{
    // Find total non empty subsequence
    var totalSubsequence = Math.pow(2, n) - 1;
 
    var countPrime = 0, countOnes = 0;
 
    // Find count of prime number and ones
    for (var i = 0; i < n; i++) {
        if (arr[i] == 1)
            countOnes++;
        else if (isPrime(arr[i]))
            countPrime++;
    }
 
    var compositeSubsequence;
 
    // Calculate the non empty one subsequence
    var onesSequence = Math.pow(2, countOnes) - 1;
 
    // Find count of composite subsequence
    compositeSubsequence
        = totalSubsequence - countPrime
          - onesSequence
          - onesSequence * countPrime;
 
    return compositeSubsequence;
}
 
// Driver code
var arr = [ 2, 1, 2 ];
var n = arr.length;
document.write( countSubsequences(arr, n));
 
</script>
 
 
Output: 
2

 



Next Article
Number of N length sequences whose product is M
author
apurvaraj
Improve
Article Tags :
  • Arrays
  • DSA
  • Mathematical
  • Prime Number
  • subsequence
Practice Tags :
  • Arrays
  • Mathematical
  • Prime Number

Similar Reads

  • Count pairs in an array whose product is composite number
    Given an array, arr[] of size N, the task is to count all the pairs of the given array whose product is a composite number. Examples: Input: arr[] = {1, 4, 7}Output: 2Explanation:Pairs whose product is a composite number are:(4, 7), (1, 4).Therefore, the required output is 2. Input: arr[] = {1, 2, 8
    15 min read
  • Number of N length sequences whose product is M
    Given two integers N and M, the task is to find the count of possible sequences a1, a2, ... of length N such that the product of all the elements of the sequence is M.Examples: Input: N = 2, M = 6 Output: 4 Possible sequences are {1, 6}, {2, 3}, {3, 2} and {6, 1}Input: N = 3, M = 24 Output: 30 Appro
    8 min read
  • Count array elements whose product of digits is a Composite Number
    Given an array arr[] consisting of N non-negative integers, the task is to count the number of array elements whose product of digits is a composite number. Examples: Input: arr[] = {13, 55, 7, 13, 11, 71, 233, 144, 89}Output: 4Explanation: The array elements having product of digits equal to a comp
    9 min read
  • Find a sequence of N prime numbers whose sum is a composite number
    Given an integer N and the task is to find a sequence of N prime numbers whose sum is a composite number.Examples: Input: N = 5 Output: 2 3 5 7 11 2 + 3 + 5 + 7 + 11 = 28 which is composite.Input: N = 6 Output: 3 5 7 11 13 17 Approach: The sum of two prime numbers is always even which is composite a
    8 min read
  • Count of K length subsequence whose product is even
    Given an array arr[] and an integer K, the task is to find number of non empty subsequence of length K from the given array arr of size N such that the product of subsequence is a even number.Example: Input: arr[] = [2, 3, 1, 7], K = 3 Output: 3 Explanation: There are 3 subsequences of length 3 whos
    6 min read
  • Number of subsequences with negative product
    Given an array arr[] of N integers, the task is to find the count of all the subsequences of the array that have a negative products. Examples: Input: arr[] = {1, -5, -6} Output: 4 Explanation {-5}, {-6}, {1, -5} and {1, -6} are the only possible subsequences Input: arr[] = {2, 3, 1} Output: 0 Expla
    6 min read
  • Number of subsequences of the form a^i b^j c^k
    Given a string, count number of subsequences of the form aibjck, i.e., it consists of i ’a’ characters, followed by j ’b’ characters, followed by k ’c’ characters where i >= 1, j >=1 and k >= 1. Note: Two subsequences are considered different if the set of array indexes picked for the 2 sub
    15+ min read
  • Count of subsequences which consists exactly K prime numbers
    Given an integer K and an array arr[], the task is to find the number of subsequences from the given array such that each subsequence consists exactly K prime numbers.Example: Input: K = 2, arr = [2, 3, 4, 6] Output: 4 Explanation: There are 4 subsequences which consists exactly 2 prime numbers {2,
    7 min read
  • Check if the product of every contiguous subsequence is different or not in a number
    Given an integer N, the task is to check if the product of every consecutive set of digits is distinct or not. Examples: Input: N = 234 Output: Yes SetProduct{2}2{2, 3}2 * 3 = 6{2, 3, 4}2 * 3 * 4 = 24{3}3{3, 4}3 * 4 = 12{4}4 All the products are distinct. Input: N = 1234 Output: No Set {1, 2} and {2
    6 min read
  • Product of all the Composite Numbers in an array
    Given an array of integers. The task is to calculate the product of all the composite numbers in an array. Note: 1 is neither prime nor composite. Examples: Input: arr[] = {2, 3, 4, 5, 6, 7} Output: 24 Composite numbers are 4 and 6. So, product = 24 Input: arr[] = {11, 13, 17, 20, 19} Output: 20 Nai
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences