-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob1498-optimized.java
More file actions
58 lines (57 loc) · 1.48 KB
/
prob1498-optimized.java
File metadata and controls
58 lines (57 loc) · 1.48 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
class Solution {
public int numSubseq(int[] arr, int tar) {
long c=0l;
int n=arr.length;
int p1=0;
Arrays.sort(arr);
int mod=(int)(Math.pow(10,9))+7;
for(int i=0;i<n;i++){
int a=arr[i];
if(a+a<=tar){
c++;
c=(c)%mod;
}
}
int m=(int)Math.pow(10,5)+1;
int[]valarr=new int[m];
int temp=1;
for(int i=0;i<m;i++){
valarr[i]=temp;
temp=(temp*2)%mod;
}
// System.out.println(c);
int p2=n-1;
while(p1<p2){
int a=arr[p1];
int b=arr[p2];
if(a+b<=tar){
int val=p2-p1;
// long imp_value=fun(2,val,mod);
long imp_value=valarr[val];
c=(long)(c*1l+((imp_value*1l-1 +mod)%mod +mod)%mod + mod)%mod;
// c+=(int)Math.pow(2,val);
// System.out.println(c);
c=(c+mod)%mod;
p1++;
}
else{
p2--;
}
}
return (int)(c+mod)%mod;
}
public long fun(int a, int val,int mod){ //very nice function, not needed though🥲
long ans=1;
while(val>0){
if(a%2!=0){
val/=2;
a=(a*a)%mod;
}
else{
val--;
ans=(ans*a)%mod;
}
}
return ans%mod;
}
}