Count smaller primes on the right of each array element
Last Updated :
06 Jul, 2021
Given an array A[] of size N, the task for each array element is to count the array elements on their right that are smaller than it and are prime.
Examples:
Input: N = 10, A[] = {5, 5, 17, 9, 12, 15, 11, 7, 39, 3}
Output: 2 1 3 2 3 3 2 1 1 0
Explanation:
For i = 1, elements at j = [2, 10] are valid answer.
For i = 2, elements at j = [10] are valid answer.
For i = 3, elements at j = [7, 8, 10] are valid answer.
For i = 4, elements at j = [8, 10] are valid answer.
For i = 5, elements at j = [7, 8, 10] are valid answer.
For i = 6, elements at j = [7, 8, 10] are valid answer.
For i = 7, elements at j = [8, 10] are valid answer.
For i = 8, elements at j = [10] are valid answer.
For i = 9, elements at j = [10] are valid answer.
For i = 5, no elements are on its right.
Input: N = 6, A[] = {43, 3, 5, 7, 2, 41}
Output: 5 1 1 1 0 0
Explanation:
For i = 1, elements at j = [2, 3, 4, 5, 6] are valid answer.
For i = 2, elements at j = [5] are valid answer.
For i = 3, elements at j = [5] are valid answer.
For i = 4, elements at j = [5] are valid answer.
For i = 5, no valid answer.
For i = 6, no valid answer.
Naive Approach: The simplest approach to solve this problem is to traverse the array and for each element, A[i], iterate over all the elements on its right and count the number of elements smaller than A[i] and are prime.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
bool is_prime( int n)
{
if (n <= 1)
return 0;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
void countSmallerPrimes( int ar[], int N)
{
for ( int i = 0; i < N; i++) {
int count = 0;
for ( int j = i + 1; j < N; j++) {
if (ar[j] <= ar[i] && is_prime(ar[j])) {
count++;
}
}
cout << count << " " ;
}
}
int main()
{
int ar[] = { 43, 3, 5, 7, 2, 41 };
int N = sizeof ar / sizeof ar[0];
countSmallerPrimes(ar, N);
return 0;
}
|
Java
class GFG{
static boolean is_prime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ; i * i <= n; i++)
{
if (n % i == 0 )
return false ;
}
return true ;
}
static void countSmallerPrimes( int ar[], int N)
{
for ( int i = 0 ; i < N; i++)
{
int count = 0 ;
for ( int j = i + 1 ; j < N; j++)
{
if (ar[j] <= ar[i] && is_prime(ar[j]))
{
count++;
}
}
System.out.print(count + " " );
}
}
public static void main(String[] args)
{
int ar[] = { 43 , 3 , 5 , 7 , 2 , 41 };
int N = ar.length;
countSmallerPrimes(ar, N);
}
}
|
Python3
def is_prime(n):
if (n < = 1 ):
return 0
for i in range ( 2 , n + 1 ):
if i * i > n:
break
if (n % i = = 0 ):
return 0
return 1
def countSmallerPrimes(ar, N):
for i in range (N):
count = 0
for j in range (i + 1 , N):
if (ar[j] < = ar[i] and is_prime(ar[j])):
count + = 1
print (count, end = " " )
if __name__ = = '__main__' :
ar = [ 43 , 3 , 5 , 7 , 2 , 41 ]
N = len (ar)
countSmallerPrimes(ar, N)
|
C#
using System;
class GFG{
static bool is_prime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2;
i * i <= n; i++)
{
if (n % i == 0)
return false ;
}
return true ;
}
static void countSmallerPrimes( int [] ar,
int N)
{
for ( int i = 0; i < N; i++)
{
int count = 0;
for ( int j = i + 1; j < N; j++)
{
if (ar[j] <= ar[i] &&
is_prime(ar[j]))
{
count++;
}
}
Console.Write(count + " " );
}
}
public static void Main()
{
int [] ar = {43, 3, 5,
7, 2, 41};
int N = ar.Length;
countSmallerPrimes(ar, N);
}
}
|
Javascript
<script>
function is_prime(n)
{
if (n <= 1)
return false ;
for (let i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false ;
}
return true ;
}
function countSmallerPrimes(ar, N)
{
for (let i = 0; i < N; i++)
{
let count = 0;
for (let j = i + 1; j < N; j++)
{
if (ar[j] <= ar[i] && is_prime(ar[j]))
{
count++;
}
}
document.write(count + " " );
}
}
let ar = [ 43, 3, 5, 7, 2, 41 ];
let N = ar.length;
countSmallerPrimes(ar, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The idea is to observe that the above approach can be optimized by iterating over the array from right to left and calculate the required count of primes for each array element using Fenwick Tree. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
const int maxn = 1e6 + 5;
int BITree[maxn];
bool is_prime( int n)
{
if (n <= 1)
return 0;
for ( int i = 2; i * i <= n; i++)
if (n % i == 0)
return 0;
return 1;
}
void update_bitree( int BITree[],
int index, int value)
{
while (index <= maxn) {
BITree[index] += value;
index += (index & (-index));
}
}
int sum_bitree( int BITree[], int index)
{
int s = 0;
while (index > 0) {
s += BITree[index];
index -= (index & (-index));
}
return s;
}
void countSmallerPrimes( int BITree[],
int ar[], int N)
{
int ans[N];
for ( int i = N - 1; i >= 0; i--) {
ans[i] = sum_bitree(BITree, ar[i]);
if (is_prime(ar[i]))
update_bitree(BITree, ar[i], 1);
}
for ( int i = 0; i < N; i++)
cout << ans[i] << " " ;
}
int main()
{
int ar[] = { 5, 5, 17, 9, 12,
15, 11, 7, 39, 3 };
int N = sizeof ar / sizeof ar[0];
countSmallerPrimes(BITree, ar, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxn =
( int )1e6 + 5 ;
static int []BITree =
new int [maxn];
static boolean is_prime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ;
i * i <= n; i++)
if (n % i == 0 )
return false ;
return true ;
}
static void update_bitree( int BITree[],
int index,
int value)
{
while (index <= maxn)
{
BITree[index] += value;
index += (index & (-index));
}
}
static int sum_bitree( int BITree[],
int index)
{
int s = 0 ;
while (index > 0 )
{
s += BITree[index];
index -= (index & (-index));
}
return s;
}
static void countSmallerPrimes( int BITree[],
int ar[], int N)
{
int []ans = new int [N];
for ( int i = N - 1 ; i >= 0 ; i--)
{
ans[i] = sum_bitree(BITree,
ar[i]);
if (is_prime(ar[i]))
update_bitree(BITree,
ar[i], 1 );
}
for ( int i = 0 ; i < N; i++)
System.out.print(ans[i] + " " );
}
public static void main(String[] args)
{
int ar[] = { 5 , 5 , 17 , 9 , 12 ,
15 , 11 , 7 , 39 , 3 };
int N = ar.length;
countSmallerPrimes(BITree, ar, N);
}
}
|
Python3
maxn = int ( 1e6 ) + 5
BITree = [ 0 ] * (maxn)
def is_prime(n):
if (n < = 1 ):
return False
i = 2
while (i * i < = n):
if (n % i = = 0 ):
return False
i + = 1
return True
def update_bitree(index, value):
while (index < = maxn):
BITree[index] + = value
index + = (index & ( - index))
def sum_bitree(index):
s = 0
while (index > 0 ):
s + = BITree[index]
index - = (index & ( - index))
return s
def countSmallerPrimes(ar, N):
ans = [ 0 ] * (N)
global BITree
for i in range (N - 1 , 0 , - 1 ):
ans[i] = sum_bitree(ar[i])
if (is_prime(ar[i])):
update_bitree(ar[i], 1 )
ans[ 0 ] = 2
for i in range (N):
print (ans[i], end = " " )
if __name__ = = '__main__' :
ar = [ 5 , 5 , 17 , 9 , 12 ,
15 , 11 , 7 , 39 , 3 ]
N = len (ar)
countSmallerPrimes(ar, N)
|
C#
using System;
class GFG{
static int maxn =
( int )1e6 + 5;
static int []BITree =
new int [maxn];
static bool is_prime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2;
i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
static void update_bitree( int []BITree,
int index,
int value)
{
while (index <= maxn)
{
BITree[index] += value;
index += (index & (-index));
}
}
static int sum_bitree( int []BITree,
int index)
{
int s = 0;
while (index > 0)
{
s += BITree[index];
index -= (index & (-index));
}
return s;
}
static void countSmallerPrimes( int []BITree,
int []ar, int N)
{
int []ans = new int [N];
for ( int i = N - 1; i >= 0; i--)
{
ans[i] = sum_bitree(BITree,
ar[i]);
if (is_prime(ar[i]))
update_bitree(BITree,
ar[i], 1);
}
for ( int i = 0; i < N; i++)
Console.Write(ans[i] + " " );
}
public static void Main(String[] args)
{
int []ar = {5, 5, 17, 9, 12,
15, 11, 7, 39, 3};
int N = ar.Length;
countSmallerPrimes(BITree, ar, N);
}
}
|
Javascript
<script>
let maxn = 1e6 + 5;
let BITree = new Array(maxn);
BITree.fill(0);
function is_prime(n)
{
if (n <= 1)
return false ;
for (let i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
function update_bitree(BITree, index, value)
{
while (index <= maxn)
{
BITree[index] += value;
index += (index & (-index));
}
}
function sum_bitree(BITree, index)
{
let s = 0;
while (index > 0)
{
s += BITree[index];
index -= (index & (-index));
}
return s;
}
function countSmallerPrimes(BITree, ar, N)
{
let ans = new Array(N);
for (let i = N - 1; i >= 0; i--)
{
ans[i] = sum_bitree(BITree, ar[i]);
if (is_prime(ar[i]))
update_bitree(BITree, ar[i], 1);
}
for (let i = 0; i < N; i++)
document.write(ans[i] + " " );
}
let ar = [5, 5, 17, 9, 12, 15, 11, 7, 39, 3];
let N = ar.length;
countSmallerPrimes(BITree, ar, N);
</script>
|
Output
2 1 3 2 3 3 2 1 1 0
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...