Count and Sum of composite elements in an array
Given an array ‘arr’ of positive integers, the task is to count the number of composite numbers in the array.
Note: 1 is neither Prime nor Composite.
Examples:
Input: arr[] = {1, 3, 4, 5, 7}
Output: 1
4 is the only composite number.
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 2
Naive Approach: A simple solution is to traverse the array and do a primality test on every element.
Efficient Approach: Using Sieve of Eratosthenes generate a boolean vector upto the size of the maximum element from the array which can be used to check whether a number is prime or not. Also add 0 and 1 as a prime so that they don’t get counted as composite numbers. Now traverse the array and find the count of those elements which are composite using the generated boolean vector.
Below is the implementation of the above approach:
C++
// C++ program to count the // number of composite numbers // in the given array #include <bits/stdc++.h> using namespace std; // Function that returns the // the count of composite numbers int compositeCount(int arr[], int n, int* sum) { // Find maximum value in the array int max_val = *max_element(arr, arr + n); // Use sieve to find all prime numbers // less than or equal to max_val // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. vector<bool> prime(max_val + 1, true); // Set 0 and 1 as primes as // they don't need to be // counted as composite numbers prime[0] = true; prime[1] = true; for (int p = 2; p * p <= max_val; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= max_val; i += p) prime[i] = false; } } // Count all composite // numbers in the arr[] int count = 0; for (int i = 0; i < n; i++) if (!prime[arr[i]]) { count++; *sum = *sum + arr[i]; } return count; } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int n = sizeof(arr) / sizeof(arr[0]); int sum = 0; cout << "Count of Composite Numbers = " << compositeCount(arr, n, ∑); cout << "\nSum of Composite Numbers = " << sum; return 0; } |
Java
import java.util.*; // Java program to count the // number of composite numbers // in the given array class GFG { static int sum = 0; // Function that returns the // the count of composite numbers static int compositeCount(int arr[], int n) { // Find maximum value in the array int max_val = Arrays.stream(arr).max().getAsInt(); // Use sieve to find all prime numbers // less than or equal to max_val // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. Vector<Boolean> prime = new Vector<Boolean>(max_val + 1); for (int i = 0; i < max_val + 1; i++) { prime.add(i, Boolean.TRUE); } // Set 0 and 1 as primes as // they don't need to be // counted as composite numbers prime.add(0, Boolean.TRUE); prime.add(1, Boolean.TRUE); for (int p = 2; p * p <= max_val; p++) { // If prime[p] is not changed, then // it is a prime if (prime.get(p) == true) { // Update all multiples of p for (int i = p * 2; i <= max_val; i += p) { prime.add(i, Boolean.FALSE); } } } // Count all composite // numbers in the arr[] int count = 0; for (int i = 0; i < n; i++) { if (!prime.get(arr[i])) { count++; sum = sum + arr[i]; } } return count; } // Driver code public static void main(String[] args) { int arr[] = {1, 2, 3, 4, 5, 6, 7}; int n = arr.length; System.out.print("Count of Composite Numbers = " + compositeCount(arr, n)); System.out.print("\nSum of Composite Numbers = " + sum); } } // This code has been contributed by 29AjayKumar |
Python3
# Python3 program to count the # number of composite numbers # in the given array # Function that returns the # the count of composite numbers def compositeCount(arr, n): Sum = 0 # Find maximum value in the array max_val = max(arr) # Use sieve to find all prime numbers # less than or equal to max_val # Create a boolean array "prime[0..n]". # A value in prime[i] will finally be # false if i is Not a prime, else True. prime = [True for i in range(max_val + 1)] # Set 0 and 1 as primes as # they don't need to be # counted as composite numbers prime[0] = True prime[1] = True for p in range(2, max_val + 1): if p * p > max_val: break # If prime[p] is not changed, # then it is a prime if (prime[p] == True): # Update all multiples of p for i in range(p * 2, max_val + 1, p): prime[i] = False # Count all composite numbers # in the arr[] count = 0 for i in range(n): if (prime[arr[i]] == False): count += 1 Sum = Sum + arr[i] return count, Sum # Driver code arr = [1, 2, 3, 4, 5, 6, 7 ] n = len(arr) count, Sum = compositeCount(arr, n) print("Count of Composite Numbers = ", count) print("Sum of Composite Numbers = ", Sum) // This code is contributed by Mohit Kumar |
C#
// C# program to count the // number of composite numbers // in the given array using System; using System.Linq; using System.Collections; class GFG { static int sum1=0; // Function that returns the // the count of composite numbers static int compositeCount(int []arr, int n, int sum) { // Find maximum value in the array int max_val = arr.Max(); // Use sieve to find all prime numbers // less than or equal to max_val // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. bool[] prime=new bool[max_val + 1]; // Set 0 and 1 as primes as // they don't need to be // counted as composite numbers prime[0] = false; prime[1] = false; for (int p = 2; p * p <= max_val; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == false) { // Update all multiples of p for (int i = p * 2; i <= max_val; i += p) prime[i] = true; } } // Count all composite // numbers in the arr[] int count = 0; for (int i = 0; i < n; i++) if (prime[arr[i]]) { count++; sum = sum + arr[i]; } sum1 = sum; return count; } // Driver code static void Main() { int []arr = { 1, 2, 3, 4, 5, 6, 7 }; int n = arr.Length; int sum = 0; Console.Write("Count of Composite Numbers = "+ compositeCount(arr, n, sum)); Console.Write("\nSum of Composite Numbers = "+sum1); } } // This code is contributed by mits |
PHP
<?php // PHP program to count the // number of composite numbers // in the given array // Function that returns the // the count of composite numbers function compositeCount($arr, $n, &$sum) { // Find maximum value in the array $max_val = max($arr); // Use sieve to find all prime numbers // less than or equal to max_val // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. $prime=array_fill(0,$max_val + 1, true); // Set 0 and 1 as primes as // they don't need to be // counted as composite numbers $prime[0] = true; $prime[1] = true; for ($p = 2; $p * $p <= $max_val; $p++) { // If prime[p] is not changed, then // it is a prime if ($prime[$p] == true) { // Update all multiples of p for ($i = $p * 2; $i <= $max_val; $i += $p) $prime[$i] = false; } } // Count all composite // numbers in the arr[] $count = 0; for ($i = 0; $i < $n; $i++) if (!$prime[$arr[$i]]) { $count++; $sum = $sum + $arr[$i]; } return $count; } // Driver code $arr = array( 1, 2, 3, 4, 5, 6, 7 ); $n = count($arr); $sum = 0; echo "Count of Composite Numbers = ".compositeCount($arr, $n, $sum); echo "\nSum of Composite Numbers = ".$sum; // This code is contributed by mits ?> |
Count of Composite Numbers = 2 Sum of Composite Numbers = 10
Time complexity : O(n)
Recommended Posts:
- Count array elements that divide the sum of all other elements
- Count number of elements between two given elements in array
- Queries for the difference between the count of composite and prime numbers in a given range
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- For each element in 1st array count elements less than or equal to it in 2nd array
- Count number of even and odd elements in an array
- Count elements present in first array but not in second
- Count distinct elements in an array
- Product of all the Composite Numbers in an array
- Count the number of elements in an array which are divisible by k
- XOR of K smallest prime and composite numbers from the given array
- Sum and Product of all Composite numbers which are divisible by k in an array
- XOR of K largest prime and composite numbers from the given array
- Count occurrences of the average of array elements with a given number
- Count elements less than or equal to a given value in a sorted rotated array
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.



