给定一个大小为N的数组A [] ,每个数组元素的任务是对右边的数组元素进行计数,这些数组元素小于它并且是素数。
例子:
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.
天真的方法:解决此问题的最简单方法是遍历数组,并对每个元素A [i]迭代其右边的所有元素,并计算小于A [i]且为质数的元素的数量。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to check if a
// number is prime or not
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;
}
// Function to find the count of
// smaller primes on the right
// of each array element
void countSmallerPrimes(int ar[], int N)
{
for (int i = 0; i < N; i++) {
// Stores the count of
// smaller primes
int count = 0;
for (int j = i + 1; j < N; j++) {
// If A[j] <= A[i] and A[j] is prime
if (ar[j] <= ar[i] && is_prime(ar[j])) {
// Increase count
count++;
}
}
// Print the count for
// the current element
cout << count << " ";
}
}
// Driver Code
int main()
{
int ar[] = { 43, 3, 5, 7, 2, 41 };
int N = sizeof ar / sizeof ar[0];
// Function call
countSmallerPrimes(ar, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check if a
// number is prime or not
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;
}
// Function to find the count of
// smaller primes on the right
// of each array element
static void countSmallerPrimes(int ar[], int N)
{
for(int i = 0; i < N; i++)
{
// Stores the count of
// smaller primes
int count = 0;
for(int j = i + 1; j < N; j++)
{
// If A[j] <= A[i] and A[j] is prime
if (ar[j] <= ar[i] && is_prime(ar[j]))
{
// Increase count
count++;
}
}
// Print the count for
// the current element
System.out.print(count + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int ar[] = { 43, 3, 5, 7, 2, 41 };
int N = ar.length;
// Function call
countSmallerPrimes(ar, N);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to check if a
# number is prime or not
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
# Function to find the count of
# smaller primes on the right
# of each array element
def countSmallerPrimes(ar, N):
for i in range(N):
# Stores the count of
# smaller primes
count = 0
for j in range(i + 1, N):
# If A[j] <= A[i] and A[j] is prime
if (ar[j] <= ar[i] and is_prime(ar[j])):
# Increase count
count += 1
# Print the count for
# the current element
print(count, end = " ")
# Driver Code
if __name__ == '__main__':
ar = [ 43, 3, 5, 7, 2, 41 ]
N = len(ar)
# Function call
countSmallerPrimes(ar, N)
# This code is contributed by mohit kumar 29
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to check if a
// number is prime or not
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;
}
// Function to find the count of
// smaller primes on the right
// of each array element
static void countSmallerPrimes(int[] ar,
int N)
{
for (int i = 0; i < N; i++)
{
// Stores the count of
// smaller primes
int count = 0;
for (int j = i + 1; j < N; j++)
{
// If A[j] <= A[i]
// and A[j] is prime
if (ar[j] <= ar[i] &&
is_prime(ar[j]))
{
// Increase count
count++;
}
}
// Print the count for
// the current element
Console.Write(count + " ");
}
}
// Driver Code
public static void Main()
{
int[] ar = {43, 3, 5,
7, 2, 41};
int N = ar.Length;
// Function call
countSmallerPrimes(ar, N);
}
}
// This code is contributed by Chitranayal
C++
// C++ Program for the above approach
#include "bits/stdc++.h"
using namespace std;
const int maxn = 1e6 + 5;
int BITree[maxn];
// Function to check if a
// number is prime or not
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;
}
// Function to update a Binary Tree
void update_bitree(int BITree[],
int index, int value)
{
while (index <= maxn) {
BITree[index] += value;
index += (index & (-index));
}
}
// Function to find the sum of
// all the elements which are
// less than or equal to index
int sum_bitree(int BITree[], int index)
{
int s = 0;
while (index > 0) {
s += BITree[index];
index -= (index & (-index));
}
return s;
}
// Function to find the number
// of smaller primes on the right
// for every array element
void countSmallerPrimes(int BITree[],
int ar[], int N)
{
int ans[N];
// Iterate the array in backwards
for (int i = N - 1; i >= 0; i--) {
// Calculating the required
// number of primes
ans[i] = sum_bitree(BITree, ar[i]);
// If current array
// element is prime
if (is_prime(ar[i]))
// Update the Fenwick tree
update_bitree(BITree, ar[i], 1);
}
for (int i = 0; i < N; i++)
cout << ans[i] << " ";
}
// Driver Code
int main()
{
int ar[] = { 5, 5, 17, 9, 12,
15, 11, 7, 39, 3 };
int N = sizeof ar / sizeof ar[0];
// Function call
countSmallerPrimes(BITree, ar, N);
return 0;
}
Java
// Java Program for the
// above approach
//include "bits/stdJava.h"
import java.util.*;
class GFG{
static int maxn =
(int)1e6 + 5;
static int []BITree =
new int[maxn];
// Function to check if a
// number is prime or not
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;
}
// Function to update a Binary Tree
static void update_bitree(int BITree[],
int index,
int value)
{
while (index <= maxn)
{
BITree[index] += value;
index += (index & (-index));
}
}
// Function to find the sum of
// all the elements which are
// less than or equal to index
static int sum_bitree(int BITree[],
int index)
{
int s = 0;
while (index > 0)
{
s += BITree[index];
index -= (index & (-index));
}
return s;
}
// Function to find the number
// of smaller primes on the right
// for every array element
static void countSmallerPrimes(int BITree[],
int ar[], int N)
{
int []ans = new int[N];
// Iterate the array in backwards
for (int i = N - 1; i >= 0; i--)
{
// Calculating the required
// number of primes
ans[i] = sum_bitree(BITree,
ar[i]);
// If current array
// element is prime
if (is_prime(ar[i]))
// Update the Fenwick tree
update_bitree(BITree,
ar[i], 1);
}
for (int i = 0; i < N; i++)
System.out.print(ans[i] + " ");
}
// Driver Code
public static void main(String[] args)
{
int ar[] = {5, 5, 17, 9, 12,
15, 11, 7, 39, 3};
int N = ar.length;
// Function call
countSmallerPrimes(BITree, ar, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the
# above approach
# include "bits/stdPython.h"
maxn = int(1e6) + 5
BITree = [0] * (maxn)
# Function to check if a
# number is prime or not
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
# Function to update a Binary Tree
def update_bitree(index, value):
while (index <= maxn):
BITree[index] += value
index += (index & (-index))
# Function to find the sum of
# all the elements which are
# less than or equal to index
def sum_bitree(index):
s = 0
while (index > 0):
s += BITree[index]
index -= (index & (-index))
return s
# Function to find the number
# of smaller primes on the right
# for every array element
def countSmallerPrimes(ar, N):
ans = [0] * (N)
global BITree
# Iterate the array in backwards
for i in range(N - 1, 0, -1):
# Calculating the required
# number of primes
ans[i] = sum_bitree(ar[i])
# If current array
# element is prime
if (is_prime(ar[i])):
# Update the Fenwick tree
update_bitree(ar[i], 1)
ans[0] = 2
for i in range(N):
print(ans[i], end = " ")
# Driver Code
if __name__ == '__main__':
ar = [ 5, 5, 17, 9, 12,
15, 11, 7, 39, 3 ]
N = len(ar)
# Function call
countSmallerPrimes(ar, N)
# This code is contributed by Amit Katiyar
C#
// C# Program for the
// above approach
//include "bits/stdJava.h"
using System;
class GFG{
static int maxn =
(int)1e6 + 5;
static int []BITree =
new int[maxn];
// Function to check if a
// number is prime or not
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;
}
// Function to update a Binary Tree
static void update_bitree(int []BITree,
int index,
int value)
{
while (index <= maxn)
{
BITree[index] += value;
index += (index & (-index));
}
}
// Function to find the sum of
// all the elements which are
// less than or equal to index
static int sum_bitree(int []BITree,
int index)
{
int s = 0;
while (index > 0)
{
s += BITree[index];
index -= (index & (-index));
}
return s;
}
// Function to find the number
// of smaller primes on the right
// for every array element
static void countSmallerPrimes(int []BITree,
int []ar, int N)
{
int []ans = new int[N];
// Iterate the array in backwards
for (int i = N - 1; i >= 0; i--)
{
// Calculating the required
// number of primes
ans[i] = sum_bitree(BITree,
ar[i]);
// If current array
// element is prime
if (is_prime(ar[i]))
// Update the Fenwick tree
update_bitree(BITree,
ar[i], 1);
}
for (int i = 0; i < N; i++)
Console.Write(ans[i] + " ");
}
// Driver Code
public static void Main(String[] args)
{
int []ar = {5, 5, 17, 9, 12,
15, 11, 7, 39, 3};
int N = ar.Length;
// Function call
countSmallerPrimes(BITree, ar, N);
}
}
// This code is contributed by Amit Katiyar
5 1 1 1 0 0
时间复杂度: O(N 2 )
辅助空间: O(N)
高效的方法:可以观察到可以通过从右到左遍历数组并使用Fenwick树计算每个数组元素的素数计数来优化上述方法。请按照以下步骤解决问题:
- 从右到左遍历数组,并通过getSum()计算当前数组元素右侧较小素数的计数。打印获得的计数。
- 现在,检查当前数组元素是否为质数,并相应地更新Fenwick树。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include "bits/stdc++.h"
using namespace std;
const int maxn = 1e6 + 5;
int BITree[maxn];
// Function to check if a
// number is prime or not
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;
}
// Function to update a Binary Tree
void update_bitree(int BITree[],
int index, int value)
{
while (index <= maxn) {
BITree[index] += value;
index += (index & (-index));
}
}
// Function to find the sum of
// all the elements which are
// less than or equal to index
int sum_bitree(int BITree[], int index)
{
int s = 0;
while (index > 0) {
s += BITree[index];
index -= (index & (-index));
}
return s;
}
// Function to find the number
// of smaller primes on the right
// for every array element
void countSmallerPrimes(int BITree[],
int ar[], int N)
{
int ans[N];
// Iterate the array in backwards
for (int i = N - 1; i >= 0; i--) {
// Calculating the required
// number of primes
ans[i] = sum_bitree(BITree, ar[i]);
// If current array
// element is prime
if (is_prime(ar[i]))
// Update the Fenwick tree
update_bitree(BITree, ar[i], 1);
}
for (int i = 0; i < N; i++)
cout << ans[i] << " ";
}
// Driver Code
int main()
{
int ar[] = { 5, 5, 17, 9, 12,
15, 11, 7, 39, 3 };
int N = sizeof ar / sizeof ar[0];
// Function call
countSmallerPrimes(BITree, ar, N);
return 0;
}
Java
// Java Program for the
// above approach
//include "bits/stdJava.h"
import java.util.*;
class GFG{
static int maxn =
(int)1e6 + 5;
static int []BITree =
new int[maxn];
// Function to check if a
// number is prime or not
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;
}
// Function to update a Binary Tree
static void update_bitree(int BITree[],
int index,
int value)
{
while (index <= maxn)
{
BITree[index] += value;
index += (index & (-index));
}
}
// Function to find the sum of
// all the elements which are
// less than or equal to index
static int sum_bitree(int BITree[],
int index)
{
int s = 0;
while (index > 0)
{
s += BITree[index];
index -= (index & (-index));
}
return s;
}
// Function to find the number
// of smaller primes on the right
// for every array element
static void countSmallerPrimes(int BITree[],
int ar[], int N)
{
int []ans = new int[N];
// Iterate the array in backwards
for (int i = N - 1; i >= 0; i--)
{
// Calculating the required
// number of primes
ans[i] = sum_bitree(BITree,
ar[i]);
// If current array
// element is prime
if (is_prime(ar[i]))
// Update the Fenwick tree
update_bitree(BITree,
ar[i], 1);
}
for (int i = 0; i < N; i++)
System.out.print(ans[i] + " ");
}
// Driver Code
public static void main(String[] args)
{
int ar[] = {5, 5, 17, 9, 12,
15, 11, 7, 39, 3};
int N = ar.length;
// Function call
countSmallerPrimes(BITree, ar, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the
# above approach
# include "bits/stdPython.h"
maxn = int(1e6) + 5
BITree = [0] * (maxn)
# Function to check if a
# number is prime or not
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
# Function to update a Binary Tree
def update_bitree(index, value):
while (index <= maxn):
BITree[index] += value
index += (index & (-index))
# Function to find the sum of
# all the elements which are
# less than or equal to index
def sum_bitree(index):
s = 0
while (index > 0):
s += BITree[index]
index -= (index & (-index))
return s
# Function to find the number
# of smaller primes on the right
# for every array element
def countSmallerPrimes(ar, N):
ans = [0] * (N)
global BITree
# Iterate the array in backwards
for i in range(N - 1, 0, -1):
# Calculating the required
# number of primes
ans[i] = sum_bitree(ar[i])
# If current array
# element is prime
if (is_prime(ar[i])):
# Update the Fenwick tree
update_bitree(ar[i], 1)
ans[0] = 2
for i in range(N):
print(ans[i], end = " ")
# Driver Code
if __name__ == '__main__':
ar = [ 5, 5, 17, 9, 12,
15, 11, 7, 39, 3 ]
N = len(ar)
# Function call
countSmallerPrimes(ar, N)
# This code is contributed by Amit Katiyar
C#
// C# Program for the
// above approach
//include "bits/stdJava.h"
using System;
class GFG{
static int maxn =
(int)1e6 + 5;
static int []BITree =
new int[maxn];
// Function to check if a
// number is prime or not
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;
}
// Function to update a Binary Tree
static void update_bitree(int []BITree,
int index,
int value)
{
while (index <= maxn)
{
BITree[index] += value;
index += (index & (-index));
}
}
// Function to find the sum of
// all the elements which are
// less than or equal to index
static int sum_bitree(int []BITree,
int index)
{
int s = 0;
while (index > 0)
{
s += BITree[index];
index -= (index & (-index));
}
return s;
}
// Function to find the number
// of smaller primes on the right
// for every array element
static void countSmallerPrimes(int []BITree,
int []ar, int N)
{
int []ans = new int[N];
// Iterate the array in backwards
for (int i = N - 1; i >= 0; i--)
{
// Calculating the required
// number of primes
ans[i] = sum_bitree(BITree,
ar[i]);
// If current array
// element is prime
if (is_prime(ar[i]))
// Update the Fenwick tree
update_bitree(BITree,
ar[i], 1);
}
for (int i = 0; i < N; i++)
Console.Write(ans[i] + " ");
}
// Driver Code
public static void Main(String[] args)
{
int []ar = {5, 5, 17, 9, 12,
15, 11, 7, 39, 3};
int N = ar.Length;
// Function call
countSmallerPrimes(BITree, ar, N);
}
}
// This code is contributed by Amit Katiyar
2 1 3 2 3 3 2 1 1 0
时间复杂度: O(N log N)
辅助空间: O(N)