给定一个大小为N的数组arr[]和一个整数K ,任务是打印一个大小为K的子数组,其元素之和为素数。如果存在多个这样的子数组,则打印它们中的任何一个。
例子:
Input: arr[] = {20, 7, 5, 4, 3, 11, 99, 87, 23, 45}, K = 4
Output: 7 5 4 3
Explanation: Sum of the elements of the subarray {7, 5, 4, 3} is 19.
Therefore, the required output is 7 5 4 3.
Input: arr[] = {11, 13, 17}, K = 1
Output: 11
方法:解决这个问题的想法是使用滑动窗口技术找到所有大小为K的子数组的总和,并检查它是否为素数。请按照以下步骤解决问题。
- 使用 Eratosthenes 筛法生成 [1, 1000000] 范围内的所有素数,以检查一个数是否为素数。
- 初始化一个变量,比如currSum来存储大小为K的子数组的元素总和。
- 找到数组的前K 个元素的总和并将其存储在currSum 中。
- 一一迭代剩余的数组元素,并通过添加第i个元素并从数组中删除第(i – K)个元素来更新currSum 。现在,检查currSum是否为素数。
- 如果发现currSum是素数,则打印当前子数组的所有元素。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Generate all prime numbers
// in the range [1, 1000000]
void sieve(bool prime[])
{
// Set all numbers as
// prime initially
for (int i = 0; i < 1000000;
i++) {
prime[i] = true;
}
// Mark 0 and 1 as non-prime
prime[0] = prime[1] = false;
for (int i = 2; i * i <= 1000000; i++) {
// If current element is prime
if (prime[i]) {
// Mark all its multiples
// as non-prime
for (int j = i * i; j <= 1000000;
j += i) {
prime[j] = false;
}
}
}
}
// Function to print the subarray
// whose sum of elements is prime
void subPrimeSum(int N, int K,
int arr[],
bool prime[])
{
// Store the current subarray
// of size K
int currSum = 0;
// Calculate the sum of
// first K elements
for (int i = 0; i < K; i++) {
currSum += arr[i];
}
// Check if currSum is prime
if (prime[currSum]) {
for (int i = 0; i < K; i++) {
cout << arr[i] << " ";
}
return;
}
// Store the start and last
// index of subarray of size K
int st = 1, en = K;
// Iterate over remaining array
while (en < N) {
currSum += arr[en] - arr[st - 1];
// Check if currSum
if (prime[currSum]) {
for (int i = st; i <= en; i++) {
cout << arr[i] << " ";
}
return;
}
en++;
st++;
}
}
// Driver Code
int main()
{
int arr[] = { 20, 7, 5, 4, 3, 11,
99, 87, 23, 45 };
int K = 4;
int N = sizeof(arr) / sizeof(arr[0]);
bool prime[1000000];
sieve(prime);
subPrimeSum(N, K, arr, prime);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Generate all prime numbers
// in the range [1, 1000000]
static void sieve(boolean prime[])
{
// Set all numbers as
// prime initially
for (int i = 0; i < 1000000; i++)
{
prime[i] = true;
}
// Mark 0 and 1 as non-prime
prime[0] = prime[1] = false;
for (int i = 2; i * i <= 1000000; i++)
{
// If current element is prime
if (prime[i])
{
// Mark all its multiples
// as non-prime
for (int j = i * i; j < 1000000; j += i)
{
prime[j] = false;
}
}
}
}
// Function to print the subarray
// whose sum of elements is prime
static void subPrimeSum(int N, int K,
int arr[],
boolean prime[])
{
// Store the current subarray
// of size K
int currSum = 0;
// Calculate the sum of
// first K elements
for (int i = 0; i < K; i++)
{
currSum += arr[i];
}
// Check if currSum is prime
if (prime[currSum])
{
for (int i = 0; i < K; i++)
{
System.out.print(arr[i] + " ");
}
return;
}
// Store the start and last
// index of subarray of size K
int st = 1, en = K;
// Iterate over remaining array
while (en < N)
{
currSum += arr[en] - arr[st - 1];
// Check if currSum
if (prime[currSum])
{
for (int i = st; i <= en; i++)
{
System.out.print(arr[i] + " ");
}
return;
}
en++;
st++;
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = {20, 7, 5, 4, 3, 11,
99, 87, 23, 45};
int K = 4;
int N = arr.length;
boolean []prime = new boolean[1000000];
sieve(prime);
subPrimeSum(N, K, arr, prime);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program to implement
# the above approach
# Generate all prime numbers
# in the range [1, 1000000]
def sieve(prime):
# Set all numbers as
# prime initially
for i in range(1000000):
prime[i] = True
# Mark 0 and 1 as non-prime
prime[0] = prime[1] = False
for i in range(2, 1000 + 1):
# If current element is prime
if (prime[i]):
# Mark all its multiples
# as non-prime
for j in range(i * i, 1000000, i):
prime[j] = False
return prime
# Function to prthe subarray
# whose sum of elements is prime
def subPrimeSum(N, K, arr, prime):
# Store the current subarray
# of size K
currSum = 0
# Calculate the sum of
# first K elements
for i in range(0, K):
currSum += arr[i]
# Check if currSum is prime
if (prime[currSum]):
for i in range(K):
print(arr[i], end = " ")
return
# Store the start and last
# index of subarray of size K
st = 1
en = K
# Iterate over remaining array
while (en < N):
currSum += arr[en] - arr[st - 1]
# Check if currSum
if (prime[currSum]):
for i in range(st, en + 1):
print(arr[i], end = " ")
return
en += 1
st += 1
# Driver Code
if __name__ == '__main__':
arr = [ 20, 7, 5, 4, 3,
11, 99, 87, 23, 45 ]
K = 4
N = len(arr)
prime = [False] * 1000000
prime = sieve(prime)
subPrimeSum(N, K, arr, prime)
# This code is contributed by Amit Katiyar
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Generate all prime numbers
// in the range [1, 1000000]
static void sieve(bool []prime)
{
// Set all numbers as
// prime initially
for (int i = 0; i < 1000000; i++)
{
prime[i] = true;
}
// Mark 0 and 1 as non-prime
prime[0] = prime[1] = false;
for (int i = 2; i * i <= 1000000; i++)
{
// If current element is prime
if (prime[i])
{
// Mark all its multiples
// as non-prime
for (int j = i * i; j < 1000000; j += i)
{
prime[j] = false;
}
}
}
}
// Function to print the subarray
// whose sum of elements is prime
static void subPrimeSum(int N, int K,
int []arr,
bool []prime)
{
// Store the current subarray
// of size K
int currSum = 0;
// Calculate the sum of
// first K elements
for (int i = 0; i < K; i++)
{
currSum += arr[i];
}
// Check if currSum is prime
if (prime[currSum])
{
for (int i = 0; i < K; i++)
{
Console.Write(arr[i] + " ");
}
return;
}
// Store the start and last
// index of subarray of size K
int st = 1, en = K;
// Iterate over remaining array
while (en < N)
{
currSum += arr[en] - arr[st - 1];
// Check if currSum
if (prime[currSum])
{
for (int i = st; i <= en; i++)
{
Console.Write(arr[i] + " ");
}
return;
}
en++;
st++;
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = {20, 7, 5, 4, 3, 11,
99, 87, 23, 45};
int K = 4;
int N = arr.Length;
bool []prime = new bool[1000000];
sieve(prime);
subPrimeSum(N, K, arr, prime);
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
7 5 4 3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live