Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
Last Updated :
04 May, 2021
Given an array of positive numbers, the task is to find the bitwise AND of the sum of non-prime numbers and the sum of prime numbers. Note that 1 is neither prime nor composite.
Examples:
Input: arr[] = {1, 3, 5, 10, 15, 7}
Output: 9
Sum of non-primes = 10 + 15 = 25
Sum of primes = 3 + 5 + 7 = 15
25 & 15 = 9
Input: arr[] = {3, 4, 6, 7}
Output: 10
Naive approach: A simple solution is to traverse the array and keep checking for every element if it is prime or not. If the number is prime, then add it to S1 which stores the sum of prime numbers from the array else add it to S2 which stores the sum of non-prime numbers. Finally, print S1 & S2.
Time complexity: O(N * sqrt(N))
Efficient approach: Generate all the primes up to the maximum element of the array using the Sieve of Eratosthenes and store them in a hash. Now, traverse the array and check if the number is prime or not. In the end, calculate and print the bitwise AND of the sum of prime numbers and the sum of composite numbers.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the bitwise AND of the
// sum of primes and the sum of non-primes
int calculateAND(int arr[], int n)
{
// 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);
// Remaining part of SIEVE
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] == true) {
// Update all multiples of p
for (int i = p * 2; i <= max_val; i += p)
prime[i] = false;
}
}
// Store the sum of primes in S1 and
// the sum of non-primes in S2
int S1 = 0, S2 = 0;
for (int i = 0; i < n; i++) {
if (prime[arr[i]]) {
// The number is prime
S1 += arr[i];
}
else if (arr[i] != 1) {
// The number is not prime
S2 += arr[i];
}
}
// Return the bitwise AND of the sums
return (S1 & S2);
}
// Driver code
int main()
{
int arr[] = { 3, 4, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << calculateAND(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
class GFG
{
static int getMax(int[] A)
{
int max = Integer.MIN_VALUE;
for (int i: A)
{
max = Math.max(max, i);
}
return max;
}
// Function to return the bitwise AND of the
// sum of primes and the sum of non-primes
static int calculateAND(int arr[], int n)
{
// using Collections.max() to find
// maximum element using only 1 line.
// Find maximum value in the array
int max_val = getMax(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.
boolean prime[] = new boolean [max_val + 1];
int i;
for (i = 0; i < max_val + 1; i++)
prime[i] = true;
// Remaining part of SIEVE
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] == true)
{
// Update all multiples of p
for ( i = p * 2; i <= max_val; i += p)
prime[i] = false;
}
}
// Store the sum of primes in S1 and
// the sum of non-primes in S2
int S1 = 0, S2 = 0;
for (i = 0; i < n; i++)
{
if (prime[arr[i]])
{
// The number is prime
S1 += arr[i];
}
else if (arr[i] != 1)
{
// The number is not prime
S2 += arr[i];
}
}
// Return the bitwise AND of the sums
return (S1 & S2);
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 3, 4, 6, 7 };
int n = arr.length;
System.out.println(calculateAND(arr, n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to return the bitwise AND of the
# sum of primes and the sum of non-primes
def calculateAND(arr, n):
# 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)]
# Remaining part of SIEVE
prime[0] = False
prime[1] = False
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]):
# Update all multiples of p
for i in range(2 * p, max_val + 1, p):
prime[i] = False
# Store the sum of primes in S1 and
# the sum of non-primes in S2
S1 = 0
S2 = 0
for i in range(n):
if (prime[arr[i]]):
# The number is prime
S1 += arr[i]
elif (arr[i] != 1):
# The number is not prime
S2 += arr[i]
# Return the bitwise AND of the sums
return (S1 & S2)
# Driver code
arr = [3, 4, 6, 7]
n = len(arr)
print(calculateAND(arr, n))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
static int getMax(int[] A)
{
int max = int.MinValue;
foreach (int i in A)
{
max = Math.Max(max, i);
}
return max;
}
// Function to return the bitwise AND of the
// sum of primes and the sum of non-primes
static int calculateAND(int []arr, int n)
{
// using Collections.max() to find
// maximum element using only 1 line.
// Find maximum value in the array
int max_val = getMax(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.
bool []prime = new bool [max_val + 1];
int i;
for (i = 0; i < max_val + 1; i++)
prime[i] = true;
// Remaining part of SIEVE
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] == true)
{
// Update all multiples of p
for (i = p * 2; i <= max_val; i += p)
prime[i] = false;
}
}
// Store the sum of primes in S1 and
// the sum of non-primes in S2
int S1 = 0, S2 = 0;
for (i = 0; i < n; i++)
{
if (prime[arr[i]])
{
// The number is prime
S1 += arr[i];
}
else if (arr[i] != 1)
{
// The number is not prime
S2 += arr[i];
}
}
// Return the bitwise AND of the sums
return (S1 & S2);
}
// Driver code
public static void Main (String[] args)
{
int []arr = { 3, 4, 6, 7 };
int n = arr.Length;
Console.WriteLine(calculateAND(arr, n));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the bitwise AND of the
// sum of primes and the sum of non-primes
function calculateAND(arr, n)
{
// Find maximum value in the array
var max_val = arr.reduce((a,b)=>Math.max(a,b));
// 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.
var prime = Array(max_val + 1).fill(true);
// Remaining part of SIEVE
prime[0] = false;
prime[1] = false;
for (var 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 (var i = p * 2; i <= max_val; i += p)
prime[i] = false;
}
}
// Store the sum of primes in S1 and
// the sum of non-primes in S2
var S1 = 0, S2 = 0;
for (var i = 0; i < n; i++) {
if (prime[arr[i]]) {
// The number is prime
S1 += arr[i];
}
else if (arr[i] != 1) {
// The number is not prime
S2 += arr[i];
}
}
// Return the bitwise AND of the sums
return (S1 & S2);
}
// Driver code
var arr = [ 3, 4, 6, 7 ];
var n = arr.length;
document.write( calculateAND(arr, n));
</script>
Time Complexity: O(N * log(log(N))
Space Complexity: O(max_val) where max_val is the maximum value of an element in the given array.
Similar Reads
Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array Given an array of positive numbers, the task is to calculate the absolute difference between sum of non-prime numbers and prime numbers. Note: 1 is neither prime nor non-prime. Examples: Input : 1 3 5 10 15 7 Output : 10 Explanation: Sum of non-primes = 25 Sum of primes = 15 Input : 3 4 6 7 Output :
15+ min read
Sum and product of k smallest and k largest composite numbers in the array Given an integer k and an array of integers arr, the task is to find the sum and product of k smallest and k largest composite numbers in the array. Assume that there are at least k composite numbers in the array. Examples: Input: arr[] = {2, 5, 6, 8, 10, 11}, k = 2 Output: Sum of k-minimum composit
15+ min read
Sum of all the Composite Numbers from Odd indices of the given array Given an array arr[] of size N which contains at least one composite number. The task is to find out the sum of all the composite numbers in the array which are at odd indices (where indexing is 1 based). Examples: Input: arr = [13, 5, 8, 16, 25] Output: 33 Explanation: The number in the odd indices
6 min read
Represent the given number as the sum of two composite numbers Given an integer N, the task is to represent N as the sum of two composite integers. There can be multiple ways possible, print any one of them. If it is not possible to represent the number as the sum of two composite numbers then print -1.Examples: Input: N = 13 Output: 4 9 4 + 9 = 13 and both 4 a
4 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 the number of primes in the prefix sum array of the given array Given an array arr[] of N integers, the task is to count the number of primes in the prefix sum array of the given array. Examples: Input: arr[] = {1, 4, 8, 4} Output: 3 The prefix sum array is {1, 5, 13, 17} and the three primes are 5, 13 and 17. Input: arr[] = {1, 5, 2, 3, 7, 9} Output: 1 Approach
9 min read