A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.
A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.
You are given two non-empty arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.
Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.
For example, consider an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20The number of semiprimes within each of these ranges is as follows:
- (1, 26) is 10,
- (4, 10) is 4,
- (16, 20) is 0.
Write a function:
class Solution { public int[] solution(int N, int[] P, int[] Q); }
that, given an integer N and two non-empty arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries.
For example, given an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20the function should return the values [10, 4, 0], as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- M is an integer within the range [1..30,000];
- each element of arrays P and Q is an integer within the range [1..N];
- P[i] ≤ Q[i].
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
// write your code in Java SE 8
if (N<4) return new int[P.length];
boolean[] notPrime = new boolean[N+1];
notPrime[0] = true; notPrime[1] = true; notPrime[2] = false;
int numPrimes = 0;
for (int i=2; i<=N; i++){
if (notPrime[i]) continue;
numPrimes++;
for (int k=i*2; k<=N/2; k+=i){
notPrime[k] = true;
}
}
int[] primes = new int[numPrimes];
numPrimes = 0;
for (int i=2; i<=N; i++){
if (!notPrime[i]) primes[numPrimes++]=i;
}
boolean[] isSemiPrime = new boolean[N+1];
int numSemiPrimes=0;
for (int i=0; i<numPrimes; i++){
for (int j=i; j<numPrimes && primes[j]<=N/primes[i]; j++){
// if (N/primes[i] < primes[j]) continue;
int k = primes[i]*primes[j];
isSemiPrime[k] = true;
numSemiPrimes++;
}
}
int[] semiPrimes = new int[numSemiPrimes];
numSemiPrimes = 0;
for (int i=4; i<=N; i++){
if (isSemiPrime[i]) semiPrimes[numSemiPrimes++]=i;
}
int[] ret = new int[P.length];
for (int i=0; i<P.length; i++){
int left = findNext(P[i], semiPrimes);
if (left<0) {ret[i]=0; continue; }
int right = findPrev(Q[i], semiPrimes);
if (right<0) {ret[i]=0; continue; }
int num = right-left+1;
ret[i] = (num<0)?0:num;
}
return ret;
}
int findNext(int val, int[] A){
//find the smallest x such that A[x]>=val, elements in A are distinct
int start = 0, end = A.length-1;
if (A[end]<val) return -1;
while (start<end){
if (A[start]>=val) return start;
int mid = (start+end)/2;
if (A[mid]>val){
end = mid;
}
else if (A[mid]<val){
start = mid+1;
}
else return mid;
}
return start;
}
int findPrev(int val, int[] A){
//find the largest x such that A[x]<=val, elements in A are distinct
int start = 0, end = A.length-1;
if (A[start]>val) return -1;
while (start<end){
if (A[end]<=val) return end;
int mid = (start+end)/2;
if (A[mid]>val){
end = mid-1;
}
else if (A[mid]<val){
start = mid+1;
}
else return mid;
}
return end;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
// write your code in Java SE 8
if (N<4) return new int[P.length];
boolean[] notPrime = new boolean[N+1];
notPrime[0] = true; notPrime[1] = true; notPrime[2] = false;
int numPrimes = 0;
for (int i=2; i<=N; i++){
if (notPrime[i]) continue;
numPrimes++;
for (int k=i*2; k<=N/2; k+=i){
notPrime[k] = true;
}
}
int[] primes = new int[numPrimes];
numPrimes = 0;
for (int i=2; i<=N; i++){
if (!notPrime[i]) primes[numPrimes++]=i;
}
boolean[] isSemiPrime = new boolean[N+1];
int numSemiPrimes=0;
for (int i=0; i<numPrimes; i++){
for (int j=i; j<numPrimes && primes[j]<=N/primes[i]; j++){
// if (N/primes[i] < primes[j]) continue;
int k = primes[i]*primes[j];
isSemiPrime[k] = true;
numSemiPrimes++;
}
}
int[] semiPrimes = new int[numSemiPrimes];
numSemiPrimes = 0;
for (int i=4; i<=N; i++){
if (isSemiPrime[i]) semiPrimes[numSemiPrimes++]=i;
}
int[] ret = new int[P.length];
for (int i=0; i<P.length; i++){
int left = findNext(P[i], semiPrimes);
if (left<0) {ret[i]=0; continue; }
int right = findPrev(Q[i], semiPrimes);
if (right<0) {ret[i]=0; continue; }
int num = right-left+1;
ret[i] = (num<0)?0:num;
}
return ret;
}
int findNext(int val, int[] A){
//find the smallest x such that A[x]>=val, elements in A are distinct
int start = 0, end = A.length-1;
if (A[end]<val) return -1;
while (start<end){
if (A[start]>=val) return start;
int mid = (start+end)/2;
if (A[mid]>val){
end = mid;
}
else if (A[mid]<val){
start = mid+1;
}
else return mid;
}
return start;
}
int findPrev(int val, int[] A){
//find the largest x such that A[x]<=val, elements in A are distinct
int start = 0, end = A.length-1;
if (A[start]>val) return -1;
while (start<=end){
if (A[end]<=val) return end;
int mid = (start+end)/2;
if (A[mid]>val){
end = mid-1;
}
else if (A[mid]<val){
start = mid+1;
}
else return mid;
}
return end;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
// write your code in Java SE 8
if (N<4) return new int[P.length];
boolean[] notPrime = new boolean[N+1];
notPrime[0] = true; notPrime[1] = true; notPrime[2] = false;
int numPrimes = 0;
for (int i=2; i<=N; i++){
if (notPrime[i]) continue;
numPrimes++;
for (int k=i*2; k<=N/2; k+=i){
notPrime[k] = true;
}
}
int[] primes = new int[numPrimes];
numPrimes = 0;
for (int i=2; i<=N; i++){
if (!notPrime[i]) primes[numPrimes++]=i;
}
boolean[] isSemiPrime = new boolean[N+1];
int numSemiPrimes=0;
for (int i=0; i<numPrimes; i++){
for (int j=i; j<numPrimes && primes[j]<=N/primes[i]; j++){
// if (N/primes[i] < primes[j]) continue;
int k = primes[i]*primes[j];
isSemiPrime[k] = true;
numSemiPrimes++;
}
}
int[] semiPrimes = new int[numSemiPrimes];
numSemiPrimes = 0;
for (int i=4; i<=N; i++){
if (isSemiPrime[i]) semiPrimes[numSemiPrimes++]=i;
}
int[] ret = new int[P.length];
for (int i=0; i<P.length; i++){
int left = findNext(P[i], semiPrimes);
if (left<0) {ret[i]=0; continue; }
int right = findPrev(Q[i], semiPrimes);
if (right<0) {ret[i]=0; continue; }
int num = right-left+1;
ret[i] = (num<0)?0:num;
}
return ret;
}
int findNext(int val, int[] A){
//find the smallest x such that A[x]>=val, elements in A are distinct
int start = 0, end = A.length-1;
if (A[end]<val) return -1;
while (start<end){
if (A[start]>=val) return start;
int mid = (start+end)/2;
if (A[mid]>val){
end = mid;
}
else if (A[mid]<val){
start = mid+1;
}
else return mid;
}
return start;
}
int findPrev(int val, int[] A){
//find the largest x such that A[x]<=val, elements in A are distinct
int start = 0, end = A.length-1;
if (A[start]>val) return -1;
while (start<=end){
if (A[end]<=val) return end;
int mid = (start+end)/2;
if (A[mid]>val){
end = mid-1;
}
else if (A[mid]<val){
start = mid+1;
}
else return mid;
}
return end;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
// write your code in Java SE 8
if (N<4) return new int[P.length];
boolean[] notPrime = new boolean[N+1];
notPrime[0] = true; notPrime[1] = true; notPrime[2] = false;
int numPrimes = 0;
for (int i=2; i<=N; i++){
if (notPrime[i]) continue;
numPrimes++;
for (int k=i*2; k<=N/2; k+=i){
notPrime[k] = true;
}
}
int[] primes = new int[numPrimes];
numPrimes = 0;
for (int i=2; i<=N; i++){
if (!notPrime[i]) primes[numPrimes++]=i;
}
boolean[] isSemiPrime = new boolean[N+1];
int numSemiPrimes=0;
for (int i=0; i<numPrimes; i++){
for (int j=i; j<numPrimes && primes[j]<=N/primes[i]; j++){
// if (N/primes[i] < primes[j]) continue;
int k = primes[i]*primes[j];
isSemiPrime[k] = true;
numSemiPrimes++;
}
}
int[] semiPrimes = new int[numSemiPrimes];
numSemiPrimes = 0;
for (int i=4; i<=N; i++){
if (isSemiPrime[i]) semiPrimes[numSemiPrimes++]=i;
}
int[] ret = new int[P.length];
for (int i=0; i<P.length; i++){
int left = findNext(P[i], semiPrimes);
if (left<0) {ret[i]=0; continue; }
int right = findPrev(Q[i], semiPrimes);
if (right<0) {ret[i]=0; continue; }
int num = right-left+1;
ret[i] = (num<0)?0:num;
}
return ret;
}
int findNext(int val, int[] A){
//find the smallest x such that A[x]>=val, elements in A are distinct
int start = 0, end = A.length-1;
if (A[end]<val) return -1;
while (start<end){
if (A[start]>=val) return start;
int mid = (start+end)/2;
if (A[mid]>val){
end = mid;
}
else if (A[mid]<val){
start = mid+1;
}
else return mid;
}
return start;
}
int findPrev(int val, int[] A){
//find the largest x such that A[x]<=val, elements in A are distinct
int start = 0, end = A.length-1;
if (A[start]>val) return -1;
while (start<=end){
if (A[end]<=val) return end;
int mid = (start+end)/2;
if (A[mid]>val){
end = mid-1;
}
else if (A[mid]<val){
start = mid+1;
}
else return mid;
}
return end;
}
}
The solution obtained perfect score.