
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find prime number K in an array such that (A[i] % K) is maximum in C++
Suppose we have an array A with n integers. We have to find one element K such that K is prime, and A[i] mod K is maximum for all valid i among all possible values of K. If no such numbers are found, then return -1. For example, if A = [2, 10, 15, 7, 6, 8, 13], then the output will be 13. There are three prime numbers 2, 7, and 13. The maximum possible values of A[i] mod 2 is 1, (15 mod 2), for 7, it will be 6 mod 7 = 6, and for 13, it will be 10 mod 13 = 10. This is the maximum.
To maximize the value of A[i] mod K, the K must be max prime number in A, and A[i] must be the greatest element from the array which is less than K. So we will just find max prime number in the array. To get that we can use Sieve to find all the prime numbers less than or equal to the maximum element from the array. Then find the maximum prime number from the array and print it. If there is no prime, then return -1.
Example
#include<iostream> #include<algorithm> #include<vector> using namespace std; int getMaxPrime(int arr[], int n) { int max_elem = *max_element(arr, arr + n); vector<bool> prime_vals(max_elem + 1, true); prime_vals[0] = false; prime_vals[1] = false; for (int p = 2; p * p <= max_elem; p++) { if (prime_vals[p] == true) { for (int i = p * 2; i <= max_elem; i += p) prime_vals[i] = false; } } int maximum = -1; for (int i = 0; i < n; i++) { if (prime_vals[arr[i]]) maximum = max(maximum, arr[i]); } return maximum; } int main() { int arr[] = { 2, 10, 15, 7, 6, 8, 13 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Max prime is: " << getMaxPrime(arr, n); }
Output
Max prime is: 13
- Related Articles
- Maximum value K such that array has at-least K elements that are >= K in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- C++ program to find ‘k’ such that its modulus with each array element is same
- Finding ‘k’ such that its modulus with each array element is same in C++
- Find k maximum elements of array in original order in C++
- k-th prime factor of a given number in java
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
- Find nth number that contains the digit k or divisible by k in C++
- Find smallest number n such that n XOR n+1 equals to given k in C++
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K in C++
