You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// from : https://leetcode.com/problems/find-a-value-of-a-mysterious-function-closest-to-target/discuss/743741/Detailed-General-Ideasolution-for-such-problems-(-AND-OR-GCD-)-in-O(N-*-log(-max(arri)-)-)
// 1416 ms
int closestToTarget1(vector<int>& arr, int target) {
set<int> s;
int n = arr.size();
int res=INT_MAX;
for(int i=n-1;i>=0;--i){
set<int> temp;
temp.insert(arr[i]);
res = min(res,abs(target-arr[i]));
for(auto v: s) {
int val = v & arr[i];
res = min(res,abs(target-val));
temp.insert(val);
}
s = temp;
}
return res;
}
// 213 ms
int closestToTarget(vector<int>& arr, int target) {