-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3 Divisors.java
More file actions
80 lines (72 loc) · 2.06 KB
/
3 Divisors.java
File metadata and controls
80 lines (72 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class GFG
{
public static void main(String args[])throws IOException
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int q = sc.nextInt();
ArrayList<Long> query = new ArrayList<>();
for(int i=0;i<q;i++){
query.add(sc.nextLong());
}
Solution ob = new Solution();
ArrayList<Integer> ans = ob.threeDivisors(query,q);
for(int cnt : ans) {
System.out.println(cnt);
}
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution{
// Time Complexity :- O(q*N*log(log(N)));
// Space Complexity :- O(N), where N is min(10^6,N);
static ArrayList<Integer> threeDivisors(ArrayList<Long> query, int q){
// code here
ArrayList<Integer> prime = primeNumber(1000000);
ArrayList<Integer> ans = new ArrayList<>();
for(long q1 : query) {
ans.add(solve(prime,q1));
}
return ans;
}
public static ArrayList<Integer> primeNumber(int n) {
int[] prime = new int[n+1];
Arrays.fill(prime,1);
prime[0]=0;
prime[1]=0;
for(int i=2;i<=n;i++) {
if(prime[i]==1) {
for(int j=i+i;j<=n;j+=i) {
prime[j]=0;
}
}
}
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<=n;i++) {
if(prime[i]==1) list.add(i);
}
return list;
}
public static int solve(ArrayList<Integer> prime,long q) {
int v1 = (int)Math.sqrt(q);
int lo=0,hi=prime.size()-1;
int ans=0;
while(lo<=hi) {
int mid = lo+(hi-lo)/2;
if(prime.get(mid)<=v1) {
lo=mid+1;
ans = Math.max(ans,mid+1);
} else {
hi=mid-1;
}
}
return ans;
}
}