Given an array of integers (less than 10^6), the task is to find the sum of all the prime numbers which appear after every (k-1) prime numbers
i.e. every K’th prime number in the array.
Input : Array : 2, 3, 5, 7, 11 ; n=5; k=2 Output : Sum = 10 Explanation: All the elements of the array are prime. So, the prime numbers after every K intervals are 3, 7 and their sum is 10. Input : Array : 41, 23, 12, 17, 18, 19 ; n=6; k=2 Output : Sum = 42
A simple approach
We have to traverse the array and find the prime numbers after every (k-1) prime numbers. In this way, we’ll have to check every element of the array whether it is prime or not which will take more time as the size of the array increases.
We will create a sieve which will store whether a number is prime or not. Then, it can be used to check a number against prime in O(1) time. In this way, we only have to keep track of every K’th prime number and maintain the running sum.
Below is the implementation of the above approach:
- Insert minimum number in array so that sum of array becomes prime
- Sum of every K’th prime number in an array
- Number of 0s and 1s at prime positions in the given array
- Number of co-prime pairs in an array
- Product of every K’th prime number in an array
- Number of prime pairs in an array
- Smallest prime number missing in an array
- Find prime number K in an array such that (A[i] % K) is maximum
- Sum of Maximum and Minimum prime factor of every number in the Array
- Check if LCM of array elements is divisible by a prime number or not
- Print prime numbers with prime sum of digits in an array
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Print the nearest prime number formed by adding prime numbers to N
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Check whether the sum of prime elements of the array is prime or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.